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

feat: Use status code 204 in place of 404. #5

Merged
merged 2 commits into from
Jun 8, 2018
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
4 changes: 3 additions & 1 deletion packages/@pollyjs/core/src/persisters/rest/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,7 @@ function handleResponse(xhr, resolve, reject) {
}
}

return xhr.status >= 200 && xhr.status < 300 ? resolve(body) : reject(xhr);
return xhr.status >= 200 && xhr.status < 300
? resolve({ body, xhr })
: reject(xhr);
}
44 changes: 25 additions & 19 deletions packages/@pollyjs/core/src/persisters/rest/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ajax from './ajax';
import Persister from '../persister';
import buildUrl from '../../utils/build-url';
import stringify from 'json-stable-stringify';
import buildUrl from '../../utils/build-url';
import Persister from '../persister';
import ajax from './ajax';

export default class RestPersister extends Persister {
ajax(url, ...args) {
Expand All @@ -10,33 +10,25 @@ export default class RestPersister extends Persister {
return ajax(buildUrl(host, apiNamespace, url), ...args);
}

findRecordingEntry(pollyRequest) {
async findRecordingEntry(pollyRequest) {
const { id, order, recordingId } = pollyRequest;

return this.ajax(
const response = await this.ajax(
`/${encodeURIComponent(recordingId)}/${id}?order=${order}`,
{
Accept: 'application/json; charset=utf-8'
}
).catch(e => {
if (e && e.status === 404) {
return null;
}
);

throw e;
});
return this._normalize(response);
}

findRecording(recordingId) {
return this.ajax(`/${encodeURIComponent(recordingId)}`, {
async findRecording(recordingId) {
const response = await this.ajax(`/${encodeURIComponent(recordingId)}`, {
Accept: 'application/json; charset=utf-8'
}).catch(e => {
if (e && e.status === 404) {
return null;
}

throw e;
});

return this._normalize(response);
}

async saveRecording(recordingId, data) {
Expand All @@ -55,4 +47,18 @@ export default class RestPersister extends Persister {
method: 'DELETE'
});
}

_normalize({ xhr, body }) {
/**
* 204 - No Content. Polly uses this status code in place of 404
* when interacting with our Rest server to prevent throwing
* request errors in consumer's stdout (console.log)
*/
if (xhr.status === 204) {
/* return null when a record was not found */
return null;
}

return body;
}
}
8 changes: 4 additions & 4 deletions packages/@pollyjs/node-server/src/api.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path';
import fs from 'fs-extra';
import path from 'path';

export default class API {
constructor(recordingsDir) {
Expand All @@ -18,7 +18,7 @@ export default class API {
}
}

return this.respond(404);
return this.respond(204);
}

getRecording(recording) {
Expand All @@ -28,7 +28,7 @@ export default class API {
return this.respond(200, fs.readJsonSync(recordingFilename));
}

return this.respond(404);
return this.respond(204);
}

saveRecording(recording, data) {
Expand All @@ -46,7 +46,7 @@ export default class API {
fs.removeSync(recordingFilename);
}

return this.respond(204);
return this.respond(200);
}

filenameFor(recording) {
Expand Down