Browse Source

add body option validation

Throw nice error about body option,
instead of `first argument must be a string or Buffer`
from http.
http2
Vsevolod Strukchinsky 10 years ago
parent
commit
a0913bc13c
  1. 4
      index.js
  2. 8
      test/test-error.js

4
index.js

@ -67,6 +67,10 @@ function got(url, opts, cb) {
throw new GotError('got can not be used as stream when options.json is used');
}
if (body && !(typeof body === 'string' || body instanceof Buffer || isStream.readable(body))) {
throw new GotError('options.body must be a ReadableStream, string or Buffer');
}
function get(url, opts, cb) {
var parsedUrl = urlLib.parse(prependHttp(url));
var fn = parsedUrl.protocol === 'https:' ? https : http;

8
test/test-error.js

@ -33,6 +33,14 @@ tape('dns error message', function (t) {
});
});
tape('options.body error message', function (t) {
t.throws(function () {
got(s.url, {body: {}});
}, /options.body must be a ReadableStream, string or Buffer/);
t.end();
});
tape('cleanup', function (t) {
s.close();
t.end();

Loading…
Cancel
Save