Skip to content

Commit

Permalink
fix(adapter): Clone the recording entry before mutating it (#294)
Browse files Browse the repository at this point in the history
Clone the recording entry before the `beforeReplay` event so any changes will not actually persist to the stored recording.
  • Loading branch information
offirgolan authored Jan 13, 2020
1 parent 5fe991d commit d7e1303
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
19 changes: 14 additions & 5 deletions packages/@pollyjs/adapter/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,18 @@ export default class Adapter {
const recordingEntry = await this.persister.findEntry(pollyRequest);

if (recordingEntry) {
await pollyRequest._emit('beforeReplay', recordingEntry);
/*
Clone the recording entry so any changes will not actually persist to
the stored recording.
if (isExpired(recordingEntry.startedDateTime, config.expiresIn)) {
Note: Using JSON.parse/stringify instead of lodash/cloneDeep since
the recording entry is stored as json.
*/
const clonedRecordingEntry = JSON.parse(JSON.stringify(recordingEntry));

await pollyRequest._emit('beforeReplay', clonedRecordingEntry);

if (isExpired(clonedRecordingEntry.startedDateTime, config.expiresIn)) {
const message =
'Recording for the following request has expired.\n' +
`${stringifyRequest(pollyRequest, null, 2)}`;
Expand All @@ -184,13 +193,13 @@ export default class Adapter {
}
}

await this.timeout(pollyRequest, recordingEntry);
await this.timeout(pollyRequest, clonedRecordingEntry);
pollyRequest.action = ACTIONS.REPLAY;

return this.onReplay(
pollyRequest,
normalizeRecordedResponse(recordingEntry.response),
recordingEntry
normalizeRecordedResponse(clonedRecordingEntry.response),
clonedRecordingEntry
);
}

Expand Down
42 changes: 42 additions & 0 deletions tests/integration/adapter-tests.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Polly } from '@pollyjs/core';
import { ACTIONS } from '@pollyjs/utils';

export default function adapterTests() {
Expand Down Expand Up @@ -126,6 +127,47 @@ export default function adapterTests() {
]);
});

it('should call beforeReplay with a cloned recording entry', async function() {
const { recordingId, recordingName, config } = this.polly;
let replayedEntry;

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

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

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

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

this.polly.server
.get(this.recordUrl())
.on('beforeReplay', (_req, entry) => (replayedEntry = entry));

const entry = har.log.entries[0];

await this.fetchRecord();

expect(replayedEntry).to.be.an('object');

expect(entry).to.deep.equal(replayedEntry);
expect(entry).to.not.equal(replayedEntry);

expect(entry.request).to.deep.equal(replayedEntry.request);
expect(entry.request).to.not.equal(replayedEntry.request);

expect(entry.response).to.deep.equal(replayedEntry.response);
expect(entry.response).to.not.equal(replayedEntry.response);

expect(entry.response.content).to.deep.equal(
replayedEntry.response.content
);
expect(entry.response.content).to.not.equal(replayedEntry.response.content);
});

it('should emit an error event', async function() {
const { server } = this.polly;
let error;
Expand Down

0 comments on commit d7e1303

Please sign in to comment.