Skip to content

Commit

Permalink
throw exception, if url is not string or object
Browse files Browse the repository at this point in the history
  • Loading branch information
floatdrop committed Apr 26, 2015
1 parent 2029c30 commit 6b01569
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions index.js
Expand Up @@ -25,6 +25,10 @@ util.inherits(GotError, NestedErrorStacks);
GotError.prototype.name = 'GotError';

function got(url, opts, cb) {
if (typeof url !== 'string' && !(url instanceof Object)) {

This comment has been minimized.

Copy link
@sindresorhus

sindresorhus Apr 27, 2015

Owner

this will fail on objects created with Object.create(null) which I use a lot for baggage-less objects. Use typeof !== 'objec' instead

This comment has been minimized.

Copy link
@floatdrop

floatdrop Apr 27, 2015

Author Contributor

done 0797e46

throw new GotError('Parameter \'url\' must be a string or object, not ' + typeof url);

This comment has been minimized.

Copy link
@sindresorhus

sindresorhus Apr 27, 2015

Owner

Use backtick instead of ' for url:

`url`

This comment has been minimized.

Copy link
@floatdrop

floatdrop Apr 27, 2015

Author Contributor

done 0797e46

}

if (typeof opts === 'function') {
cb = opts;
opts = {};
Expand Down
15 changes: 15 additions & 0 deletions test/test-arguments.js
Expand Up @@ -18,6 +18,13 @@ tape('setup', function (t) {
});
});

tape('url argument is required', function (t) {
t.throws(function () {
got();
}, /Parameter 'url' must be a string or object, not undefined/);
t.end();
});

tape('accepts url.parse object as first argument', function (t) {
got({host: s.host, port: s.port, path: '/test'}, function (err, data) {
t.error(err);
Expand All @@ -34,6 +41,14 @@ tape('extends parsed string with opts', function (t) {
});
});

tape('extends parsed string with opts', function (t) {
got(s.url + '/?test=doge', {query: {test: 'wow'}}, function (err, data) {
t.error(err);
t.equal(data, '/?test=wow');
t.end();
});
});

tape('cleanup', function (t) {
s.close();
t.end();
Expand Down

0 comments on commit 6b01569

Please sign in to comment.