Skip to content

Commit

Permalink
more test for better coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
andrasq committed Mar 16, 2021
1 parent 415a60f commit d8d2374
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
8 changes: 3 additions & 5 deletions index.js
Expand Up @@ -88,13 +88,11 @@ function microreq( uri, body, callback ) {

function defaults( options ) {
if (!options || typeof options === 'string') options = { url: options || '' };
var opts = mergeOpts({}, options);
if (opts.baseUrl) opts.baseUrl = rtrim(opts.baseUrl, '/');
var caller = {
_opts: opts,
_opts: mergeOpts({}, options),
call: function(method, uri, body, cb) {
if (!uri || typeof uri === 'string') uri = { url: uri || '' };
var url = buildUrl(rtrim(uri.baseUrl, '/') || caller._opts.baseUrl, uri.url);
if (!uri || typeof uri === 'string') uri = { url: '' + uri };
var url = buildUrl(rtrim(uri.baseUrl || caller._opts.baseUrl || '', '/'), uri.url || caller._opts.url);
return module.exports.request(mergeOpts({}, caller._opts, uri, { method: method, url: url }), body, cb);
},
get: function get(url, body, cb) { return caller.call('GET', url, body, cb) },
Expand Down
12 changes: 10 additions & 2 deletions test.js
Expand Up @@ -526,15 +526,23 @@ module.exports = {
t.ok(spy.called);
t.contains(spy.args[0][0], {
method: 'FOO', // retains method
baseUrl: 'some-base-url', // inherited option, and strips trailing '/' from baseUrl
someOption: 'TRUE-234', // default option
otherOption: '567', // user option
url: 'some-base-url/some-path', // built url
url: 'some-base-url/some-path', // built url using inherited option
headers: { Accept: 'foo/bar' }, // merged headers
});
t.done();
})
},
'caller uses default url': function(t) {
var caller = request.defaults({ url: '/url' }).defaults({ baseUrl: '/some' });
var spy = t.stubOnce(request, 'request').yields(null, {});
caller.call('GET', '', 'mock body', function(err, res, body) {
t.ok(spy.called);
t.contains(spy.args[0][0], { url: '/some/url' });
t.done();
})
},
'caller invokes microreq.request': function(t) {
var caller = request.defaults();
var spy = t.stubOnce(request, 'request').yields(null, { body: 'mock response' });
Expand Down

0 comments on commit d8d2374

Please sign in to comment.