Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugin): allow to transform entries before saving a HAR #182

Merged

Conversation

derevnjuk
Copy link
Member

@derevnjuk derevnjuk commented Feb 1, 2023

You can modify the entries before saving the HAR by using the transform option. This option should be set to a path to a module that exports a function, similar to the filter option, to change the Entry object as desired.

cy.recordHar({ transform: '../support/remove-sensitive-data.ts' });

Here's a simple example of what the remove-sensitive-data.ts module might look like:

import { Entry } from 'har-format';

const PASSWORD_REGEXP = /("password":\s*")\w+("\s*)/;

export default async (entry: Entry) => {
  try {
    // Remove sensitive information from the request body
    if (entry.request.postData?.text) {
      entry.request.postData.text = entry.request.postData.text.replace(
        PASSWORD_REGEXP,
        '$1***$2'
      );
    }

    return entry;
  } catch {
    return entry;
  }
};

In this example, the transform function will replace any instances of password with *** in both the request and response bodies of each entry.

closes #174

@derevnjuk derevnjuk added the Type: enhancement New feature or request. label Feb 1, 2023
@derevnjuk derevnjuk self-assigned this Feb 1, 2023
@derevnjuk derevnjuk force-pushed the feat_#174/allow_to_transform_entries_before_saving_a_har branch from 5fd661e to f6541f3 Compare February 1, 2023 12:26
@derevnjuk derevnjuk force-pushed the feat_#174/allow_to_transform_entries_before_saving_a_har branch from f6541f3 to 87b2447 Compare February 1, 2023 12:27
@codeclimate
Copy link

codeclimate bot commented Feb 1, 2023

Code Climate has analyzed commit 31faeba and detected 0 issues on this pull request.

The test coverage on the diff in this pull request is 100.0% (50% is the threshold).

This pull request will bring the total coverage in the repository to 92.3% (0.2% change).

View more on Code Climate.

@derevnjuk derevnjuk force-pushed the feat_#174/allow_to_transform_entries_before_saving_a_har branch from a0f1353 to e23de09 Compare February 1, 2023 13:39
@derevnjuk derevnjuk force-pushed the feat_#174/allow_to_transform_entries_before_saving_a_har branch from 54af146 to 099024a Compare February 1, 2023 14:24
@derevnjuk derevnjuk marked this pull request as ready for review February 1, 2023 14:30
@derevnjuk derevnjuk merged commit fceeb4e into master Feb 1, 2023
@derevnjuk derevnjuk deleted the feat_#174/allow_to_transform_entries_before_saving_a_har branch February 1, 2023 14:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: enhancement New feature or request.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow to transform entries before saving a HAR
2 participants