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

Allow to transform entries before saving a HAR #174

Closed
derevnjuk opened this issue Jan 24, 2023 · 0 comments · Fixed by #182 or #183
Closed

Allow to transform entries before saving a HAR #174

derevnjuk opened this issue Jan 24, 2023 · 0 comments · Fixed by #182 or #183
Assignees
Labels

Comments

@derevnjuk
Copy link
Member

derevnjuk commented Jan 24, 2023

Description
It would be useful to have a way to transform recorded entries before saving the HAR file.

Possible solution
Provide the ability to transform entries by introducing the transform option as a path to a module that exports a function to modify entries before saving the HAR. The function should take as a parameter and return an Entry object.

Here's an example of how to use the filter option:

cy.recordHar({ transform: '../support/truncate-response-body.ts' });

And here's an example of what the truncate-response-body.ts filter module might look like:

import { Entry } from 'har-format';

export default (entry: Entry): Entry => ({
  ...entry,
  response: {
    ...entry.response,
    content: {
      ...entry.response.content,
      text: entry.response.content.text?.substring(0, 100)
    }
  }
});

This example demonstrates how the transform function can be used to truncate the response body of entries.

Relates to #169 (comment)

@derevnjuk derevnjuk self-assigned this Jan 24, 2023
derevnjuk added a commit that referenced this issue Feb 1, 2023
derevnjuk added a commit that referenced this issue Feb 1, 2023
derevnjuk added a commit that referenced this issue Feb 1, 2023
derevnjuk added a commit that referenced this issue Feb 1, 2023
derevnjuk added a commit that referenced this issue Feb 1, 2023
derevnjuk added a commit that referenced this issue Feb 1, 2023
derevnjuk added a commit that referenced this issue Feb 1, 2023
derevnjuk added a commit that referenced this issue 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.

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

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

```ts
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 added a commit that referenced this issue Feb 1, 2023
derevnjuk added a commit that referenced this issue Feb 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
1 participant