|
| 1 | +import { createProxyMiddleware, createApp } from './test-kit'; |
| 2 | +import * as request from 'supertest'; |
| 3 | +import { getLocal, Mockttp } from 'mockttp'; |
| 4 | +import type { Options, Plugin } from '../../src/types'; |
| 5 | + |
| 6 | +describe('E2E Plugins', () => { |
| 7 | + let mockTargetServer: Mockttp; |
| 8 | + |
| 9 | + beforeEach(async () => { |
| 10 | + mockTargetServer = getLocal(); |
| 11 | + await mockTargetServer.start(); |
| 12 | + }); |
| 13 | + |
| 14 | + afterEach(async () => { |
| 15 | + await mockTargetServer.stop(); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should register a plugin and access the http-proxy object', async () => { |
| 19 | + let proxyReqUrl: string; |
| 20 | + let responseStatusCode: number; |
| 21 | + |
| 22 | + mockTargetServer.forGet('/users/1').thenReply(200, '{"userName":"John"}'); |
| 23 | + |
| 24 | + const simplePlugin: Plugin = (proxy) => { |
| 25 | + proxy.on('proxyReq', (proxyReq, req, res, options) => (proxyReqUrl = req.url)); |
| 26 | + proxy.on('proxyRes', (proxyRes, req, res) => (responseStatusCode = proxyRes.statusCode)); |
| 27 | + }; |
| 28 | + |
| 29 | + const config: Options = { |
| 30 | + target: `http://localhost:${mockTargetServer.port}`, |
| 31 | + plugins: [simplePlugin], // register a plugin |
| 32 | + }; |
| 33 | + const proxyMiddleware = createProxyMiddleware(config); |
| 34 | + const app = createApp(proxyMiddleware); |
| 35 | + const agent = request(app); |
| 36 | + |
| 37 | + const response = await agent.get('/users/1').expect(200); |
| 38 | + |
| 39 | + expect(proxyReqUrl).toBe('/users/1'); |
| 40 | + expect(response.text).toBe('{"userName":"John"}'); |
| 41 | + expect(responseStatusCode).toBe(200); |
| 42 | + }); |
| 43 | +}); |
0 commit comments