fix: explicity reject non-200 status codes (#342)

This commit is contained in:
Toufic Mouallem 2019-03-30 00:50:55 +02:00 committed by Adam Pash
parent b6c82f2b16
commit 262dda94b3
4 changed files with 10 additions and 10 deletions

View File

@ -32,12 +32,12 @@ describe('Mercury', () => {
assert.equal(result.content.indexOf('score="') === -1, true);
});
it('returns an error on non-2xx responses', async () => {
it('returns an error on non-200 responses', async () => {
const error = await Mercury.parse(
'https://www.thekitchn.com/instant-pot-chicken-pesto-pasta-eating-instantly-267141'
);
assert(/instructed to reject non-2xx/i.test(error.message));
assert(/instructed to reject non-200/i.test(error.message));
});
it('returns an error on invalid content types', async () => {

View File

@ -22,7 +22,7 @@ describe('Resource', () => {
const url = 'http://nytimes.com/500';
const error = await Resource.create(url);
assert(/instructed to reject non-2xx/i.test(error.message));
assert(/instructed to reject non-200/i.test(error.message));
});
it('fetches with different encoding on body', async () => {

View File

@ -21,11 +21,11 @@ function get(options) {
}
// Evaluate a response to ensure it's something we should be keeping.
// This does not validate in the sense of a response being 200 level or
// not. Validation here means that we haven't found reason to bail from
// This does not validate in the sense of a response being 200 or not.
// Validation here means that we haven't found reason to bail from
// further processing of this url.
export function validateResponse(response, parseNon2xx = false) {
export function validateResponse(response, parseNon200 = false) {
// Check if we got a valid status code
// This isn't great, but I'm requiring a statusMessage to be set
// before short circuiting b/c nock doesn't set it in tests
@ -40,11 +40,11 @@ export function validateResponse(response, parseNon2xx = false) {
throw new Error(
`Unable to fetch content. Original exception was ${response.error}`
);
} else if (!parseNon2xx) {
} else if (!parseNon200) {
throw new Error(
`Resource returned a response status code of ${
response.statusCode
} and resource was instructed to reject non-2xx level status codes.`
} and resource was instructed to reject non-200 status codes.`
);
}
}

View File

@ -95,14 +95,14 @@ describe('validateResponse(response)', () => {
}, /unable to fetch content/i);
});
it('throws an error if response code is not 2xx', () => {
it('throws an error if response code is not 200', () => {
const invalidResponse = {
statusCode: 500,
};
assert.throws(() => {
validateResponse(invalidResponse);
}, /instructed to reject non-2xx/i);
}, /instructed to reject non-200/i);
});
it('throws an error if response has bad content-type', () => {