-
Notifications
You must be signed in to change notification settings - Fork 130
/
server.test.js
67 lines (57 loc) · 1.71 KB
/
server.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
// ava is a test runner for Node.js that isolates tests
const test = require('ava');
// micro is a minimal http framework (what's run by `npm start`)
const micro = require('micro');
// test-listen creates URLs with ephimeral ports ideal for isolated tests
const listen = require('test-listen');
// node-fetch brings window.fetch to Node.js
const fetch = require('node-fetch');
const main = require('.');
// serveStatic
[
['/', /Quickstart/],
['/index.html', /sandbox\.web\.squarecdn/],
['/favicon.ico', /.+/],
].forEach(([path, re]) => {
test(`serves ${path}`, async (t) => {
const service = micro(main);
const url = await listen(service);
const res = await fetch(url + path);
t.true(res.ok);
const body = await res.text();
t.regex(body, re);
service.close(t.falsy);
});
});
// createPayment
test('createPayment errors with invalid payload', async (t) => {
const service = micro(main);
const url = await listen(service);
const res = await fetch(`${url}/payment`, {
method: 'post',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
wrong: true,
}),
});
// expect 'bad request' response because body is wrong (literally)
t.false(res.ok);
t.is(res.status, 400);
service.close(t.falsy);
});
// storeCard
test('storeCard errors with invalid payload', async (t) => {
const service = micro(main);
const url = await listen(service);
const res = await fetch(`${url}/card`, {
method: 'post',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
wrong: true,
}),
});
// expect 'bad request' response because body is wrong (literally)
t.false(res.ok);
t.is(res.status, 400);
service.close(t.falsy);
});