-
Notifications
You must be signed in to change notification settings - Fork 30.2k
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
http: verify method is a string #10111
Changes from 1 commit
242c5c9
c711ac7
29f78c1
7774c3a
9561854
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be written as: if (_method != null && typeof _method !== 'string') If you don't choose to rewrite like that, the |
||
&& typeof _method !== 'string') { | ||
throw new TypeError('Method must be a string'); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the best backwards compatibility (and to emulate the method value selection behavior below the if (options.method && typeof options.method !== 'string') {
throw new TypeError('Method must be a string');
} The reason being that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mscdex that would not fix the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, it's a difference between semver-ness. This change is definitely going to be semver-major as-is, but it could be semver-patch with the check I suggested. It's up to you. |
||
var method = self.method = (options.method || 'GET').toUpperCase(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
if (!common._checkIsHttpToken(method)) { | ||
throw new TypeError('Method must be a valid HTTP token'); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const http = require('http'); | ||
|
||
const expectedSuccesses = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only the keys seem to be used. This could probably be an array... |
||
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/); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you want to be really strict, this could be |
||
} | ||
|
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ... and then you can loop over the array here. Right now, the number of times |
||
ok(null); | ||
ok('get'); | ||
ok('post'); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can just move the
var method
declaration up here and re-assign on line 75, that way we don't have a second variable for the same thing?