diff --git a/src/request/providers/node.ts b/src/request/providers/node.ts index 2b0edde9..3649983a 100644 --- a/src/request/providers/node.ts +++ b/src/request/providers/node.ts @@ -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; diff --git a/tests/unit/request/node.ts b/tests/unit/request/node.ts index 302793f9..efe571b7 100644 --- a/tests/unit/request/node.ts +++ b/tests/unit/request/node.ts @@ -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', @@ -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 {