Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw error when making HEAD request with a body #1429

Merged
merged 2 commits into from Feb 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()
})