Skip to content

Commit

Permalink
test(mockttp): fix deprecated functions
Browse files Browse the repository at this point in the history
  • Loading branch information
chimurai committed Apr 23, 2022
1 parent 2e5ccac commit c5870b0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 30 deletions.
42 changes: 21 additions & 21 deletions test/e2e/http-proxy-middleware.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ describe('E2E http-proxy-middleware', () => {
});

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

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

Expand All @@ -99,8 +99,8 @@ describe('E2E http-proxy-middleware', () => {
)
);

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

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

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

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

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

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

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

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

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

await mockTargetServer.get().thenCallback((req) => {
await mockTargetServer.forGet().thenCallback((req) => {
completedRequest = req;
return { statusCode: 200, body: 'OK' };
});
Expand Down Expand Up @@ -337,13 +337,13 @@ describe('E2E http-proxy-middleware', () => {
});

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

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

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

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

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

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

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

expect(logMessages).not.toBeUndefined();
Expand Down
8 changes: 5 additions & 3 deletions test/e2e/path-rewriter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ describe('E2E pathRewrite', () => {

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

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

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

const agent = request(
Expand Down
12 changes: 6 additions & 6 deletions test/e2e/router.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,23 @@ describe('E2E router', () => {
targetPortC = await getPort();

await targetServerA
.anyRequest()
.forAnyRequest()
.thenPassThrough({ ignoreHostCertificateErrors: ['localhost'] });
await targetServerB
.anyRequest()
.forAnyRequest()
.thenPassThrough({ ignoreHostCertificateErrors: ['localhost'] });
await targetServerC
.anyRequest()
.forAnyRequest()
.thenPassThrough({ ignoreHostCertificateErrors: ['localhost'] });

await targetServerA
.anyRequest()
.forAnyRequest()
.thenCallback(({ protocol }) => ({ body: protocol === 'https' ? 'A' : 'NOT HTTPS A' }));
await targetServerB
.anyRequest()
.forAnyRequest()
.thenCallback(({ protocol }) => ({ body: protocol === 'https' ? 'B' : 'NOT HTTPS B' }));
await targetServerC
.anyRequest()
.forAnyRequest()
.thenCallback(({ protocol }) => ({ body: protocol === 'https' ? 'C' : 'NOT HTTPS C' }));

await targetServerA.start(targetPortA);
Expand Down

0 comments on commit c5870b0

Please sign in to comment.