Skip to content

Commit

Permalink
feat: Make PollyRequest.respond accept a response object (#168)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Any adapters calling `pollyRequest.respond` should pass it a response object instead of the previous 3 arguments (statusCode, headers, body).
  • Loading branch information
offirgolan authored Jan 25, 2019
1 parent a08a8cf commit 5b07b26
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 38 deletions.
51 changes: 22 additions & 29 deletions packages/@pollyjs/adapter/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default class Adapter {

/* eslint-disable-next-line getter-return */
static get name() {
assert('Must override the static `name` getter.', false);
assert('Must override the static `name` getter.');
}

get defaultOptions() {
Expand Down Expand Up @@ -217,27 +217,34 @@ export default class Adapter {
);
}

/* Required Hooks */
onConnect() {
this.assert('Must implement the `onConnect` hook.', false);
this.assert('Must implement the `onConnect` hook.');
}

onDisconnect() {
this.assert('Must implement the `onDisconnect` hook.', false);
this.assert('Must implement the `onDisconnect` hook.');
}

/**
* @param {PollyRequest} pollyRequest
* @returns {response}
* @returns {{ statusCode: number, headers: Object, body: string }}
*/
async passthroughRequest(/* pollyRequest */) {
this.assert('Must implement `passthroughRequest`.', false);
this.assert('Must implement the `passthroughRequest` hook.');
}

/* Other Hooks */
/**
* Make sure the response from a Polly request is delivered to the
* user through the adapter interface.
*
* Calling `pollyjs.flush()` will await this method.
*
* @param {PollyRequest} pollyRequest
*/
async respondToRequest(/* pollyRequest */) {}

/**
* @param {PollyRequest} pollyRequest
* @returns {*}
*/
async onRecord(pollyRequest) {
await this.onPassthrough(pollyRequest);
Expand All @@ -248,31 +255,26 @@ export default class Adapter {
* @param {PollyRequest} pollyRequest
* @param {Object} normalizedResponse The normalized response generated from the recording entry
* @param {Object} recordingEntry The entire recording entry
* @returns {*}
*/
async onReplay(pollyRequest, { statusCode, headers, body }) {
await pollyRequest.respond(statusCode, headers, body);
async onReplay(pollyRequest, normalizedResponse) {
await pollyRequest.respond(normalizedResponse);
}

/**
* @param {PollyRequest} pollyRequest
* @param {PollyResponse} response
* @returns {*}
* @param {PollyResponse} pollyResponse
*/
async onIntercept(pollyRequest, { statusCode, headers, body }) {
await pollyRequest.respond(statusCode, headers, body);
async onIntercept(pollyRequest, pollyResponse) {
await pollyRequest.respond(pollyResponse);
}

/**
* @param {PollyRequest} pollyRequest
* @returns {*}
*/
async onPassthrough(pollyRequest) {
const { statusCode, headers, body } = await this.passthroughRequest(
pollyRequest
);
const response = await this.passthroughRequest(pollyRequest);

await pollyRequest.respond(statusCode, headers, body);
await pollyRequest.respond(response);
}

/**
Expand All @@ -294,7 +296,6 @@ export default class Adapter {

/**
* @param {PollyRequest} pollyRequest
* @param {*} result The returned result value from the request handler
*/
async onRequestFinished(pollyRequest) {
await this.respondToRequest(pollyRequest);
Expand All @@ -309,12 +310,4 @@ export default class Adapter {
onRequestFailed(pollyRequest, error) {
pollyRequest.promise.reject(error);
}

/**
* Make sure the response from a Polly request is delivered to the
* user through the adapter interface.
*
* Calling `pollyjs.flush()` will await this method.
*/
async respondToRequest(/* pollyRequest */) {}
}
13 changes: 9 additions & 4 deletions packages/@pollyjs/core/src/-private/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default class PollyRequest extends HTTPBase {
this.setHeaders(request.headers);
this.recordingName = polly.recordingName;
this.recordingId = polly.recordingId;
this.requestArguments = freeze(request.requestArguments || []);
this.requestArguments = freeze(request.requestArguments);
this.promise = defer();
this[POLLY] = polly;
this[EVENT_EMITTER] = new EventEmitter({ eventNames: SUPPORTED_EVENTS });
Expand Down Expand Up @@ -163,7 +163,9 @@ export default class PollyRequest extends HTTPBase {
this.timestamp = timestamp();
}

async respond(status, headers, body) {
async respond(response) {
const { statusCode, headers, body } = response || {};

assert(
'Cannot respond to a request that already has a response.',
!this.didRespond
Expand All @@ -172,8 +174,11 @@ export default class PollyRequest extends HTTPBase {
// Timestamp the response
this.response.timestamp = timestamp();

// Set the status code and headers
this.response.status(status).setHeaders(headers);
// Set the status code
this.response.status(statusCode);

// Se the headers
this.response.setHeaders(headers);

// Set the body without modifying any headers (instead of using .send())
this.response.body = body;
Expand Down
8 changes: 4 additions & 4 deletions packages/@pollyjs/persister/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default class Persister {

/* eslint-disable-next-line getter-return */
static get name() {
assert('Must override the static `name` getter.', false);
assert('Must override the static `name` getter.');
}

get defaultOptions() {
Expand Down Expand Up @@ -202,14 +202,14 @@ export default class Persister {
}

findRecording() {
this.assert('Must implement the `findRecording` hook.', false);
this.assert('Must implement the `findRecording` hook.');
}

saveRecording() {
this.assert('Must implement the `saveRecording` hook.', false);
this.assert('Must implement the `saveRecording` hook.');
}

deleteRecording() {
this.assert('Must implement the `deleteRecording` hook.', false);
this.assert('Must implement the `deleteRecording` hook.');
}
}
2 changes: 1 addition & 1 deletion packages/@pollyjs/utils/src/utils/assert.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default function(msg, condition) {
if (typeof condition === 'undefined' || condition === false) {
if (!condition) {
throw new Error(`[Polly] ${msg}`);
}
}

0 comments on commit 5b07b26

Please sign in to comment.