json apis
This commit is contained in:
@@ -4,6 +4,13 @@ import * as fs from 'fs';
|
||||
|
||||
let sampleFilePath: string = path.join(__dirname, 'testoutput.txt');
|
||||
|
||||
interface HttpBinData {
|
||||
url: string;
|
||||
data: any;
|
||||
json: any;
|
||||
args?: any
|
||||
}
|
||||
|
||||
describe('basics', () => {
|
||||
let _http: httpm.HttpClient;
|
||||
|
||||
@@ -191,5 +198,45 @@ describe('basics', () => {
|
||||
expect(res.message.statusCode).toBe(404);
|
||||
let body: string = await res.readBody();
|
||||
done();
|
||||
});
|
||||
|
||||
it('gets a json object', async() => {
|
||||
let jsonObj: httpm.ITypedResponse<HttpBinData> = await _http.getJson<HttpBinData>('https://httpbin.org/get');
|
||||
expect(jsonObj.statusCode).toBe(200);
|
||||
expect(jsonObj.result).toBeDefined();
|
||||
expect(jsonObj.result.url).toBe('https://httpbin.org/get');
|
||||
});
|
||||
|
||||
it('getting a non existent json object returns null', async() => {
|
||||
let jsonObj: httpm.ITypedResponse<HttpBinData> = await _http.getJson<HttpBinData>('https://httpbin.org/status/404');
|
||||
expect(jsonObj.statusCode).toBe(404);
|
||||
expect(jsonObj.result).toBeNull();
|
||||
});
|
||||
|
||||
it('posts a json object', async() => {
|
||||
let res: any = { name: 'foo' };
|
||||
let restRes: httpm.ITypedResponse<HttpBinData> = await _http.postJson<HttpBinData>('https://httpbin.org/post', res);
|
||||
expect(restRes.statusCode).toBe(200);
|
||||
expect(restRes.result).toBeDefined();
|
||||
expect(restRes.result.url).toBe('https://httpbin.org/post');
|
||||
expect(restRes.result.json.name).toBe('foo');
|
||||
});
|
||||
|
||||
it('puts a json object', async() => {
|
||||
let res: any = { name: 'foo' };
|
||||
let restRes: httpm.ITypedResponse<HttpBinData> = await _http.putJson<HttpBinData>('https://httpbin.org/put', res);
|
||||
expect(restRes.statusCode).toBe(200);
|
||||
expect(restRes.result).toBeDefined();
|
||||
expect(restRes.result.url).toBe('https://httpbin.org/put');
|
||||
expect(restRes.result.json.name).toBe('foo');
|
||||
});
|
||||
|
||||
it('patch a json object', async() => {
|
||||
let res: any = { name: 'foo' };
|
||||
let restRes: httpm.ITypedResponse<HttpBinData> = await _http.patchJson<HttpBinData>('https://httpbin.org/patch', res);
|
||||
expect(restRes.statusCode).toBe(200);
|
||||
expect(restRes.result).toBeDefined();
|
||||
expect(restRes.result.url).toBe('https://httpbin.org/patch');
|
||||
expect(restRes.result.json.name).toBe('foo');
|
||||
});
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user