initial commit

This commit is contained in:
Bryan MacFarlane
2020-01-09 18:32:59 -05:00
commit 0fc3b75a6d
15 changed files with 6163 additions and 0 deletions

57
__tests__/auth.test.ts Normal file
View File

@@ -0,0 +1,57 @@
import * as httpm from '../_out';
import * as path from 'path';
import * as am from '../_out/auth';
describe('auth', () => {
beforeEach(() => {
})
afterEach(() => {
})
it('does basic http get request with basic auth', async() => {
let bh: am.BasicCredentialHandler = new am.BasicCredentialHandler('johndoe', 'password');
let http: httpm.HttpClient = new httpm.HttpClient('http-client-tests', [bh]);
let res: httpm.HttpClientResponse = await http.get('http://httpbin.org/get');
expect(res.message.statusCode).toBe(200);
let body: string = await res.readBody();
let obj:any = JSON.parse(body);
let auth: string = obj.headers.Authorization;
let creds: string = Buffer.from(auth.substring('Basic '.length), 'base64').toString();
expect(creds).toBe('johndoe:password');
expect(obj.url).toBe("https://httpbin.org/get");
});
it('does basic http get request with pat token auth', async() => {
let token: string = 'scbfb44vxzku5l4xgc3qfazn3lpk4awflfryc76esaiq7aypcbhs';
let ph: am.PersonalAccessTokenCredentialHandler =
new am.PersonalAccessTokenCredentialHandler(token);
let http: httpm.HttpClient = new httpm.HttpClient('http-client-tests', [ph]);
let res: httpm.HttpClientResponse = await http.get('http://httpbin.org/get');
expect(res.message.statusCode).toBe(200);
let body: string = await res.readBody();
let obj:any = JSON.parse(body);
let auth: string = obj.headers.Authorization;
let creds: string = Buffer.from(auth.substring('Basic '.length), 'base64').toString();
expect(creds).toBe('PAT:' + token);
expect(obj.url).toBe("https://httpbin.org/get");
});
it('does basic http get request with pat token auth', async() => {
let token: string = 'scbfb44vxzku5l4xgc3qfazn3lpk4awflfryc76esaiq7aypcbhs';
let ph: am.BearerCredentialHandler =
new am.BearerCredentialHandler(token);
let http: httpm.HttpClient = new httpm.HttpClient('http-client-tests', [ph]);
let res: httpm.HttpClientResponse = await http.get('http://httpbin.org/get');
expect(res.message.statusCode).toBe(200);
let body: string = await res.readBody();
let obj:any = JSON.parse(body);
let auth: string = obj.headers.Authorization;
expect(auth).toBe('Bearer ' + token);
expect(obj.url).toBe("https://httpbin.org/get");
});
})

182
__tests__/basics.test.ts Normal file
View File

@@ -0,0 +1,182 @@
import * as httpm from '../_out';
import * as path from 'path';
import * as am from '../_out/auth';
import * as fs from 'fs';
let sampleFilePath: string = path.join(__dirname, 'testoutput.txt');
describe('basics', () => {
let _http: httpm.HttpClient;
let _httpbin: httpm.HttpClient;
beforeEach(() => {
_http = new httpm.HttpClient('http-client-tests');
})
afterEach(() => {
})
it('constructs', () => {
let http: httpm.HttpClient = new httpm.HttpClient('typed-test-client-tests');
expect(http).toBeDefined();
});
// responses from httpbin return something like:
// {
// "args": {},
// "headers": {
// "Connection": "close",
// "Host": "httpbin.org",
// "User-Agent": "typed-test-client-tests"
// },
// "origin": "173.95.152.44",
// "url": "https://httpbin.org/get"
// }
it('does basic http get request', async() => {
let res: httpm.HttpClientResponse = await _http.get('http://httpbin.org/get');
expect(res.message.statusCode).toBe(200);
let body: string = await res.readBody();
let obj: any = JSON.parse(body);
expect(obj.url).toBe("https://httpbin.org/get");
expect(obj.headers["User-Agent"]).toBeTruthy();
});
it('does basic http get request with no user agent', async() => {
let http: httpm.HttpClient = new httpm.HttpClient();
let res: httpm.HttpClientResponse = await http.get('http://httpbin.org/get');
expect(res.message.statusCode).toBe(200);
let body: string = await res.readBody();
let obj: any = JSON.parse(body);
expect(obj.url).toBe("https://httpbin.org/get");
expect(obj.headers["User-Agent"]).toBeFalsy();
});
it('does basic https get request', async() => {
let res: httpm.HttpClientResponse = await _http.get('https://httpbin.org/get');
expect(res.message.statusCode).toBe(200);
let body: string = await res.readBody();
let obj: any = JSON.parse(body);
expect(obj.url).toBe("https://httpbin.org/get");
});
it('does basic http get request with default headers', async() => {
let http: httpm.HttpClient = new httpm.HttpClient('http-client-tests', [], {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
});
let res: httpm.HttpClientResponse = await http.get('http://httpbin.org/get');
expect(res.message.statusCode).toBe(200);
let body: string = await res.readBody();
let obj:any = JSON.parse(body);
expect(obj.headers.Accept).toBe('application/json');
expect(obj.headers['Content-Type']).toBe('application/json');
expect(obj.url).toBe("https://httpbin.org/get");
});
it('does basic http get request with merged headers', async() => {
let http: httpm.HttpClient = new httpm.HttpClient('http-client-tests', [], {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
});
let res: httpm.HttpClientResponse = await http.get('http://httpbin.org/get', {
'content-type': 'application/x-www-form-urlencoded'
});
expect(res.message.statusCode).toBe(200);
let body: string = await res.readBody();
let obj:any = JSON.parse(body);
expect(obj.headers.Accept).toBe('application/json');
expect(obj.headers['Content-Type']).toBe('application/x-www-form-urlencoded');
expect(obj.url).toBe("https://httpbin.org/get");
});
it('pipes a get request', () => {
return new Promise<string>(async (resolve, reject) => {
let file: NodeJS.WritableStream = fs.createWriteStream(sampleFilePath);
(await _http.get('https://httpbin.org/get')).message.pipe(file).on('close', () => {
let body: string = fs.readFileSync(sampleFilePath).toString();
let obj:any = JSON.parse(body);
expect(obj.url).toBe("https://httpbin.org/get");
resolve();
});
});
});
it('does basic get request with redirects', async() => {
let res: httpm.HttpClientResponse = await _http.get("https://httpbin.org/redirect-to?url=" + encodeURIComponent("https://httpbin.org/get"))
expect(res.message.statusCode).toBe(200);
let body: string = await res.readBody();
let obj:any = JSON.parse(body);
expect(obj.url).toBe("https://httpbin.org/get");
});
it('does basic get request with redirects (303)', async() => {
let res: httpm.HttpClientResponse = await _http.get("https://httpbin.org/redirect-to?url=" + encodeURIComponent("https://httpbin.org/get") + '&status_code=303')
expect(res.message.statusCode).toBe(200);
let body: string = await res.readBody();
let obj:any = JSON.parse(body);
expect(obj.url).toBe("https://httpbin.org/get");
});
it('returns 404 for not found get request on redirect', async() => {
let res: httpm.HttpClientResponse = await _http.get("https://httpbin.org/redirect-to?url=" + encodeURIComponent("https://httpbin.org/status/404") + '&status_code=303')
expect(res.message.statusCode).toBe(404);
let body: string = await res.readBody();
});
it('does not follow redirects if disabled', async() => {
let http: httpm.HttpClient = new httpm.HttpClient('typed-test-client-tests', null, { allowRedirects: false });
let res: httpm.HttpClientResponse = await http.get("https://httpbin.org/redirect-to?url=" + encodeURIComponent("https://httpbin.org/get"))
expect(res.message.statusCode).toBe(302);
let body: string = await res.readBody();
});
it('does basic head request', async() => {
let res: httpm.HttpClientResponse = await _http.head('http://httpbin.org/get');
expect(res.message.statusCode).toBe(200);
});
it('does basic http delete request', async() => {
let res: httpm.HttpClientResponse = await _http.del('http://httpbin.org/delete');
expect(res.message.statusCode).toBe(200);
let body: string = await res.readBody();
let obj:any = JSON.parse(body);
});
it('does basic http post request', async() => {
let b: string = 'Hello World!';
let res: httpm.HttpClientResponse = await _http.post('http://httpbin.org/post', b);
expect(res.message.statusCode).toBe(200);
let body: string = await res.readBody();
let obj:any = JSON.parse(body);
expect(obj.data).toBe(b);
expect(obj.url).toBe("https://httpbin.org/post");
});
it('does basic http patch request', async() => {
let b: string = 'Hello World!';
let res: httpm.HttpClientResponse = await _http.patch('http://httpbin.org/patch', b);
expect(res.message.statusCode).toBe(200);
let body: string = await res.readBody();
let obj:any = JSON.parse(body);
expect(obj.data).toBe(b);
expect(obj.url).toBe("https://httpbin.org/patch");
});
it('does basic http options request', async() => {
let res: httpm.HttpClientResponse = await _http.options('http://httpbin.org');
expect(res.message.statusCode).toBe(200);
let body: string = await res.readBody();
});
it('returns 404 for not found get request', async() => {
let res: httpm.HttpClientResponse = await _http.get('http://httpbin.org/status/404');
expect(res.message.statusCode).toBe(404);
let body: string = await res.readBody();
});
})

View File

@@ -0,0 +1,65 @@
import * as httpm from '../_out';
import * as path from 'path';
import * as am from '../_out/auth';
import * as fs from 'fs';
let sampleFilePath: string = path.join(__dirname, 'testoutput.txt');
describe('basics', () => {
let _http: httpm.HttpClient;
let _httpbin: httpm.HttpClient;
beforeEach(() => {
_http = new httpm.HttpClient('typed-test-client-tests', [], { keepAlive: true });
})
afterEach(() => {
})
it('does basic http get request with keepAlive true', async() => {
let res: httpm.HttpClientResponse = await _http.get('http://httpbin.org/get');
expect(res.message.statusCode).toBe(200);
let body: string = await res.readBody();
let obj:any = JSON.parse(body);
expect(obj.url).toBe("https://httpbin.org/get");
});
it('does basic head request with keepAlive true', async() => {
let res: httpm.HttpClientResponse = await _http.head('http://httpbin.org/get');
expect(res.message.statusCode).toBe(200);
});
it('does basic http delete request with keepAlive true', async() => {
let res: httpm.HttpClientResponse = await _http.del('http://httpbin.org/delete');
expect(res.message.statusCode).toBe(200);
let body: string = await res.readBody();
let obj:any = JSON.parse(body);
});
it('does basic http post request with keepAlive true', async() => {
let b: string = 'Hello World!';
let res: httpm.HttpClientResponse = await _http.post('http://httpbin.org/post', b);
expect(res.message.statusCode).toBe(200);
let body: string = await res.readBody();
let obj:any = JSON.parse(body);
expect(obj.data).toBe(b);
expect(obj.url).toBe("https://httpbin.org/post");
});
it('does basic http patch request with keepAlive true', async() => {
let b: string = 'Hello World!';
let res: httpm.HttpClientResponse = await _http.patch('http://httpbin.org/patch', b);
expect(res.message.statusCode).toBe(200);
let body: string = await res.readBody();
let obj:any = JSON.parse(body);
expect(obj.data).toBe(b);
expect(obj.url).toBe("https://httpbin.org/patch");
});
it('does basic http options request with keepAlive true', async() => {
let res: httpm.HttpClientResponse = await _http.options('http://httpbin.org');
expect(res.message.statusCode).toBe(200);
let body: string = await res.readBody();
});
});

96
__tests__/proxy.test.ts Normal file
View File

@@ -0,0 +1,96 @@
import * as pm from '../_out/proxy';
import * as url from 'url';
describe('proxy', () => {
beforeEach(() => {
})
afterEach(() => {
})
it('does not return proxyUrl if variables not set', () => {
_clearVars();
let proxyUrl = pm.getProxyUrl(url.parse('https://github.com'));
expect(proxyUrl).toBeUndefined();
})
it('returns proxyUrl if https_proxy set for https url', () => {
_clearVars();
process.env["https_proxy"] = "https://myproxysvr";
let proxyUrl = pm.getProxyUrl(url.parse('https://github.com'));
expect(proxyUrl).toBeDefined();
})
it('does not return proxyUrl if http_proxy set for https url', () => {
_clearVars();
process.env["http_proxy"] = "https://myproxysvr";
let proxyUrl = pm.getProxyUrl(url.parse('https://github.com'));
expect(proxyUrl).toBeUndefined();
})
it('returns proxyUrl if http_proxy set for http url', () => {
_clearVars();
process.env["http_proxy"] = "http://myproxysvr";
let proxyUrl = pm.getProxyUrl(url.parse('http://github.com'));
expect(proxyUrl).toBeDefined();
})
it('does not return proxyUrl if only host as no_proxy list', () => {
_clearVars();
process.env["https_proxy"] = "https://myproxysvr";
process.env["no_proxy"] = "myserver"
let proxyUrl = pm.getProxyUrl(url.parse('https://myserver'));
expect(proxyUrl).toBeUndefined();
})
it('does not return proxyUrl if host in no_proxy list', () => {
_clearVars();
process.env["https_proxy"] = "https://myproxysvr";
process.env["no_proxy"] = "otherserver,myserver,anotherserver:8080"
let proxyUrl = pm.getProxyUrl(url.parse('https://myserver'));
expect(proxyUrl).toBeUndefined();
})
it('does not return proxyUrl if host in no_proxy list with spaces', () => {
_clearVars();
process.env["https_proxy"] = "https://myproxysvr";
process.env["no_proxy"] = "otherserver, myserver ,anotherserver:8080"
let proxyUrl = pm.getProxyUrl(url.parse('https://myserver'));
expect(proxyUrl).toBeUndefined();
})
it('does not return proxyUrl if host in no_proxy list with ports', () => {
_clearVars();
process.env["https_proxy"] = "https://myproxysvr";
process.env["no_proxy"] = "otherserver, myserver:8080 ,anotherserver"
let proxyUrl = pm.getProxyUrl(url.parse('https://myserver:8080'));
expect(proxyUrl).toBeUndefined();
})
it('returns proxyUrl if https_proxy set and not in no_proxy list', () => {
_clearVars();
process.env["https_proxy"] = "https://myproxysvr";
process.env["no_proxy"] = "otherserver, myserver ,anotherserver:8080"
let proxyUrl = pm.getProxyUrl(url.parse('https://github.com'));
expect(proxyUrl).toBeDefined();
})
it('returns proxyUrl if https_proxy set empty no_proxy set', () => {
_clearVars();
process.env["https_proxy"] = "https://myproxysvr";
process.env["no_proxy"] = ""
let proxyUrl = pm.getProxyUrl(url.parse('https://github.com'));
expect(proxyUrl).toBeDefined();
})
})
function _clearVars() {
delete process.env.http_proxy;
delete process.env.HTTP_PROXY;
delete process.env.https_proxy;
delete process.env.HTTPS_PROXY;
delete process.env.no_proxy;
delete process.env.NO_PROXY;
}