-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.js
125 lines (102 loc) · 2.84 KB
/
index.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
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
require("env-yaml").config();
const test = require("tape");
const sinon = require("sinon");
const { createRequest, createResponse } = require("node-mocks-http");
const { akamaiFastPurge } = require("./index.js");
test("akamaiFastPurge: 400 Error: expected HTTP POST", async (t) => {
const req = createRequest({
method: "GET",
url: "/",
params: {},
body: {},
headers: {
"Content-Type": "application/json",
},
});
const res = createResponse();
sinon.spy(res, "status");
sinon.spy(res, "json");
await akamaiFastPurge(req, res);
t.doesNotThrow(() => sinon.assert.calledOnceWithExactly(res.status, 400));
t.doesNotThrow(() =>
sinon.assert.calledOnceWithExactly(res.json, {
error: "Error: expected HTTP POST",
})
);
t.end();
});
test(`akamaiFastPurge: 400 Error: Requires "X-Auth" header with Service Key`, async (t) => {
const req = createRequest({
method: "POST",
url: "/",
params: {},
body: {},
headers: {
"Content-Type": "application/json",
},
});
const res = createResponse();
sinon.spy(res, "status");
sinon.spy(res, "json");
await akamaiFastPurge(req, res);
t.doesNotThrow(() => sinon.assert.calledOnceWithExactly(res.status, 400));
t.doesNotThrow(() =>
sinon.assert.calledOnceWithExactly(res.json, {
error: `Error: Requires "X-Auth" header with Service Key`,
})
);
t.end();
});
test(`akamaiFastPurge: 401 Error: Unauthorized`, async (t) => {
const req = createRequest({
method: "POST",
url: "/",
params: {},
body: {},
headers: {
"Content-Type": "application/json",
"X-Auth": "FAIL",
},
});
const res = createResponse();
sinon.spy(res, "status");
sinon.spy(res, "json");
await akamaiFastPurge(req, res);
t.doesNotThrow(() => sinon.assert.calledOnceWithExactly(res.status, 401));
t.doesNotThrow(() =>
sinon.assert.calledOnceWithExactly(res.json, {
error: `Error: Unauthorized`,
})
);
t.end();
});
test(`akamaiFastPurge: 200 Success`, async (t) => {
const req = createRequest({
method: "POST",
url: "/",
params: {},
body: JSON.stringify({
objects: ["Foo", "Bar"],
}),
headers: {
"Content-Type": "application/json",
"X-Auth": process.env.SERVICE_KEY,
},
});
const res = createResponse();
sinon.spy(res, "status");
sinon.spy(res, "json");
await akamaiFastPurge(req, res);
t.doesNotThrow(() => {
sinon.assert.calledOnceWithExactly(res.status, 200);
}, "res.status should be called with 200");
// NOTE: leaving these test case here to document
// we can not test it as the response body given to the json
// function is dynamic due to the nature of the akamai response
// t.doesNotThrow(() =>
// sinon.assert.calledOnceWithExactly(res.json, {
// error: `Error: Unauthorized`,
// })
// );
t.end();
});