From 242c5c929cc4c348b89c21ceb66a6adc5117b007 Mon Sep 17 00:00:00 2001 From: Luca Maraschi Date: Sun, 4 Dec 2016 09:32:51 +0100 Subject: [PATCH 1/5] http: verify method is a string --- lib/_http_client.js | 5 ++ .../test-http-client-check-http-token.js | 48 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 test/parallel/test-http-client-check-http-token.js diff --git a/lib/_http_client.js b/lib/_http_client.js index 6837c94df98eea..6f6451d223e668 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -68,6 +68,11 @@ function ClientRequest(options, cb) { self.socketPath = options.socketPath; self.timeout = options.timeout; + const _method = options.method; + if (_method !== undefined && _method !== null + && typeof _method !== 'string') { + throw new TypeError('Method must be a string'); + } var method = self.method = (options.method || 'GET').toUpperCase(); if (!common._checkIsHttpToken(method)) { throw new TypeError('Method must be a valid HTTP token'); diff --git a/test/parallel/test-http-client-check-http-token.js b/test/parallel/test-http-client-check-http-token.js new file mode 100644 index 00000000000000..1e908374abc995 --- /dev/null +++ b/test/parallel/test-http-client-check-http-token.js @@ -0,0 +1,48 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const http = require('http'); + +const expectedSuccesses = { + undefined: 'GET', + null: 'GET', + 'get': 'GET', + 'post': 'POST' +}; + +const methods = Object.keys(expectedSuccesses); +let requestCount = 0; + +const server = http.createServer((req, res) => { + requestCount++; + res.end(); + + if (methods.length === requestCount) { + server.close(); + } +}).listen(0, test); + +function test() { + function fail(input) { + assert.throws(() => { + http.request({ method: input, path: '/' }, common.fail); + }, /Method must be a string/); + } + + fail(-1); + fail(1); + fail(0); + fail({}); + fail(true); + fail(false); + fail([]); + + function ok(method) { + http.request({ method: method, port: server.address().port }).end(); + } + + ok(undefined); + ok(null); + ok('get'); + ok('post'); +} From c711ac7a1593fe2395b940448666431c028957ec Mon Sep 17 00:00:00 2001 From: Luca Maraschi Date: Tue, 6 Dec 2016 08:28:06 +0100 Subject: [PATCH 2/5] http: added @cjihrig feedbacks --- lib/_http_client.js | 3 +-- .../test-http-client-check-http-token.js | 20 ++++++------------- 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/lib/_http_client.js b/lib/_http_client.js index 6f6451d223e668..08a9f2dfd08a8c 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -69,8 +69,7 @@ function ClientRequest(options, cb) { self.timeout = options.timeout; const _method = options.method; - if (_method !== undefined && _method !== null - && typeof _method !== 'string') { + if (_method != null && typeof _method !== 'string') { throw new TypeError('Method must be a string'); } var method = self.method = (options.method || 'GET').toUpperCase(); diff --git a/test/parallel/test-http-client-check-http-token.js b/test/parallel/test-http-client-check-http-token.js index 1e908374abc995..5a2b84a973262c 100644 --- a/test/parallel/test-http-client-check-http-token.js +++ b/test/parallel/test-http-client-check-http-token.js @@ -3,21 +3,14 @@ const common = require('../common'); const assert = require('assert'); const http = require('http'); -const expectedSuccesses = { - undefined: 'GET', - null: 'GET', - 'get': 'GET', - 'post': 'POST' -}; - -const methods = Object.keys(expectedSuccesses); +const expectedSuccesses = [undefined, null, 'GET', 'post']; let requestCount = 0; const server = http.createServer((req, res) => { requestCount++; res.end(); - if (methods.length === requestCount) { + if (expectedSuccesses.length === requestCount) { server.close(); } }).listen(0, test); @@ -26,7 +19,7 @@ function test() { function fail(input) { assert.throws(() => { http.request({ method: input, path: '/' }, common.fail); - }, /Method must be a string/); + }, /^TypeError: Method must be a string$/); } fail(-1); @@ -41,8 +34,7 @@ function test() { http.request({ method: method, port: server.address().port }).end(); } - ok(undefined); - ok(null); - ok('get'); - ok('post'); + expectedSuccesses.forEach((method) => { + ok(method); + }); } From 29f78c14c425779149211ba47fa11baa70dfb3d5 Mon Sep 17 00:00:00 2001 From: Luca Maraschi Date: Wed, 7 Dec 2016 08:22:34 +0100 Subject: [PATCH 3/5] http: renamed method for consistency --- lib/_http_client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/_http_client.js b/lib/_http_client.js index 08a9f2dfd08a8c..9242736d6cf6d2 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -72,7 +72,7 @@ function ClientRequest(options, cb) { if (_method != null && typeof _method !== 'string') { throw new TypeError('Method must be a string'); } - var method = self.method = (options.method || 'GET').toUpperCase(); + var method = self.method = (_method || 'GET').toUpperCase(); if (!common._checkIsHttpToken(method)) { throw new TypeError('Method must be a valid HTTP token'); } From 7774c3a1e37bc8d9e36fa1810abb2283ecafb315 Mon Sep 17 00:00:00 2001 From: Luca Maraschi Date: Wed, 7 Dec 2016 16:53:30 +0100 Subject: [PATCH 4/5] http: variables rearrangement --- lib/_http_client.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/_http_client.js b/lib/_http_client.js index 9242736d6cf6d2..755d94ad8bdc58 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -68,11 +68,11 @@ function ClientRequest(options, cb) { self.socketPath = options.socketPath; self.timeout = options.timeout; - const _method = options.method; - if (_method != null && typeof _method !== 'string') { + let method = options.method; + if (method != null && typeof method !== 'string') { throw new TypeError('Method must be a string'); } - var method = self.method = (_method || 'GET').toUpperCase(); + method = self.method = (method || 'GET').toUpperCase(); if (!common._checkIsHttpToken(method)) { throw new TypeError('Method must be a valid HTTP token'); } From 9561854ef1404cb498901b004586e3d585c9ee2c Mon Sep 17 00:00:00 2001 From: Luca Maraschi Date: Wed, 7 Dec 2016 16:58:01 +0100 Subject: [PATCH 5/5] http: changed let to var --- lib/_http_client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/_http_client.js b/lib/_http_client.js index 755d94ad8bdc58..85e865f565a039 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -68,7 +68,7 @@ function ClientRequest(options, cb) { self.socketPath = options.socketPath; self.timeout = options.timeout; - let method = options.method; + var method = options.method; if (method != null && typeof method !== 'string') { throw new TypeError('Method must be a string'); }