Skip to content

Commit 05ff48b

Browse files
RobiNinoDanny McCormick
authored andcommitted
Add handlers option to downloadTool (#53)
* Add handlers option to downloadTool * Add handlers to scrape
1 parent 7060267 commit 05ff48b

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

test/tests/toolTests.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path = require('path');
33
import fs = require('fs');
44
import shell = require('shelljs');
55
import os = require('os');
6+
import hm = require('typed-rest-client/Handlers');
67

78
import * as mocha from 'mocha';
89
process.env['AGENT_VERSION'] = '2.115.0';
@@ -171,4 +172,38 @@ describe('Tool Tests', function () {
171172
}
172173
});
173174
});
175+
176+
it('download with basic authentication', function () {
177+
this.timeout(2000);
178+
179+
return new Promise<void>(async (resolve, reject) => {
180+
// General parameters:
181+
let username: string = "usr";
182+
let correctPassword: string = "pass";
183+
let url: string = "http://httpbin.org/basic-auth/" + username + "/" + correctPassword;
184+
185+
// First try downloading with WRONG credentials and verify receiving status code 401:
186+
try {
187+
let basicAuthHandler: hm.BasicCredentialHandler[] = [new hm.BasicCredentialHandler(username, "WrongPassword")];
188+
let downPath: string = await toolLib.downloadTool(url, null, basicAuthHandler);
189+
190+
reject(new Error('Should have received status code 401 when downloading with bad credentials'));
191+
}
192+
catch (err){
193+
try {
194+
assert.equal(err['httpStatusCode'], 401, 'Should have received status code 401 when downloading with bad credentials');
195+
196+
// Then try downloading with the CORRECT credentials and verify file downloaded:
197+
let basicAuthHandler: hm.BasicCredentialHandler[] = [new hm.BasicCredentialHandler(username, correctPassword)];
198+
let downPath: string = await toolLib.downloadTool(url, null, basicAuthHandler);
199+
200+
assert(tl.exist(downPath), 'File should have been downloaded');
201+
resolve()
202+
}
203+
catch (err){
204+
reject(err);
205+
}
206+
}
207+
});
208+
});
174209
});

tool.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ let requestOptions = {
1919
proxy: tl.getHttpProxyConfiguration(),
2020
cert: tl.getHttpCertConfiguration()
2121
} as ifm.IRequestOptions;
22-
let http: httpm.HttpClient = new httpm.HttpClient(userAgent, null, requestOptions);
2322
tl.setResourcePath(path.join(__dirname, 'lib.json'));
2423

2524
export function debug(message: string): void {
@@ -194,10 +193,13 @@ export function findLocalToolVersions(toolName: string, arch?: string) {
194193
*
195194
* @param url url of tool to download
196195
* @param fileName optional fileName. Should typically not use (will be a guid for reliability). Can pass fileName with an absolute path.
196+
* @param handlers optional handlers array. Auth handlers to pass to the HttpClient for the tool download.
197197
*/
198-
export async function downloadTool(url: string, fileName?: string): Promise<string> {
198+
export async function downloadTool(url: string, fileName?: string, handlers?: ifm.IRequestHandler[]): Promise<string> {
199199
return new Promise<string>(async (resolve, reject) => {
200200
try {
201+
handlers = handlers || null;
202+
let http: httpm.HttpClient = new httpm.HttpClient(userAgent, handlers, requestOptions);
201203
tl.debug(fileName);
202204
fileName = fileName || uuidV4();
203205

@@ -519,8 +521,11 @@ function _createExtractFolder(dest?: string): string {
519521
*
520522
* @param url url to scrape
521523
* @param regex regex to use for version matches
524+
* @param handlers optional handlers array. Auth handlers to pass to the HttpClient for the tool download.
522525
*/
523-
export async function scrape(url: string, regex: RegExp): Promise<string[]> {
526+
export async function scrape(url: string, regex: RegExp, handlers?: ifm.IRequestHandler[]): Promise<string[]> {
527+
handlers = handlers || null;
528+
let http: httpm.HttpClient = new httpm.HttpClient(userAgent, handlers, requestOptions);
524529
let output: string = await (await http.get(url)).readBody();
525530

526531
let matches = output.match(regex);
@@ -556,4 +561,4 @@ function _getAgentTemp(): string {
556561
}
557562

558563
return tempDirectory;
559-
}
564+
}

0 commit comments

Comments
 (0)