Skip to content

Commit

Permalink
Merge pull request #822 from KingHenne/fix/isomorphic-fetch-basic-auth
Browse files Browse the repository at this point in the history
Fix isomorphic fetch usage with basicAuth/matchHeader
  • Loading branch information
ianwsperber committed Feb 10, 2017
2 parents 2318034 + 933cbad commit 86babe8
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/common.js
Expand Up @@ -311,7 +311,7 @@ function matchStringOrRegexp(target, pattern) {
if (pattern instanceof RegExp) {
return target.toString().match(pattern);
} else {
return target === pattern;
return target.toString() === pattern;
}
}

Expand Down
55 changes: 54 additions & 1 deletion tests/test_isomorphic_fetch.js
Expand Up @@ -32,7 +32,7 @@ test("string-based reqheaders match works", function(t) {
get('/path2').
reply(200, 'somemoardata');

fetch('http://isomorphicfetchland.com/path2', {
return fetch('http://isomorphicfetchland.com/path2', {
headers: {
'header': 'header value',
}
Expand All @@ -49,3 +49,56 @@ test("string-based reqheaders match works", function(t) {
throw err;
});
});

test("basicAuth match works", function (t) {
var scope = nock('http://isomorphicfetchland.com').
get('/path2').
basicAuth({
user: 'username',
pass: 'password'
}).
reply(200, 'somemoardata');

return fetch('http://isomorphicfetchland.com/path2', {
headers: {
'Authorization': 'Basic ' + new Buffer('username:password').toString('base64'),
}
}).
then(function (res) {
return res.text();
}).
then(function (text) {
scope.done();
t.equal(text, 'somemoardata', "response should match");
t.end();
}).
catch(function (err) {
throw err;
});
});

test("matchHeader works", function (t) {
var authorizationHeader = 'Basic ' + new Buffer('username:password').toString('base64');

var scope = nock('http://isomorphicfetchland.com').
get('/path2').
matchHeader('authorization', authorizationHeader).
reply(200, 'somemoardata');

return fetch('http://isomorphicfetchland.com/path2', {
headers: {
'Authorization': authorizationHeader,
}
}).
then(function (res) {
return res.text();
}).
then(function (text) {
scope.done();
t.equal(text, 'somemoardata', "response should match");
t.end();
}).
catch(function (err) {
throw err;
});
});

0 comments on commit 86babe8

Please sign in to comment.