-
Notifications
You must be signed in to change notification settings - Fork 44
/
test.js
177 lines (146 loc) · 4.49 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
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
import { promisify } from "util";
import test from "ava";
import request from "supertest";
import {
bootstrapKoaApp,
oncePerKey,
AsyncCounter,
buildUrlWithParams,
buildUrlWithQuery,
} from "./src/util";
const agendaAppUrl = "http://localhost:4041";
const testAppUrl = "http://localhost:4042";
const { app: testApp, router: testAppRouter } = bootstrapKoaApp();
const getTestAppUrl = (path) => (path ? `${testAppUrl}${path}` : testAppUrl);
const agendaAppRequest = request(agendaAppUrl);
const bootstrapApp = async () => {
const { app, jobsReady } = require("./src");
await promisify(app.listen)
.bind(app)(4041)
.then(() => console.log("agenda-rest app running"));
await promisify(testApp.listen)
.bind(testApp)(4042)
.then(() => console.log("test app running"));
await jobsReady;
};
test.before(() => bootstrapApp());
test.serial("POST /api/job fails without content", async (t) => {
const res = await agendaAppRequest.post("/api/job").send();
t.is(res.status, 400);
});
test.serial("POST /api/job succeeds when a job is specified", async (t) => {
const res = await agendaAppRequest
.post("/api/job")
.send({ name: "foo", url: getTestAppUrl("/fooWrong") });
t.is(res.status, 200);
});
test.serial("PUT /api/job fails when the job does not exists", async (t) => {
const res = await agendaAppRequest
.put("/api/job/fooWrong")
.send({ url: getTestAppUrl("/foo") });
t.is(res.status, 400);
});
test.serial("PUT /api/job succeeds when the job exists", async (t) => {
const res = await agendaAppRequest
.put("/api/job/foo")
.send({ url: getTestAppUrl("/foo") });
t.is(res.status, 200);
});
const fooProps = {};
const defineFooEndpoint = (
route,
message,
countTimes = 1,
statusCode = 200
) => {
const counter = new AsyncCounter(countTimes);
fooProps.counter = counter;
fooProps.message = message;
fooProps.statusCode = statusCode;
const define = oncePerKey(route, () =>
testAppRouter.post(route, async (ctx, next) => {
ctx.body = fooProps.message;
ctx.status = fooProps.statusCode;
console.log(
`${fooProps.message}! ${await fooProps.counter.count()} of ${
fooProps.counter.countTimes
}`
);
await next();
})
);
define();
return counter;
};
/* TODO
testAppRouter.post('/foo/:fooParam', async (ctx, next) => {
console.log('foo with params invoked!');
console.log(ctx.params);
console.log(ctx.request.body);
ctx.body = 'foo with params success';
ctx.status = 200;
await next();
});
testAppRouter.post('/foo/cb', async (ctx, next) => {
console.log('foo callback invoked!');
ctx.body = 'foo callback success';
ctx.status = 200;
await next();
});
*/
test.serial(
"POST /api/job/now with existing foo definition invokes the foo endpoint",
async (t) => {
const counter = defineFooEndpoint("/foo", "foo now invoked");
const res = await agendaAppRequest
.post("/api/job/now")
.send({ name: "foo" });
t.is(res.text, "job scheduled for now");
await counter.finished;
}
);
test.serial(
"POST /api/job/every with existing foo definition invokes the foo endpoint",
async (t) => {
const counter = defineFooEndpoint("/foo", "foo every invoked", 3);
const res = await agendaAppRequest
.post("/api/job/every")
.send({ name: "foo", interval: "2 seconds" });
t.is(res.text, "job scheduled for repetition");
await counter.finished;
}
);
test.serial(
"POST /api/job/once with existing foo definition invokes the foo endpoint",
async (t) => {
const counter = defineFooEndpoint("/foo", "foo once invoked");
const res = await agendaAppRequest
.post("/api/job/once")
.send({ name: "foo", interval: new Date().getTime() + 10000 });
// .send({name: 'foo', interval: 'in 10 seconds'});
t.is(res.text, "job scheduled for once");
await counter.finished;
}
);
test.serial("DELETE /api/job succeeds when a job is defined", async (t) => {
const res = await agendaAppRequest.delete("/api/job/foo");
t.is(res.status, 200);
});
test("Build URL with parameters.", (t) => {
t.is(
buildUrlWithParams({
url: "http://example.com:8888/foo/:param1/:param2",
params: { param1: "value1", param2: "value2" },
}),
"http://example.com:8888/foo/value1/value2"
);
});
test("Build URL with query.", (t) => {
t.is(
buildUrlWithQuery({
url: "http://example.com/foo",
query: { query1: "value1", query2: "value2" },
}),
"http://example.com/foo?query1=value1&query2=value2"
);
});