forked from davetemplin/web-request
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ts
95 lines (85 loc) · 4.1 KB
/
test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Project: https://github.com/davetemplin/web-request/
// Written by: Dave Templin <https://github.com/davetemplin/>
import * as fs from 'fs';
import * as path from 'path';
import {assert} from 'chai';
import * as WebRequest from './index';
describe('all', function () {
it('get', async function () {
var response = await WebRequest.get('http://www.google.com/');
assert(response.contentType === 'text/html', 'contentType');
assert(response.charset === 'ISO-8859-1', 'charset');
assert(response.statusCode === 200, 'statusCode');
assert(response.statusMessage === 'OK', 'statusMessage');
assert(response.method === 'GET', 'method');
assert(response.headers['content-type'] === 'text/html; charset=ISO-8859-1', 'headers');
assert(response.uri.protocol === 'http:', 'uri.protocol');
assert(response.uri.host === 'www.google.com', 'uri.host');
assert(response.uri.path === '/', 'uri.path');
assert(response.contentLength > 1000, 'contentLength');
assert(response.content.indexOf('<!doctype html>') === 0, 'content');
});
it('404', async function () {
var response = await WebRequest.get('http://www.google.com/invalid');
assert(response.statusCode === 404);
});
it('error', async function () {
try {
var response = await WebRequest.get('');
assert(false, 'Expected exception did not occur');
}
catch (err) {
assert(err instanceof WebRequest.RequestError, 'instanceof');
assert(err.message === 'options.uri is a required argument', 'message');
}
});
it('json1', async function () {
var result = await WebRequest.json<any>('http://query.yahooapis.com/v1/public/yql?q=select+*+from+yahoo.finance.quotes+where+symbol+IN+(%22YHOO%22,%22AAPL%22)&format=json&env=http://datatables.org/alltables.env');
assert(result.query.results.quote[0].Symbol === 'YHOO', 'YHOO');
assert(result.query.results.quote[1].Symbol === 'AAPL', 'AAPL');
});
it('json2', async function () {
interface QuoteResult {
query: {
results: {
quote: Array<{
Symbol: string;
Bid: string;
DaysHigh: string;
DaysLow: string;
Volume: string;
}>
}
}
}
var result = await WebRequest.json<QuoteResult>('http://query.yahooapis.com/v1/public/yql?q=select+*+from+yahoo.finance.quotes+where+symbol+IN+(%22YHOO%22,%22AAPL%22)&format=json&env=http://datatables.org/alltables.env');
assert(result.query.results.quote[0].Symbol === 'YHOO', 'YHOO');
assert(result.query.results.quote[1].Symbol === 'AAPL', 'AAPL');
});
it('cookies', async function () {
var response = await WebRequest.get('http://www.google.com/', {jar: true});
assert(response.cookies.length > 0, response.cookies.length.toString() + ' cookies');
});
it('stream', async function () {
var file = 'google.png';
var request = WebRequest.stream('https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png');
var w = fs.createWriteStream(file);
request.pipe(w);
var response = await request.response;
await new Promise(resolve => w.on('finish', () => resolve()));
var stat = fs.statSync(file);
assert(stat.size > 10000, 'file-size');
assert(response.content === null, 'null content');
fs.unlinkSync(file);
});
it('throwResponseError', async function () {
try {
await WebRequest.get('http://xyzzy.com/123', {throwResponseError: true});
assert(false, 'Expected exception did not occur');
}
catch (err) {
assert(err instanceof WebRequest.ResponseError, 'instanceof');
assert((<WebRequest.ResponseError<string>>err).response.statusCode === 404, 'statusCode');
}
});
});