Skip to content

Commit

Permalink
fix(persister): Only treat status codes >= 400 as failed (#430)
Browse files Browse the repository at this point in the history
  • Loading branch information
offirgolan authored Nov 30, 2021
1 parent 06499fc commit 7658952
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ _Default_: `false`

If `false`, Polly will throw when attempting to persist any failed requests.
A request is considered to be a failed request when its response's status code
is `< 200` or `≥ 300`.
is `≥ 400`.

**Example**

Expand Down
2 changes: 1 addition & 1 deletion packages/@pollyjs/persister/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default class Persister {

this.assert(
`Cannot persist response for [${entry.request.method}] ${entry.request.url} because the status code was ${entry.response.status} and \`recordFailedRequests\` is \`false\``,
request.response.ok || request.config.recordFailedRequests
entry.response.status < 400 || request.config.recordFailedRequests
);

/*
Expand Down
18 changes: 16 additions & 2 deletions tests/integration/persister-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export default function persisterTests() {
this.polly.configure({ recordFailedRequests: false });

try {
await this.fetchRecord();
await this.relativeFetch('/echo?status=400');
await this.polly.stop();
} catch (e) {
error = e;
Expand All @@ -222,7 +222,21 @@ export default function persisterTests() {
it('should not error when persisting a failed request and `recordFailedRequests` is true', async function () {
this.polly.configure({ recordFailedRequests: true });

await this.fetchRecord();
await this.relativeFetch('/echo?status=400');
await this.polly.stop();

const har = await this.polly.persister.findRecording(
this.polly.recordingId
);

expect(har).to.be.an('object');
expect(har.log.entries).to.have.lengthOf(1);
});

it('should not error when persisting a 302 request and `recordFailedRequests` is false', async function () {
this.polly.configure({ recordFailedRequests: false });

await this.relativeFetch('/echo?status=302');
await this.polly.stop();

const har = await this.polly.persister.findRecording(
Expand Down

0 comments on commit 7658952

Please sign in to comment.