-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
75 lines (65 loc) · 2.1 KB
/
test.js
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
const qs = require('qs');
const micro = require('micro');
const listen = require('test-listen');
const request = require('request-promise');
const test = require('ava').test;
const urlEncoded = require('./index');
const success = { test: 'success' };
const simpleQuery = {
one: 'first',
two: 'second',
other: 'more',
arr: ['arr[first]', 'arr[second]', 'arr.third'],
obj: {
three: 'obj.third',
four: 'obj.fourth',
},
};
const queryExample = Object.assign({}, simpleQuery);
const bodyExample = Object.assign({}, simpleQuery);
const testRequest = async (handler, options) => {
const service = micro(await urlEncoded(handler));
options.uri = await listen(service);
const body = await request(options);
service.close();
return body;
};
queryExample['this'] = 'is a query example';
bodyExample['this'] = 'is a body example';
test('a query string', async t => {
const result = await urlEncoded(qs.stringify(simpleQuery));
t.deepEqual(simpleQuery, result);
});
test('a query buffer', async t => {
const buff = new Buffer(qs.stringify(simpleQuery));
const result = await urlEncoded(buff);
t.deepEqual(simpleQuery, result);
});
test('a request with query', async t => {
const handler = async (req, res) => {
t.deepEqual(queryExample, req.query);
return success;
};
const options = { method: 'GET', qs: queryExample };
const body = await testRequest(handler, options);
t.deepEqual(JSON.parse(body).test, 'success');
});
test('a request with body', async t => {
const handler = async (req, res) => {
t.deepEqual(bodyExample, req.body);
return success;
};
const options = { method: 'POST', form: bodyExample };
const body = await testRequest(handler, options);
t.deepEqual(JSON.parse(body).test, 'success');
});
test('a request with both', async t => {
const handler = async (req, res) => {
t.deepEqual(queryExample, req.query);
t.deepEqual(bodyExample, req.body);
return success;
};
const options = { method: 'POST', qs: queryExample, form: bodyExample };
const body = await testRequest(handler, options);
t.deepEqual(JSON.parse(body).test, 'success');
});