-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcheck.test.w
64 lines (54 loc) · 1.57 KB
/
check.test.w
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
bring "./check.w" as c;
bring http;
bring cloud;
bring expect;
let checkName = "Test";
let api = new cloud.Api();
let s = new cloud.Bucket() as "customize";
let responseKey = "response.json";
let simulateFailure = inflight () => {
s.putJson(responseKey, cloud.ApiResponse {
status: 404
});
};
api.get("/foo", inflight (req) => {
let response = s.tryGetJson(responseKey) ?? { status: 200, body: req.path };
return cloud.ApiResponse.fromJson(response);
});
new cloud.Function(simulateFailure) as "simulate failure";
let check = new c.Check(inflight () => {
let response = http.get("{api.url}/foo");
if !response.ok {
throw "response status {response.status}";
}
log(response.body);
expect.equal(response.body, "/foo"); // body is expected to be the path
}, deploy: false) as checkName;
test "run() with success" {
let result = check.run();
assert(result.ok);
assert(result.timestamp.length > 0);
log(Json.stringify(result));
}
test "latest() is nil before first run" {
expect.nil(check.latest());
}
test "latest() returns the last check status" {
let result = check.run();
let latest = check.latest();
log(Json.stringify(latest));
expect.equal(latest != nil, true);
expect.equal(result, latest);
}
test "run() with failure" {
assert(check.run().ok); // first run
simulateFailure();
let result = check.run();
assert(!result.ok);
expect.equal(result.error, "response status 404");
expect.equal(check.latest()?.ok, false);
}
let check_id = nodeof(check).id;
test "check name is set" {
expect.equal("check {check_id}", "check {checkName}");
}