From 1641ab13ae018364ee19e11294c811342b4d6cdf Mon Sep 17 00:00:00 2001 From: Conor Hawes Date: Sat, 4 Sep 2021 15:31:46 -0500 Subject: [PATCH 1/3] Add guard for 204 (don't just rely on content length) --- src/http/http.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/http/http.js b/src/http/http.js index 2b5c231..7317352 100644 --- a/src/http/http.js +++ b/src/http/http.js @@ -67,12 +67,12 @@ import { * getUsers() * .then(res => console.log('The users are', res)) * .catch(err => console.log('An error occurred!', err)) - * + * * // Shorthand: pass `url` as first argument: * function getUsers () { * return http('/users', options) * } - * + * */ // Enable shorthand with optional string `url` first argument. @@ -85,9 +85,9 @@ export function parseArguments (...args) { } // Get JSON from response -async function getResponseBody (response) { +async function getResponseBody(response) { // Don't parse empty body - if (response.headers.get('Content-Length') === '0') return null + if (response.headers.get('Content-Length') === '0' || response.status === 204) return null try { return await response.json() } catch (e) { @@ -98,13 +98,12 @@ async function getResponseBody (response) { } async function http (...args) { - - const { + const { before=noop, __mock_response, // used for unit testing ...options } = parseArguments(...args) - + const parsedOptions = await composeMiddleware( before, setDefaults, @@ -117,7 +116,7 @@ async function http (...args) { )(options) const { - onSuccess, + onSuccess, onFailure, camelizeResponse, successDataPath, From d41ce1fb796bbd139e1055352627e481feaf8172 Mon Sep 17 00:00:00 2001 From: Conor Hawes Date: Sat, 4 Sep 2021 15:32:10 -0500 Subject: [PATCH 2/3] Add specs --- __mocks__/isomorphic-fetch.js | 12 ++++++++++-- test/http/http.test.js | 18 ++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/__mocks__/isomorphic-fetch.js b/__mocks__/isomorphic-fetch.js index d480a36..b236e50 100644 --- a/__mocks__/isomorphic-fetch.js +++ b/__mocks__/isomorphic-fetch.js @@ -1,21 +1,29 @@ export const successUrl = '/success' +export const noContentUrl = '/no-content' export const failureUrl = '/failure' export const unauthorizedUrl = '/unauthorized' export const networkErrorUrl = '/network-error' const statuses = { [successUrl]: 200, + [noContentUrl]: 204, [failureUrl]: 400, [unauthorizedUrl]: 401 } export default jest.fn(function (url, options) { + const { headers={} } = options + const body = { ...options, url } + const response = { // Response echoes back passed options headers: { - get: () => {} + get: (header) => { + return headers[header] + }, + ...headers, }, - json: () => Promise.resolve({ ...options, url }), + json: () => Promise.resolve(body), ok: ![failureUrl, unauthorizedUrl].includes(url), status: statuses[url] } diff --git a/test/http/http.test.js b/test/http/http.test.js index e5ba8be..dacafb7 100644 --- a/test/http/http.test.js +++ b/test/http/http.test.js @@ -1,5 +1,5 @@ import Base64 from 'Base64' -import { successUrl, failureUrl } from 'isomorphic-fetch' +import { successUrl, noContentUrl, failureUrl } from 'isomorphic-fetch' import { http } from '../../src' // These tests rely on the mock Fetch() @@ -137,7 +137,7 @@ test('http onFailure hook is not triggered when an error is thrown in onSuccess' expect.assertions(1) const onSuccess = () => { throw new Error('Oops') } const onFailure = jest.fn() - return http(successUrl, { onFailure, onSuccess }).catch(e => { + return http(successUrl, { onFailure, onSuccess }).catch(() => { expect(onFailure).not.toHaveBeenCalled() }) }) @@ -289,6 +289,20 @@ test('http sets basic auth header if `auth` is present', () => { }) }) +test('http returns null when content-length is zero', () => { + return http(successUrl, { headers: { 'Content-Length': '0' }}) + .then((res) => { + expect(res).toBe(null) + }) +}) + +test('http returns null when the status is 204 (no content)', () => { + return http(noContentUrl) + .then((res) => { + expect(res).toBe(null) + }) +}) + /* MOCK STUFF */ // Mock token elements From fd6c433acbf30439e8672d4e0fa7ca8c4b71911e Mon Sep 17 00:00:00 2001 From: Conor Hawes Date: Sat, 4 Sep 2021 15:37:33 -0500 Subject: [PATCH 3/3] Bump patch --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index cbbbad4..53560a6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@launchpadlab/lp-requests", - "version": "4.1.8", + "version": "4.1.9", "description": "Request helpers", "main": "lib/index.js", "scripts": {