Skip to content

Commit

Permalink
feat(persister): Add keepUnusedRequests config option (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
offirgolan authored Sep 14, 2018
1 parent 08a9ded commit 3f5f5b2
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 7 deletions.
21 changes: 21 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,27 @@ polly.configure({
});
```

### keepUnusedRequests

_Type_: `Boolean`
_Default_: `false`

When disabled, requests that have not been captured by the running Polly
instance will be removed from any previous recording. This ensures that a
recording will only contain the requests that were made during the lifespan
of the Polly instance. When enabled, new requests will be appended to the
recording file.

__Example__

```js
polly.configure({
persisterOptions: {
keepUnusedRequests: true
}
});
```

## timing

_Type_: `Function`
Expand Down
4 changes: 3 additions & 1 deletion packages/@pollyjs/core/src/defaults/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ export default {
adapterOptions: {},

persister: null,
persisterOptions: {},
persisterOptions: {
keepUnusedRequests: false
},

logging: false,

Expand Down
27 changes: 25 additions & 2 deletions packages/@pollyjs/persister/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import HAR from './har';
import Entry from './har/entry';
import stringify from 'fast-json-stable-stringify';
import { assert } from '@pollyjs/utils';
import { ACTIONS, assert } from '@pollyjs/utils';

const CREATOR_NAME = 'Polly.JS';

Expand Down Expand Up @@ -84,11 +84,15 @@ export default class Persister {
modify the payload (i.e. encrypting the request & response).
*/
await request._emit('beforePersist', entry);

entries.push(entry);
}

har.log.addEntries(entries);

if (!this.polly.config.persisterOptions.keepUnusedRequests) {
this._removeUnusedEntries(recordingId, har);
}

promises.push(this.save(recordingId, har));
}

Expand Down Expand Up @@ -176,6 +180,25 @@ export default class Persister {
);
}

/**
* Remove all entries from the given HAR that do not match any requests in
* the current Polly instance.
*
* @param {String} recordingId
* @param {HAR} har
*/
_removeUnusedEntries(recordingId, har) {
const requests = this.polly._requests.filter(
r =>
r.recordingId === recordingId &&
(r.action === ACTIONS.RECORD || r.action === ACTIONS.REPLAY)
);

har.log.entries = har.log.entries.filter(entry =>
requests.find(r => entry._id === r.id && entry._order === r.order)
);
}

findRecording() {
this.assert('Must implement the `findRecording` hook.', false);
}
Expand Down
42 changes: 38 additions & 4 deletions tests/integration/persister-tests.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Polly } from '@pollyjs/core';
import * as validate from 'har-validator/lib/async';

const { keys } = Object;

export default function persisterTests() {
it('should persist valid HAR', async function() {
const { recordingId, persister } = this.polly;
Expand Down Expand Up @@ -162,7 +160,43 @@ export default function persisterTests() {

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

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

it('should remove unused entries when `keepUnusedRequests` is false', async function() {
const { recordingName, recordingId, config } = this.polly;

const orderedRecordUrl = order => `${this.recordUrl()}?order=${order}`;

await this.fetch(orderedRecordUrl(1));
await this.fetch(orderedRecordUrl(2));
await this.polly.persister.persist();

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

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

await this.polly.stop();

this.polly = new Polly(recordingName, config);
this.polly.replay();
this.polly.configure({
persisterOptions: {
keepUnusedRequests: false
}
});

await this.fetch(orderedRecordUrl(1)); // -> Replay
await this.fetch(orderedRecordUrl(3)); // -> New recording
await this.polly.persister.persist();

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

expect(har).to.be.an('object');
expect(har.log.entries).to.have.lengthOf(2);
expect(har.log.entries[0].request.url).to.include(orderedRecordUrl(1));
expect(har.log.entries[1].request.url).to.include(orderedRecordUrl(3));
});
}

0 comments on commit 3f5f5b2

Please sign in to comment.