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

fix(core): Compute order based on id and recording name #342

Merged
merged 1 commit into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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