Skip to content

Commit 35ac1db

Browse files
authored
test(mockttp): fix deprecated functions (chimurai#772)
1 parent 2e5ccac commit 35ac1db

File tree

3 files changed

+32
-30
lines changed

3 files changed

+32
-30
lines changed

test/e2e/http-proxy-middleware.spec.ts

+21-21
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ describe('E2E http-proxy-middleware', () => {
6565
});
6666

6767
it('should have response body: "HELLO WEB"', async () => {
68-
await mockTargetServer.get('/api').thenReply(200, 'HELLO WEB');
68+
await mockTargetServer.forGet('/api').thenReply(200, 'HELLO WEB');
6969
const response = await agent.get(`/api`).expect(200);
7070
expect(response.text).toBe('HELLO WEB');
7171
});
7272

7373
it('should have proxied the uri-path and uri-query, but not the uri-hash', async () => {
7474
await mockTargetServer
75-
.get('/api/b/c/dp')
75+
.forGet('/api/b/c/dp')
7676
.withExactQuery('?q=1&r=[2,3]')
7777
.thenReply(200, 'OK');
7878

@@ -99,8 +99,8 @@ describe('E2E http-proxy-middleware', () => {
9999
)
100100
);
101101

102-
await mockTargetServer.post('/api').thenCallback((req) => {
103-
expect(req.body.text).toBe('foo=bar&bar=baz');
102+
await mockTargetServer.forPost('/api').thenCallback(async (req) => {
103+
expect(await req.body.getText()).toBe('foo=bar&bar=baz');
104104
return { status: 200 };
105105
});
106106
await agent.post('/api').send('foo=bar').send('bar=baz').expect(200);
@@ -120,8 +120,8 @@ describe('E2E http-proxy-middleware', () => {
120120
)
121121
);
122122

123-
await mockTargetServer.post('/api').thenCallback((req) => {
124-
expect(req.body.json).toEqual({ foo: 'bar', bar: 'baz', doubleByte: '文' });
123+
await mockTargetServer.forPost('/api').thenCallback(async (req) => {
124+
expect(await req.body.getJson()).toEqual({ foo: 'bar', bar: 'baz', doubleByte: '文' });
125125
return { status: 200 };
126126
});
127127
await agent.post('/api').send({ foo: 'bar', bar: 'baz', doubleByte: '文' }).expect(200);
@@ -143,7 +143,7 @@ describe('E2E http-proxy-middleware', () => {
143143
)
144144
);
145145

146-
await mockTargetServer.get('/api/b/c/d').thenReply(200, 'HELLO WEB');
146+
await mockTargetServer.forGet('/api/b/c/d').thenReply(200, 'HELLO WEB');
147147
const response = await agent.get(`/api/b/c/d`).expect(200);
148148
expect(response.text).toBe('HELLO WEB');
149149
});
@@ -162,7 +162,7 @@ describe('E2E http-proxy-middleware', () => {
162162
)
163163
);
164164

165-
await mockTargetServer.get('/api/b/c/d').thenReply(200, 'HELLO WEB');
165+
await mockTargetServer.forGet('/api/b/c/d').thenReply(200, 'HELLO WEB');
166166
const response = await agent.get(`/api/b/c/d`).expect(404);
167167
expect(response.status).toBe(404);
168168
});
@@ -181,13 +181,13 @@ describe('E2E http-proxy-middleware', () => {
181181
});
182182

183183
it('should proxy to path /api', async () => {
184-
await mockTargetServer.get(/\/api\/.+/).thenReply(200, 'HELLO /API');
184+
await mockTargetServer.forGet(/\/api\/.+/).thenReply(200, 'HELLO /API');
185185
const response = await agent.get(`/api/b/c/d`).expect(200);
186186
expect(response.text).toBe('HELLO /API');
187187
});
188188

189189
it('should proxy to path /ajax', async () => {
190-
await mockTargetServer.get(/\/ajax\/.+/).thenReply(200, 'HELLO /AJAX');
190+
await mockTargetServer.forGet(/\/ajax\/.+/).thenReply(200, 'HELLO /AJAX');
191191
const response = await agent.get(`/ajax/b/c/d`).expect(200);
192192
expect(response.text).toBe('HELLO /AJAX');
193193
});
@@ -211,7 +211,7 @@ describe('E2E http-proxy-middleware', () => {
211211
});
212212

213213
it('should proxy to path', async () => {
214-
await mockTargetServer.get(/\/api\/.+/).thenReply(200, 'HELLO /api');
214+
await mockTargetServer.forGet(/\/api\/.+/).thenReply(200, 'HELLO /api');
215215
const response = await agent.get(`/api/b/c/d`).expect(200);
216216
expect(response.text).toBe('HELLO /api');
217217
});
@@ -230,13 +230,13 @@ describe('E2E http-proxy-middleware', () => {
230230
});
231231

232232
it('should proxy to paths ending with *.html', async () => {
233-
await mockTargetServer.get(/.+html$/).thenReply(200, 'HELLO .html');
233+
await mockTargetServer.forGet(/.+html$/).thenReply(200, 'HELLO .html');
234234
const response = await agent.get(`/api/some/endpoint/index.html`).expect(200);
235235
expect(response.text).toBe('HELLO .html');
236236
});
237237

238238
it('should not proxy to paths ending with *.json', async () => {
239-
await mockTargetServer.get(/.+json$/).thenReply(200, 'HELLO .html');
239+
await mockTargetServer.forGet(/.+json$/).thenReply(200, 'HELLO .html');
240240
const response = await agent.get(`/api/some/endpoint/data.json`).expect(404);
241241
expect(response.status).toBe(404);
242242
});
@@ -258,7 +258,7 @@ describe('E2E http-proxy-middleware', () => {
258258
it('should send request header "host" to target server', async () => {
259259
let completedRequest: CompletedRequest;
260260

261-
await mockTargetServer.get().thenCallback((req) => {
261+
await mockTargetServer.forGet().thenCallback((req) => {
262262
completedRequest = req;
263263
return { statusCode: 200, body: 'OK' };
264264
});
@@ -337,13 +337,13 @@ describe('E2E http-proxy-middleware', () => {
337337
});
338338

339339
it('should add `x-added` as custom header to response"', async () => {
340-
await mockTargetServer.get().thenReply(200, 'HELLO .html');
340+
await mockTargetServer.forGet().thenReply(200, 'HELLO .html');
341341
const response = await agent.get(`/api/some/endpoint/index.html`).expect(200);
342342
expect(response.header['x-added']).toBe('foobar');
343343
});
344344

345345
it('should remove `x-removed` field from response header"', async () => {
346-
await mockTargetServer.get().thenCallback((req) => {
346+
await mockTargetServer.forGet().thenCallback((req) => {
347347
return {
348348
statusCode: 200,
349349
headers: {
@@ -375,7 +375,7 @@ describe('E2E http-proxy-middleware', () => {
375375

376376
it('should add `x-added` as custom header to request"', async () => {
377377
let completedRequest: CompletedRequest;
378-
await mockTargetServer.get().thenCallback((req) => {
378+
await mockTargetServer.forGet().thenCallback((req) => {
379379
completedRequest = req;
380380
return { statusCode: 200 };
381381
});
@@ -402,13 +402,13 @@ describe('E2E http-proxy-middleware', () => {
402402
});
403403

404404
it('should have rewritten path from "/api/foo/bar" to "/rest/foo/bar"', async () => {
405-
await mockTargetServer.get('/rest/foo/bar').thenReply(200, 'HELLO /rest/foo/bar');
405+
await mockTargetServer.forGet('/rest/foo/bar').thenReply(200, 'HELLO /rest/foo/bar');
406406
const response = await agent.get(`/api/foo/bar`).expect(200);
407407
expect(response.text).toBe('HELLO /rest/foo/bar');
408408
});
409409

410410
it('should have removed path from "/remove/api/lipsum" to "/api/lipsum"', async () => {
411-
await mockTargetServer.get('/api/lipsum').thenReply(200, 'HELLO /api/lipsum');
411+
await mockTargetServer.forGet('/api/lipsum').thenReply(200, 'HELLO /api/lipsum');
412412
const response = await agent.get(`/remove/api/lipsum`).expect(200);
413413
expect(response.text).toBe('HELLO /api/lipsum');
414414
});
@@ -425,7 +425,7 @@ describe('E2E http-proxy-middleware', () => {
425425
});
426426

427427
it('should proxy to target with the baseUrl', async () => {
428-
await mockTargetServer.get('/api/foo/bar').thenReply(200, 'HELLO /api/foo/bar');
428+
await mockTargetServer.forGet('/api/foo/bar').thenReply(200, 'HELLO /api/foo/bar');
429429
const response = await agent.get(`/api/foo/bar`).expect(200);
430430
expect(response.text).toBe('HELLO /api/foo/bar');
431431
});
@@ -454,7 +454,7 @@ describe('E2E http-proxy-middleware', () => {
454454
});
455455

456456
it('should have logged messages', async () => {
457-
await mockTargetServer.get('/api/foo/bar').thenReply(200);
457+
await mockTargetServer.forGet('/api/foo/bar').thenReply(200);
458458
await agent.get(`/api/foo/bar`).expect(200);
459459

460460
expect(logMessages).not.toBeUndefined();

test/e2e/path-rewriter.spec.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ describe('E2E pathRewrite', () => {
1616

1717
describe('Rewrite paths with rules table', () => {
1818
it('should remove "/foobar" from path', async () => {
19-
mockTargetServer.get('/api/lorum/ipsum').thenReply(200, '/API RESPONSE AFTER PATH REWRITE');
19+
mockTargetServer
20+
.forGet('/api/lorum/ipsum')
21+
.thenReply(200, '/API RESPONSE AFTER PATH REWRITE');
2022

2123
const agent = request(
2224
createApp(
@@ -38,7 +40,7 @@ describe('E2E pathRewrite', () => {
3840
describe('Rewrite paths with function', () => {
3941
it('should remove "/foobar" from path', async () => {
4042
mockTargetServer
41-
.get('/api/lorum/ipsum')
43+
.forGet('/api/lorum/ipsum')
4244
.thenReply(200, '/API RESPONSE AFTER PATH REWRITE FUNCTION');
4345

4446
const agent = request(
@@ -60,7 +62,7 @@ describe('E2E pathRewrite', () => {
6062
describe('Rewrite paths with function which return undefined', () => {
6163
it('should proxy with requested path', async () => {
6264
mockTargetServer
63-
.get('/api/lorum/ipsum')
65+
.forGet('/api/lorum/ipsum')
6466
.thenReply(200, '/API RESPONSE AFTER PATH REWRITE FUNCTION');
6567

6668
const agent = request(

test/e2e/router.spec.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,23 @@ describe('E2E router', () => {
2727
targetPortC = await getPort();
2828

2929
await targetServerA
30-
.anyRequest()
30+
.forAnyRequest()
3131
.thenPassThrough({ ignoreHostCertificateErrors: ['localhost'] });
3232
await targetServerB
33-
.anyRequest()
33+
.forAnyRequest()
3434
.thenPassThrough({ ignoreHostCertificateErrors: ['localhost'] });
3535
await targetServerC
36-
.anyRequest()
36+
.forAnyRequest()
3737
.thenPassThrough({ ignoreHostCertificateErrors: ['localhost'] });
3838

3939
await targetServerA
40-
.anyRequest()
40+
.forAnyRequest()
4141
.thenCallback(({ protocol }) => ({ body: protocol === 'https' ? 'A' : 'NOT HTTPS A' }));
4242
await targetServerB
43-
.anyRequest()
43+
.forAnyRequest()
4444
.thenCallback(({ protocol }) => ({ body: protocol === 'https' ? 'B' : 'NOT HTTPS B' }));
4545
await targetServerC
46-
.anyRequest()
46+
.forAnyRequest()
4747
.thenCallback(({ protocol }) => ({ body: protocol === 'https' ? 'C' : 'NOT HTTPS C' }));
4848

4949
await targetServerA.start(targetPortA);

0 commit comments

Comments
 (0)