-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathcreateServer.test.ts
More file actions
190 lines (146 loc) · 5.6 KB
/
createServer.test.ts
File metadata and controls
190 lines (146 loc) · 5.6 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
178
179
180
181
182
183
184
185
186
187
188
189
190
import createServer from './createServer';
import hasResolved from 'has-resolved';
test('GET /once.txt should return 200 OK', async () => {
const { dispose, port, promises } = await createServer({
playbacks: [{
req: { method: 'GET', url: '/once.txt' },
res: { body: 'OK' }
}]
});
try {
const res1 = await fetch(`http://localhost:${ port }/once.txt`, undefined);
expect(res1).toHaveProperty('ok', true);
expect(await hasResolved(promises[0])).toBeTruthy();
const res2 = await fetch(`http://localhost:${ port }/once.txt`, undefined);
expect(res2).toHaveProperty('status', 404);
} finally {
dispose();
}
});
test('OPTIONS /once.txt should keep return 200 OK until GET /once.txt', async () => {
const { dispose, port, promises } = await createServer({
playbacks: [{
req: { method: 'GET', url: '/once.txt' },
res: { body: 'OK' }
}]
});
try {
const res1 = await fetch(`http://localhost:${ port }/once.txt`, { method: 'OPTIONS' });
expect(res1).toHaveProperty('ok', true);
expect(await hasResolved(promises[0])).toBeFalsy();
const res2 = await fetch(`http://localhost:${ port }/once.txt`, { method: 'OPTIONS' });
expect(res2).toHaveProperty('ok', true);
expect(await hasResolved(promises[0])).toBeFalsy();
const res3 = await fetch(`http://localhost:${ port }/once.txt`, undefined);
expect(res3).toHaveProperty('ok', true);
expect(await hasResolved(promises[0])).toBeTruthy();
const res4 = await fetch(`http://localhost:${ port }/once.txt`, { method: 'OPTIONS' });
expect(res4).toHaveProperty('status', 404);
expect(await hasResolved(promises[0])).toBeTruthy();
} finally {
dispose();
}
});
test('GET should succeed with a strict ordered sequence', async () => {
const { dispose, port, promises } = await createServer({
playbacks: [{
req: { method: 'GET', url: '/1.txt' },
res: { body: '1' }
}, {
req: { method: 'GET', url: '/2.txt' },
res: { body: '2' }
}]
});
try {
const res1 = await fetch(`http://localhost:${ port }/1.txt`, undefined);
expect(res1).toHaveProperty('ok', true);
expect(await res1.text()).toBe('1');
expect(await hasResolved(promises[0])).toBeTruthy();
expect(await hasResolved(promises[1])).toBeFalsy();
const res2 = await fetch(`http://localhost:${ port }/2.txt`, undefined);
expect(res2).toHaveProperty('ok', true);
expect(await res2.text()).toBe('2');
expect(await hasResolved(promises[0])).toBeTruthy();
expect(await hasResolved(promises[1])).toBeTruthy();
const res3 = await fetch(`http://localhost:${ port }/1.txt`, undefined);
expect(res3).toHaveProperty('status', 404);
} finally {
dispose();
}
});
test('GET should fail if out-of-order', async () => {
const { dispose, port, promises } = await createServer({
playbacks: [{
req: { method: 'GET', url: '/1.txt' },
res: { body: '1' }
}, {
req: { method: 'GET', url: '/2.txt' },
res: { body: '2' }
}]
});
try {
const res1 = await fetch(`http://localhost:${ port }/2.txt`, undefined);
expect(res1).toHaveProperty('status', 404);
expect(await hasResolved(promises[0])).toBeFalsy();
expect(await hasResolved(promises[1])).toBeFalsy();
const res2 = await fetch(`http://localhost:${ port }/1.txt`, undefined);
expect(res2).toHaveProperty('ok', true);
expect(await hasResolved(promises[0])).toBeTruthy();
expect(await hasResolved(promises[1])).toBeFalsy();
const res3 = await fetch(`http://localhost:${ port }/2.txt`, undefined);
expect(res3).toHaveProperty('ok', true);
expect(await hasResolved(promises[0])).toBeTruthy();
expect(await hasResolved(promises[1])).toBeTruthy();
const res4 = await fetch(`http://localhost:${ port }/2.txt`, undefined);
expect(res4).toHaveProperty('status', 404);
} finally {
dispose();
}
});
test('GET unordered requests', async () => {
const { dispose, port, promises } = await createServer({
playbacks: [
[
{
req: { method: 'GET', url: '/1a.txt' },
res: { body: '1a' }
}, {
req: { method: 'GET', url: '/1b.txt' },
res: { body: '1b' }
}
],
{
req: { method: 'GET', url: '/2.txt' },
res: { body: '2' }
}
]
});
try {
// 404: We must get either 1a or 1b first
const res1 = await fetch(`http://localhost:${ port }/2.txt`, undefined);
expect(res1).toHaveProperty('status', 404);
expect(await hasResolved(promises[1])).toBeFalsy();
// 200: We get either 1a or 1b
const res2 = await fetch(`http://localhost:${ port }/1b.txt`, undefined);
expect(res2).toHaveProperty('ok', true);
expect(await hasResolved(promises[0][0])).toBeFalsy();
expect(await hasResolved(promises[0][1])).toBeTruthy();
// 404: We must get 1a first
const res3 = await fetch(`http://localhost:${ port }/2.txt`, undefined);
expect(res3).toHaveProperty('status', 404);
// 200: We got 1a
const res4 = await fetch(`http://localhost:${ port }/1a.txt`, undefined);
expect(res4).toHaveProperty('ok', true);
expect(await hasResolved(promises[0][0])).toBeTruthy();
expect(await hasResolved(promises[0][1])).toBeTruthy();
// 200: We got 2
const res5 = await fetch(`http://localhost:${ port }/2.txt`, undefined);
expect(res5).toHaveProperty('ok', true);
expect(await hasResolved(promises[1])).toBeTruthy();
// 404: Playbacks is finished
const res6 = await fetch(`http://localhost:${ port }/2.txt`, undefined);
expect(res6).toHaveProperty('status', 404);
} finally {
dispose();
}
});