From 1f895d55be33811a37ae583593d6b70be33cf1ee Mon Sep 17 00:00:00 2001 From: Mordy Tikotzky Date: Sun, 15 Feb 2015 10:19:33 -0500 Subject: [PATCH 1/2] failing test for setting head with a body --- tests/test-errors.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) 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() +}) From d6c911df9310e2115c21445f90058f1003cd55f7 Mon Sep 17 00:00:00 2001 From: Mordy Tikotzky Date: Sun, 15 Feb 2015 10:21:49 -0500 Subject: [PATCH 2/2] throw error if method is head and sending a body --- index.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) 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) }