Skip to content
This repository has been archived by the owner on Jul 30, 2018. It is now read-only.

fixed usage of node auth with special characters #323

Merged
merged 1 commit into from
May 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/request/providers/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ export function getAuth(proxyAuth: string | undefined, options: NodeRequestOptio
}

if (options.user || options.password) {
return encodeURIComponent(options.user || '') + ':' + encodeURIComponent(options.password || '');
return `${ options.user || '' }:${ options.password || '' }`;
}

return undefined;
Expand Down
70 changes: 34 additions & 36 deletions tests/unit/request/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,18 @@ function getAuthRequestUrl(dataKey: string, user: string = 'user', password: str
return requestUrl.slice(0, 7) + user + ':' + password + '@' + requestUrl.slice(7);
}

function assertBasicAuthentication(options: { user?: string, password?: string }, expectedAuth: string, dfd: any) {
nodeRequest(getRequestUrl('foo.json'), options).then(
dfd.callback(function (response: any) {
const actual: string = response.nativeResponse.req._headers.authorization;
const expected = `Basic ${ new Buffer(expectedAuth).toString('base64') }`;

assert.strictEqual(actual, expected);
}),
dfd.reject.bind(dfd)
);
}

registerSuite({
name: 'request/node',

Expand Down Expand Up @@ -413,49 +425,35 @@ registerSuite({

'user and password': {
both(this: any): void {
const dfd = this.async();
nodeRequest(getRequestUrl('foo.json'), {
user: 'user name',
password: 'pass word'
}).then(
dfd.callback(function (response: any) {
const actual: string = response.nativeResponse.req._headers.authorization;
const expected: string = 'Basic ' + new Buffer('user%20name:pass%20word').toString('base64');

assert.strictEqual(actual, expected);
}),
dfd.reject.bind(dfd)
);
const user = 'user name';
const password = 'pass word';
assertBasicAuthentication({
user,
password
}, `${ user }:${ password }`, this.async());
},

'user only'(this: any): void {
const dfd = this.async();
nodeRequest(getRequestUrl('foo.json'), {
user: 'user name'
}).then(
dfd.callback(function (response: any) {
const actual: string = response.nativeResponse.req._headers.authorization;
const expected: string = 'Basic ' + new Buffer('user%20name:').toString('base64');

assert.strictEqual(actual, expected);
}),
dfd.reject.bind(dfd)
);
const user = 'user name';
assertBasicAuthentication({
user
}, `${ user }:`, this.async());
},

'password only'(this: any): void {
const dfd = this.async();
nodeRequest(getRequestUrl('foo.json'), {
password: 'pass word'
}).then(
dfd.callback(function (response: any) {
const actual: string = response.nativeResponse.req._headers.authorization;
const expected: string = 'Basic ' + new Buffer(':pass%20word').toString('base64');
const password = 'pass word';
assertBasicAuthentication({
password
}, `:${ password }`, this.async());
},

assert.strictEqual(actual, expected);
}),
dfd.reject.bind(dfd)
);
'special characters'(this: any): void {
const user = '$pecialUser';
const password = '__!passW@rd';
assertBasicAuthentication({
user,
password
}, `${ user }:${ password }`, this.async());
},

error(this: any): void {
Expand Down