diff --git a/index.js b/index.js index 3581b83b4..28c6cf9b6 100755 --- a/index.js +++ b/index.js @@ -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) } @@ -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) } diff --git a/tests/test-errors.js b/tests/test-errors.js index fde97fefe..bb5ab7ccc 100644 --- a/tests/test-errors.js +++ b/tests/test-errors.js @@ -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() +})