Skip to content

Commit

Permalink
Merge pull request #1429 from tikotzky/throw-error-when-head-with-body
Browse files Browse the repository at this point in the history
Throw error when making HEAD request with a body
  • Loading branch information
nylen committed Feb 16, 2015
2 parents 5764559 + d6c911d commit b8de4a2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
9 changes: 4 additions & 5 deletions index.js
Expand Up @@ -47,6 +47,10 @@ function request (uri, options, callback) {
options.callback = params.callback
options.uri = params.uri

if (params.options.method === 'HEAD' && paramsHaveRequestBody(params)) {
throw new Error('HTTP HEAD requests MUST NOT include a request body.')
}

return new request.Request(options)
}

Expand All @@ -66,11 +70,6 @@ request.get = function (uri, options, callback) {
request.head = function (uri, options, callback) {
var params = initParams(uri, options, callback)
params.options.method = 'HEAD'

if (paramsHaveRequestBody(params)) {
throw new Error('HTTP HEAD requests MUST NOT include a request body.')
}

return requester(params)(params.uri || null, params.options, params.callback)
}

Expand Down
19 changes: 19 additions & 0 deletions tests/test-errors.js
Expand Up @@ -77,3 +77,22 @@ tape('multipart without body 2', function(t) {
}, /^Error: Body attribute missing in multipart\.$/)
t.end()
})

tape('head method with a body', function(t) {
t.throws(function() {
request(local, {
method: 'HEAD',
body: 'foo'
})
}, /HTTP HEAD requests MUST NOT include a request body/)
t.end()
})

tape('head method with a body 2', function(t) {
t.throws(function() {
request.head(local, {
body: 'foo'
})
}, /HTTP HEAD requests MUST NOT include a request body/)
t.end()
})

0 comments on commit b8de4a2

Please sign in to comment.