Skip to content

Commit

Permalink
feat(network): allow to filter hosts (#70)
Browse files Browse the repository at this point in the history
Extend recording options by adding `includeHosts` field to include only requests on specific hosts
  • Loading branch information
tmieulet authored Jan 27, 2022
1 parent 45e0018 commit 0de0c29
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ You can set `content` flag to `false` to skip loading `content` fields in the HA
cy.recordHar({ content: false });
```

To include only requests on specific hosts, you can specify a list of hosts using `includeHosts`.

```js
cy.recordHar({ includeHosts: [ '.*.execute-api.eu-west-1.amazonaws.com'] });
```

To exclude some requests, you can specify a list of paths to be excluded using `excludePaths`.

```js
Expand Down
1 change: 1 addition & 0 deletions src/Plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface SaveOptions {
export interface RecordOptions {
content: boolean;
excludePaths: string[];
includeHosts: string[];
}

export class Plugin {
Expand Down
14 changes: 12 additions & 2 deletions src/network/NetworkObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,9 +542,19 @@ export class NetworkObserver {
}

private excludeRequest(request: NetworkRequest): boolean {
const { path = '/' } = request.parsedURL;
const { host, path = '/' } = request.parsedURL;
const { includeHosts, excludePaths } = this.options;
if (includeHosts?.length > 0) {
if (
!includeHosts.some((hostPattern: string): boolean =>
new RegExp(hostPattern).test(host)
)
) {
return true;
}
}

return !!this.options.excludePaths?.some((excludedPath: string): boolean =>
return !!excludePaths?.some((excludedPath: string): boolean =>
new RegExp(excludedPath).test(path)
);
}
Expand Down

0 comments on commit 0de0c29

Please sign in to comment.