Skip to content

Commit

Permalink
Handle no content (#95)
Browse files Browse the repository at this point in the history
* Add guard for 204 (don't just rely on content length)

* Add specs

* Bump patch
  • Loading branch information
chawes13 committed Sep 16, 2021
1 parent 3aa51d7 commit b54340d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
12 changes: 10 additions & 2 deletions __mocks__/isomorphic-fetch.js
Original file line number Diff line number Diff line change
@@ -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]
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@launchpadlab/lp-requests",
"version": "4.1.8",
"version": "4.1.9",
"description": "Request helpers",
"main": "lib/index.js",
"scripts": {
Expand Down
15 changes: 7 additions & 8 deletions src/http/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) {
Expand All @@ -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,
Expand All @@ -117,7 +116,7 @@ async function http (...args) {
)(options)

const {
onSuccess,
onSuccess,
onFailure,
camelizeResponse,
successDataPath,
Expand Down
18 changes: 16 additions & 2 deletions test/http/http.test.js
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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()
})
})
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b54340d

Please sign in to comment.