Skip to content

Commit

Permalink
test: add auth test
Browse files Browse the repository at this point in the history
  • Loading branch information
hyj1991 committed Aug 13, 2018
1 parent 4d435d5 commit d0bd1f2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
3 changes: 3 additions & 0 deletions test/fixtures/auth/.cnpmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
registry=https://registry-mock.org/
//registry-mock.org/:always-auth=true
//registry-mock.org/:_password="bW9jaw=="
32 changes: 32 additions & 0 deletions test/get.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
'use strict';

const fs = require('fs');
const assert = require('assert');
const mm = require('mm');
const path = require('path');
const mockCnpmrc = path.join(__dirname, './fixtures/auth/');
if (!fs.existsSync(mockCnpmrc + '.cnpmrc')) {
fs.writeFileSync(mockCnpmrc + '.cnpmrc', 'registry=https://registry-mock.org/\n//registry-mock.org/:always-auth=true\n//registry-mock.org/:_password="bW9jaw=="');
}
mm(process.env, 'HOME', mockCnpmrc);
mm(process.env, 'USERPROFILE', mockCnpmrc);
// clean cnpm_config.js cache
delete require.cache[require.resolve('../lib/get')];
delete require.cache[require.resolve('../lib/cnpm_config')];
const get = require('../lib/get');
mm.restore();

describe('test/get.test.js', () => {
it('should retry on JSON parse error', function* () {
Expand All @@ -18,4 +31,23 @@ describe('test/get.test.js', () => {
assert(err.res.requestUrls.length === 5);
}
});

it('should set auth info into header', function* () {
const logger = {
warn(msg) {
assert(msg.indexOf('[npminstall:get] retry GET') === 0);
},
};
const options = { dataType: 'json' };
assert(fs.existsSync(mockCnpmrc + '.cnpmrc'));
try {
yield get('https://registry-mock.org/mock', options, { console: logger });
assert(false, 'should not run this');
} catch (err) {
const headers = options.headers;
assert(headers.Authorization);
assert(err.name === 'RequestError');
assert(err.res.requestUrls.length === 5);
}
});
});

0 comments on commit d0bd1f2

Please sign in to comment.