Skip to content

Commit dacc39e

Browse files
authored
feat(network): allow excluding requests by any status code (#143)
The `minStatusCodeToInclude` option has been deprecated. Alternatively, you can have more granular control over the requests to exclude by passing an array of status codes you want to exclude from the recorded HAR file. ```js cy.recordHar({ excludeStatusCodes: [200, 201, 204] }); ``` > Please note that both options `minStatusCodeToInclude` and `excludeStatusCodes` are mutually exclusive. closes #140
1 parent 2b1eb1b commit dacc39e

File tree

8 files changed

+140
-34
lines changed

8 files changed

+140
-34
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,16 @@ For example, to only include requests that have a status code of 400 or greater,
182182
cy.recordHar({ minStatusCodeToInclude: 400 });
183183
```
184184

185+
> As of version 6, this option will be removed. Use `excludeStatusCodes` instead.
186+
187+
Alternatively, you can have more granular control over the requests to exclude by passing an array of status codes you want to exclude from the recorded HAR file.
188+
189+
```js
190+
cy.recordHar({ excludeStatusCodes: [200, 201, 204] });
191+
```
192+
193+
> Please note that both options `minStatusCodeToInclude` and `excludeStatusCodes` are mutually exclusive.
194+
185195
By default, when you use `recordHar` command, it will include the blob requests in the recorded HAR file. However, those requests only make sense when they are used on the same page they were created. To exclude the blob requests from the recorded HAR file, set the `includeBlobs` to false as follows:
186196

187197
```js

src/network/NetworkObserverOptions.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ export interface NetworkObserverOptions {
33
excludePaths?: string[];
44
includeHosts?: string[];
55
includeMimes?: string[];
6+
excludeStatusCodes?: number[];
7+
/**
8+
* @deprecated As of version 6, this field will be removed. Use {@link excludeStatusCodes} instead.
9+
*/
610
minStatusCodeToInclude?: number;
711
/**
812
* @deprecated As of version 6, this flag will be disabled by default.

src/network/filters/CompositeFilter.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@ import { NetworkRequest } from '../NetworkRequest';
33
import { HostFilter } from './HostFilter';
44
import { PathFilter } from './PathFilter';
55
import { MimeFilter } from './MimeFilter';
6-
import { StatusCodeFilter } from './StatusCodeFilter';
6+
import { MinStatusCodeFilter } from './MinStatusCodeFilter';
77
import { BlobFilter } from './BlobFilter';
8+
import { StatusCodeFilter } from './StatusCodeFilter';
89

910
export class CompositeFilter implements RequestFilter {
1011
constructor(
1112
private readonly children: RequestFilter[] = [
1213
new HostFilter(),
1314
new PathFilter(),
1415
new MimeFilter(),
15-
new StatusCodeFilter(),
16-
new BlobFilter()
16+
new BlobFilter(),
17+
new MinStatusCodeFilter(),
18+
new StatusCodeFilter()
1719
]
1820
) {}
1921

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { MinStatusCodeFilter } from './MinStatusCodeFilter';
2+
import { RequestFilterOptions } from './RequestFilter';
3+
import { NetworkRequest } from '../NetworkRequest';
4+
import { describe, beforeEach, it, expect } from '@jest/globals';
5+
import { instance, mock, reset, when } from 'ts-mockito';
6+
7+
describe('MinStatusCodeFilter', () => {
8+
const networkRequestMock = mock<NetworkRequest>();
9+
10+
let sut!: MinStatusCodeFilter;
11+
12+
beforeEach(() => {
13+
sut = new MinStatusCodeFilter();
14+
});
15+
16+
afterEach(() => reset(networkRequestMock));
17+
18+
describe('wouldApply', () => {
19+
it.each([
20+
{},
21+
{ minStatusCodeToInclude: undefined },
22+
{ minStatusCodeToInclude: null },
23+
{ minStatusCodeToInclude: 'test' },
24+
{ minStatusCodeToInclude: NaN }
25+
])(
26+
'should return false when the filter is not applicable (options: %j)',
27+
options => {
28+
// act
29+
const result = sut.wouldApply(
30+
options as unknown as RequestFilterOptions
31+
);
32+
// assert
33+
expect(result).toBe(false);
34+
}
35+
);
36+
37+
it.each([
38+
{ minStatusCodeToInclude: 1 },
39+
{ minStatusCodeToInclude: -1 },
40+
{ minStatusCodeToInclude: 1.1 },
41+
{ minStatusCodeToInclude: '1' }
42+
])(
43+
'should return true when the filter is applicable (options: %j)',
44+
options => {
45+
// act
46+
const result = sut.wouldApply(
47+
options as unknown as RequestFilterOptions
48+
);
49+
// assert
50+
expect(result).toBe(true);
51+
}
52+
);
53+
});
54+
55+
describe('apply', () => {
56+
it('should return true if the request status code equals or is greater than the threshold', () => {
57+
// arrange
58+
const statusCode = 200;
59+
when(networkRequestMock.statusCode).thenReturn(statusCode);
60+
const options = { minStatusCodeToInclude: statusCode };
61+
// act
62+
const result = sut.apply(instance(networkRequestMock), options);
63+
// assert
64+
expect(result).toBe(true);
65+
});
66+
67+
it('should return false if the request status code is less than the threshold', () => {
68+
// arrange
69+
const statusCode = 101;
70+
when(networkRequestMock.statusCode).thenReturn(statusCode);
71+
const options = { minStatusCodeToInclude: 200 };
72+
// act
73+
const result = sut.apply(instance(networkRequestMock), options);
74+
// assert
75+
expect(result).toBe(false);
76+
});
77+
});
78+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { RequestFilter, RequestFilterOptions } from './RequestFilter';
2+
import { NetworkRequest } from '../NetworkRequest';
3+
4+
export class MinStatusCodeFilter implements RequestFilter {
5+
public apply(
6+
request: NetworkRequest,
7+
{ minStatusCodeToInclude = 0 }: RequestFilterOptions
8+
): boolean {
9+
const threshold = this.normalizeThreshold(minStatusCodeToInclude);
10+
11+
return request.statusCode >= threshold;
12+
}
13+
14+
public wouldApply(options: RequestFilterOptions): boolean {
15+
const threshold = this.normalizeThreshold(options.minStatusCodeToInclude);
16+
17+
return typeof threshold === 'number';
18+
}
19+
20+
private normalizeThreshold(value: unknown): number | undefined {
21+
return !isNaN(+value) && value !== null
22+
? Math.round(Math.abs(+value))
23+
: undefined;
24+
}
25+
}

src/network/filters/StatusCodeFilter.spec.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { StatusCodeFilter } from './StatusCodeFilter';
21
import { RequestFilterOptions } from './RequestFilter';
32
import { NetworkRequest } from '../NetworkRequest';
3+
import { StatusCodeFilter } from './StatusCodeFilter';
44
import { describe, beforeEach, it, expect } from '@jest/globals';
55
import { instance, mock, reset, when } from 'ts-mockito';
66

@@ -18,10 +18,9 @@ describe('StatusCodeFilter', () => {
1818
describe('wouldApply', () => {
1919
it.each([
2020
{},
21-
{ minStatusCodeToInclude: undefined },
22-
{ minStatusCodeToInclude: null },
23-
{ minStatusCodeToInclude: 'test' },
24-
{ minStatusCodeToInclude: NaN }
21+
{ excludeStatusCodes: undefined },
22+
{ excludeStatusCodes: null },
23+
{ excludeStatusCodes: [] }
2524
])(
2625
'should return false when the filter is not applicable (options: %j)',
2726
options => {
@@ -35,10 +34,8 @@ describe('StatusCodeFilter', () => {
3534
);
3635

3736
it.each([
38-
{ minStatusCodeToInclude: 1 },
39-
{ minStatusCodeToInclude: -1 },
40-
{ minStatusCodeToInclude: 1.1 },
41-
{ minStatusCodeToInclude: '1' }
37+
{ excludeStatusCodes: [200] },
38+
{ excludeStatusCodes: [200, 201] }
4239
])(
4340
'should return true when the filter is applicable (options: %j)',
4441
options => {
@@ -53,22 +50,21 @@ describe('StatusCodeFilter', () => {
5350
});
5451

5552
describe('apply', () => {
56-
it('should return true if the request status code equals or is greater than the threshold', () => {
53+
it('should return true if the request status code is not in exclusions', () => {
5754
// arrange
58-
const statusCode = 200;
59-
when(networkRequestMock.statusCode).thenReturn(statusCode);
60-
const options = { minStatusCodeToInclude: statusCode };
55+
when(networkRequestMock.statusCode).thenReturn(200);
56+
const options = { excludeStatusCodes: [400] };
6157
// act
6258
const result = sut.apply(instance(networkRequestMock), options);
6359
// assert
6460
expect(result).toBe(true);
6561
});
6662

67-
it('should return false if the request status code is less than the threshold', () => {
63+
it('should return false if the request status code is in exclusions', () => {
6864
// arrange
69-
const statusCode = 101;
65+
const statusCode = 200;
7066
when(networkRequestMock.statusCode).thenReturn(statusCode);
71-
const options = { minStatusCodeToInclude: 200 };
67+
const options = { excludeStatusCodes: [statusCode] };
7268
// act
7369
const result = sut.apply(instance(networkRequestMock), options);
7470
// assert

src/network/filters/StatusCodeFilter.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,12 @@ import { NetworkRequest } from '../NetworkRequest';
44
export class StatusCodeFilter implements RequestFilter {
55
public apply(
66
request: NetworkRequest,
7-
{ minStatusCodeToInclude = 0 }: RequestFilterOptions
7+
{ excludeStatusCodes }: RequestFilterOptions
88
): boolean {
9-
const threshold = this.normalizeThreshold(minStatusCodeToInclude);
10-
11-
return request.statusCode >= threshold;
9+
return !excludeStatusCodes?.includes(request.statusCode);
1210
}
1311

1412
public wouldApply(options: RequestFilterOptions): boolean {
15-
const threshold = this.normalizeThreshold(options.minStatusCodeToInclude);
16-
17-
return typeof threshold === 'number';
18-
}
19-
20-
private normalizeThreshold(value: unknown): number | undefined {
21-
return !isNaN(+value) && value !== null
22-
? Math.round(Math.abs(+value))
23-
: undefined;
13+
return options.excludeStatusCodes?.length > 0;
2414
}
2515
}

src/network/filters/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
export * from './BlobFilter';
2+
export * from './CompositeFilter';
13
export * from './HostFilter';
24
export * from './MimeFilter';
5+
export * from './MinStatusCodeFilter';
36
export * from './PathFilter';
47
export * from './RequestFilter';
5-
export * from './BlobFilter';
6-
export * from './CompositeFilter';
78
export * from './StatusCodeFilter';

0 commit comments

Comments
 (0)