This repository was archived by the owner on May 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
234 lines (195 loc) · 7.09 KB
/
test.js
File metadata and controls
234 lines (195 loc) · 7.09 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
'use strict';
let http = require('http');
let _ = require('lodash');
let assert = require('chai').assert;
let koa = require('koa');
let koaRouter = require('koa-router');
let body = require('koa-body');
let util = require('kinda-util').create();
let KindaHTTPClient = require('./src');
suite('KindaHTTPClient', function() {
let httpServer, baseURL;
let catchError = async function(fn) {
let err;
try {
await fn();
} catch (e) {
err = e;
}
return err;
};
suiteSetup(async function() {
let serverPort = 8888;
baseURL = 'http://localhost:' + serverPort;
let server = koa();
server.use(body());
let router = koaRouter();
server.use(router.routes());
server.use(router.allowedMethods());
router.get('/simple', function *() {
this.body = 'ok';
});
router.get('/restricted-area', function *() {
let accessToken = this.headers['x-access-token'];
if (accessToken !== 'secret') {
this.status = 403;
return;
}
this.body = 'ok';
});
router.get('/resource', function *() {
if (!_.startsWith(this.headers.accept, 'application/json')) {
this.status = 400;
return;
}
this.body = { result: 'ok' };
});
router.post('/resource', function *() {
if (!_.startsWith(this.headers.accept, 'application/json')) {
this.status = 400;
return;
}
if (!_.startsWith(this.headers['content-type'], 'application/json')) {
this.status = 400;
return;
}
this.status = 201;
this.body = { result: 'ok' };
});
router.put('/resource', function *() {
this.body = { result: 'ok' };
});
router.del('/resource', function *() {
this.status = 204;
});
router.get('/not-json', function *() {
this.body = 'ok'; // 'Content-Type' will be 'text/plain'
});
router.get('/not-unicode', function *() {
this.type = 'text/html; charset=iso-8859-1';
this.body = new Buffer('436166e9', 'hex');
});
router.get('/json-but-incorrect-content-type', function *() {
this.body = '"ok"'; // 'Content-Type' will be 'text/plain'
});
router.get('/big-resource', function *() {
yield util.timeout(55);
this.body = 'ok';
});
router.get('/binary', function *() {
this.body = new Buffer([1, 2, 3]);
});
httpServer = http.createServer(server.callback());
httpServer.listen(serverPort);
});
suiteTeardown(async function() {
httpServer.close();
});
test('simple get', async function() {
let httpClient = KindaHTTPClient.create();
let res = await httpClient.get(baseURL + '/simple');
assert.strictEqual(res.statusCode, 200);
assert.isTrue(_.startsWith(res.headers['content-type'], 'text/plain'));
assert.strictEqual(res.body, 'ok');
res = await httpClient.get(baseURL + '/invalid-url');
assert.strictEqual(res.statusCode, 404);
});
test('default options', async function() {
let httpClient = KindaHTTPClient.create();
let res = await httpClient.get(baseURL + '/restricted-area');
assert.strictEqual(res.statusCode, 403);
httpClient = KindaHTTPClient.create({
headers: { 'x-access-token': 'secret' }
});
res = await httpClient.get(baseURL + '/restricted-area');
assert.strictEqual(res.statusCode, 200);
assert.strictEqual(res.body, 'ok');
});
test('method options', async function() {
let httpClient = KindaHTTPClient.create({
headers: { 'x-access-token': 'invalid-secret' }
});
let res = await httpClient.get(baseURL + '/restricted-area');
assert.strictEqual(res.statusCode, 403);
res = await httpClient.get({
url: baseURL + '/restricted-area',
headers: { 'x-access-token': 'secret' }
});
assert.strictEqual(res.statusCode, 200);
assert.strictEqual(res.body, 'ok');
});
test('get json', async function() {
let httpClient = KindaHTTPClient.create();
let res = await httpClient.get(baseURL + '/resource');
assert.strictEqual(res.statusCode, 400);
httpClient = KindaHTTPClient.create({ json: true });
res = await httpClient.get(baseURL + '/resource');
assert.strictEqual(res.statusCode, 200);
assert.isTrue(_.startsWith(res.headers['content-type'], 'application/json'));
assert.deepEqual(res.body, { result: 'ok' });
httpClient = KindaHTTPClient.create({ json: true });
res = await httpClient.get(baseURL + '/not-json');
assert.strictEqual(res.statusCode, 200);
assert.isTrue(_.startsWith(res.headers['content-type'], 'text/plain'));
assert.deepEqual(res.body, 'ok');
httpClient = KindaHTTPClient.create({ json: true });
res = await httpClient.get(baseURL + '/json-but-incorrect-content-type');
assert.strictEqual(res.statusCode, 200);
assert.isTrue(_.startsWith(res.headers['content-type'], 'text/plain'));
assert.deepEqual(res.body, 'ok');
});
test('post json', async function() {
let httpClient = KindaHTTPClient.create({ json: true });
let res = await httpClient.post(baseURL + '/resource', { param: 123 });
assert.strictEqual(res.statusCode, 201);
assert.isTrue(_.startsWith(res.headers['content-type'], 'application/json'));
assert.deepEqual(res.body, { result: 'ok' });
});
test('put json', async function() {
let httpClient = KindaHTTPClient.create({ json: true });
let res = await httpClient.put(baseURL + '/resource');
assert.strictEqual(res.statusCode, 200);
assert.isTrue(_.startsWith(res.headers['content-type'], 'application/json'));
assert.deepEqual(res.body, { result: 'ok' });
});
test('delete json', async function() {
let httpClient = KindaHTTPClient.create({ json: true });
let res = await httpClient.del(baseURL + '/resource');
assert.strictEqual(res.statusCode, 204);
assert.isUndefined(res.body);
});
test('get non unicode text', async function() {
let httpClient = KindaHTTPClient.create();
let res = await httpClient.get({ url: baseURL + '/not-unicode', encoding: null });
assert.strictEqual(res.statusCode, 200);
assert.strictEqual(res.headers['content-type'], 'text/html; charset=iso-8859-1');
let body = res.body.toString('binary'); // equivalent to latin-1
assert.strictEqual(body, 'Café');
});
test('get binary', async function() {
let httpClient = KindaHTTPClient.create();
let res = await httpClient.get({ url: baseURL + '/binary', encoding: null });
assert.strictEqual(res.statusCode, 200);
assert.isTrue(Buffer.isBuffer(res.body));
assert.strictEqual(res.body.length, 3);
assert.strictEqual(res.body[0], 1);
assert.strictEqual(res.body[1], 2);
assert.strictEqual(res.body[2], 3);
});
test('timeout', async function() {
let httpClient = KindaHTTPClient.create();
let err = await catchError(async function() {
await httpClient.get({
url: baseURL + '/big-resource',
timeout: 50
});
});
assert.instanceOf(err, Error);
let res = await httpClient.get({
url: baseURL + '/big-resource',
timeout: 100
});
assert.strictEqual(res.statusCode, 200);
assert.strictEqual(res.body, 'ok');
});
});