Skip to content

Commit

Permalink
fix: handle empty responses to HEAD requests
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Apr 30, 2018
1 parent 522bf07 commit fc800a2
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/request/request.js
Expand Up @@ -39,8 +39,6 @@ function request (requestOptions) {
return fetch(requestOptions.url, pick(requestOptions, 'method', 'body', 'headers', 'timeout', 'agent'))

.then(response => {
const contentType = response.headers.get('content-type')

for (const keyAndValue of response.headers.entries()) {
headers[keyAndValue[0]] = keyAndValue[1]
}
Expand All @@ -49,6 +47,15 @@ function request (requestOptions) {
return
}

// GitHub API returns 200 for HEAD requsets
if (requestOptions.method === 'HEAD') {
if (response.status < 400) {
return
}

throw new HttpError(response.statusText, response.status, headers)
}

if (response.status === 304) {
requestOptions.url = response.headers.location
throw new HttpError('Not modified', response.status, headers)
Expand All @@ -62,6 +69,7 @@ function request (requestOptions) {
})
}

const contentType = response.headers.get('content-type')
if (/application\/json/.test(contentType)) {
return response.json()
}
Expand Down

0 comments on commit fc800a2

Please sign in to comment.