Skip to content

Commit

Permalink
fix(core): Compute order based on id and recording name (#342)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: A request's order is will now be computed based on its id and the recording name it will be persisted to.
  • Loading branch information
offirgolan authored Jun 5, 2020
1 parent d210ac0 commit 42004d2
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 17 deletions.
5 changes: 3 additions & 2 deletions packages/@pollyjs/core/src/-private/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,13 +265,14 @@ export default class PollyRequest extends HTTPBase {
// Guid is a string representation of the identifiers
this.id = md5(stringify(this.identifiers));

// Order is calculated on other requests with the same id
// Order is calculated on other requests with the same id and recording id
// Only requests before this current one are taken into account.
this.order =
matchRequestsBy.order && !this.shouldPassthrough && !this.shouldIntercept
? requests
.slice(0, requests.indexOf(this))
.filter(r => r.id === this.id).length
.filter(r => r.id === this.id && r.recordingId === this.recordingId)
.length
: 0;
}
}
70 changes: 55 additions & 15 deletions tests/integration/adapter-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,68 @@ import { ACTIONS } from '@pollyjs/utils';

export default function adapterTests() {
it('should respect request order', async function() {
let res = await this.fetchRecord();
const testOrder = async () => {
let res = await this.fetchRecord();

expect(res.status).to.equal(404);
expect(res.status).to.equal(404);

res = await this.fetchRecord({
method: 'POST',
body: JSON.stringify({ foo: 'bar' }),
headers: { 'Content-Type': 'application/json' }
});
res = await this.fetchRecord({
method: 'POST',
body: JSON.stringify({ foo: 'bar' }),
headers: { 'Content-Type': 'application/json' }
});

expect(res.status).to.equal(200);
expect(res.status).to.equal(200);

res = await this.fetchRecord();
const json = await res.json();
res = await this.fetchRecord();
const json = await res.json();

expect(json).to.deep.equal({ foo: 'bar' });
expect(json).to.deep.equal({ foo: 'bar' });

res = await this.fetchRecord({ method: 'DELETE' });
expect(res.status).to.equal(200);
res = await this.fetchRecord({ method: 'DELETE' });
expect(res.status).to.equal(200);

res = await this.fetchRecord();
expect(res.status).to.equal(404);
};

this.polly.configure({ recordIfMissing: false });

const { recordingName, config } = this.polly;

this.polly.record();
await testOrder();
await this.polly.stop();

this.polly = new Polly(recordingName, config);
this.polly.replay();
await testOrder();
});

it('should respect request order across multiple recordings', async function() {
const recordingName = this.polly.recordingName;
const otherRecordingName = `${this.polly.recordingName}-other`;
const order = {
[recordingName]: [],
[otherRecordingName]: []
};

this.polly.server.any(this.recordUrl()).on('beforeResponse', req => {
order[req.recordingName].push(req.order);
});

await this.fetchRecord();
await this.fetchRecord();

this.polly.server.any(this.recordUrl()).recordingName(otherRecordingName);
await this.fetchRecord();
await this.fetchRecord();

this.polly.server.any(this.recordUrl()).recordingName();
await this.fetchRecord();

res = await this.fetchRecord();
expect(res.status).to.equal(404);
expect(order[recordingName]).to.have.ordered.members([0, 1, 2]);
expect(order[otherRecordingName]).to.have.ordered.members([0, 1]);
});

it('should properly handle 204 status code response', async function() {
Expand Down

0 comments on commit 42004d2

Please sign in to comment.