Skip to content

Commit

Permalink
Normalize options.method (#547)
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak authored and sindresorhus committed Aug 8, 2018
1 parent 0061266 commit ecf3180
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
3 changes: 2 additions & 1 deletion source/create.js
Expand Up @@ -37,7 +37,8 @@ const create = defaults => {
return defaults.handler(normalizeArguments(url, options, defaults), next);
};

for (const method of defaults.methods) {
const methods = defaults.methods.map(method => method.toLowerCase());
for (const method of methods) {
got[method] = (url, options) => got(url, {...options, method});
got.stream[method] = (url, options) => got.stream(url, {...options, method});
}
Expand Down
24 changes: 12 additions & 12 deletions source/index.js
Expand Up @@ -4,23 +4,23 @@ const create = require('./create');

const defaults = {
methods: [
'get',
'post',
'put',
'patch',
'head',
'delete'
'GET',
'POST',
'PUT',
'PATCH',
'HEAD',
'DELETE'
],
options: {
retry: {
retries: 2,
methods: [
'get',
'put',
'head',
'delete',
'options',
'trace'
'GET',
'PUT',
'HEAD',
'DELETE',
'OPTIONS',
'TRACE'
],
statusCodes: [
408,
Expand Down
6 changes: 4 additions & 2 deletions source/normalize-arguments.js
Expand Up @@ -82,7 +82,7 @@ module.exports = (url, options, defaults) => {

const {body} = options;
if (is.nullOrUndefined(body)) {
options.method = (options.method || 'GET').toUpperCase();
options.method = options.method || 'GET';
} else {
const isObject = is.object(body) && !Buffer.isBuffer(body) && !is.nodeStream(body);
if (!is.nodeStream(body) && !is.string(body) && !is.buffer(body) && !(options.form || options.json)) {
Expand Down Expand Up @@ -114,9 +114,11 @@ module.exports = (url, options, defaults) => {
options.body._buffer = body;
}

options.method = (options.method || 'POST').toUpperCase();
options.method = options.method || 'POST';
}

options.method = options.method.toUpperCase();

if (options.hostname === 'unix') {
const matches = /(.+?):(.+)/.exec(options.path);

Expand Down
18 changes: 18 additions & 0 deletions test/arguments.js
Expand Up @@ -47,6 +47,24 @@ test('options are optional', async t => {
t.is((await got(`${s.url}/test`)).body, '/test');
});

test('methods are normalized', async t => {
const instance = got.create({
methods: got.defaults.methods,
options: got.defaults.options,
handler: (options, next) => {
if (options.method === options.method.toUpperCase()) {
t.pass();
} else {
t.fail();
}

return next(options);
}
});

await instance(`${s.url}/test`, {method: 'post'});
});

test('accepts url.parse object as first argument', async t => {
t.is((await got({
hostname: s.host,
Expand Down

0 comments on commit ecf3180

Please sign in to comment.