-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_test.js
More file actions
177 lines (169 loc) · 6.21 KB
/
client_test.js
File metadata and controls
177 lines (169 loc) · 6.21 KB
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 Client, { isLocalURL } from "./client.js";
import {
assert,
assertEquals,
} from "https://deno.land/std@0.185.0/testing/asserts.ts";
import TestServer from "./internal/testing/server.js";
Deno.test("Client", async (t) => {
const serverOptions = { hostname: "0.0.0.0", port: 3003 };
const serverURL = `http://${serverOptions.hostname}:${serverOptions.port}`;
const server = new TestServer(serverOptions);
const serverShutdown = server.start();
// doTest wraps t.step in order to disable the sanitizeOps and sanitizeResources options by default. These must be
// disabled because the server (in the Deno standard library) leaves a hanging async operation. This is an upstream
// problem. From time to time, these should be re-enabled in case the upstream problem has been resolved.
const doTest = async (name, fn) =>
await t.step({ name, fn, sanitizeOps: false, sanitizeResources: false });
await doTest("can check if localhost", () => {
const testUrls = [
{ url: "http://localhost", condition: true },
{ url: "http://localhost:8080", condition: true },
{ url: "http://applura.site", condition: true },
{ url: "http://applura.site:8080", condition: true },
{ url: "http://applura.app", condition: false },
{ url: "http://local.applura.app", condition: false },
{ url: serverURL, condition: false },
];
testUrls.forEach(({ url, condition }) => {
assert(
isLocalURL(new URL(url)) === condition,
`${url} ${condition ? "isLocal" : "not isLocal"}`,
);
});
});
await doTest("can process HTTP responses", async (t) => {
await t.step("200 OK", async () => {
const client = new Client(serverURL);
const loop = client.start();
server.respondWith(
new Response(
'{"data":{"type":"empty","id":"resource"}}',
{
status: 200,
headers: {
"content-type": "application/vnd.api+json",
},
},
),
);
const { resource, problem } = (await loop.next()).value;
const { status } = client.response();
assertEquals(status, 200);
assertEquals(resource.type, "empty");
assertEquals(problem, undefined);
client.stop();
});
// A 204 No Content response should not trigger a new event loop, but the latest response should be accessible.
await t.step("204 No Content", async (t) => {
const client = new Client(serverURL);
const tests = Object.entries({
"preliminary request": () => {
assertEquals(client.response().status, 200);
assertEquals(client.resource().id, "200 resource");
},
"primary request": () => {
assertEquals(client.response().status, 204);
assertEquals(client.resource().id, "200 resource");
},
"closing request": () => {
assertEquals(client.response().status, 200);
assertEquals(client.resource().id, "another 200 resource");
},
});
server.queueResponses([
new Response('{"data":{"type":"myType","id":"200 resource"}}', {
status: 200,
headers: {
"content-type": "application/vnd.api+json",
},
}),
new Response(null, {
status: 204,
headers: {
"content-type": "application/vnd.api+json",
},
}),
new Response('{"data":{"type":"myType","id":"another 200 resource"}}', {
status: 200,
headers: {
"content-type": "application/vnd.api+json",
},
}),
]);
const performTest = async ([name, test]) => await t.step(name, test);
let testIndex = 0;
let eventCount = 0;
for await (const _ of client.start()) {
eventCount++;
await performTest(tests[testIndex]);
testIndex++;
// Since the 204 response won't advance this loop, its test must be triggered as a special case.
if (tests[testIndex] && tests[testIndex][0] === "primary request") {
await client.follow(serverURL);
await performTest(tests[testIndex]);
testIndex++;
}
if (testIndex === tests.length) {
client.stop();
break;
}
client.follow(serverURL);
}
assertEquals(eventCount, 2);
});
});
await doTest("can process unexpected HTTP responses", async (t) => {
await t.step("unrecognized content-type header", async () => {
const client = new Client(serverURL);
const loop = client.start();
server.respondWith(
new Response(
'{"data":{"type":"myType","id":"bad resource"}}',
{ headers: { "content-type": "foobar" } },
),
);
let { resource, problem } = (await loop.next()).value;
let { status } = client.response();
assertEquals(status, 200);
assertEquals(resource, undefined);
assertEquals(
problem.type,
"https://docs.applura.com/client/v4/errors#UnexpectedContentTypeError",
);
// Get a good response.
server.respondWith(
new Response(
'{"data":{"type":"myType","id":"200 resource"}}',
{ headers: { "content-type": "application/vnd.api+json" } },
),
);
client.follow(serverURL);
({ resource, problem } = (await loop.next()).value);
({ status } = client.response());
assertEquals(status, 200);
assertEquals(resource.id, "200 resource");
// A good response should clear the problem.
assertEquals(problem, undefined);
// Then get another unrecognized content-type response to ensure that the last good resource is still
// available.
server.respondWith(
new Response(
'{"data":{"type":"myType","id":"bad resource"}}',
{ headers: { "content-type": "foobar" } },
),
);
client.follow(serverURL);
({ resource, problem } = (await loop.next()).value);
({ status } = client.response());
assertEquals(status, 200);
assertEquals(resource.id, "200 resource");
assertEquals(
problem.type,
"https://docs.applura.com/client/v4/errors#UnexpectedContentTypeError",
);
client.stop();
});
});
server.stop();
await serverShutdown;
});