|
| 1 | +import * as http from 'http'; |
| 2 | +import { createProxyServer } from 'http-proxy'; |
| 3 | + |
| 4 | +import { removeSecurityFromCookie } from './removeSecurityFromCookie'; |
| 5 | + |
| 6 | +let sourceServer: http.Server; |
| 7 | +let proxyServer: http.Server; |
| 8 | + |
| 9 | +const proxyPort = 2370; |
| 10 | +const sourcePort = 2371; |
| 11 | +const proxyUrl = `http://127.0.0.1:${proxyPort}`; |
| 12 | +const sourceUrl = `http://127.0.0.1:${sourcePort}`; |
| 13 | + |
| 14 | +afterEach(() => { |
| 15 | + sourceServer.close(); |
| 16 | + proxyServer.close(); |
| 17 | +}); |
| 18 | + |
| 19 | +test('removeSecurityFromCookie', () => { |
| 20 | + return new Promise(done => { |
| 21 | + const proxy = createProxyServer({ target: sourceUrl }); |
| 22 | + |
| 23 | + proxy.on('proxyRes', (proxyRes, req, res) => { |
| 24 | + removeSecurityFromCookie(proxyRes, req, res); |
| 25 | + expect(proxyRes.headers['set-cookie']).toEqual(['test']); |
| 26 | + res.end('Response'); |
| 27 | + }); |
| 28 | + |
| 29 | + function proxyRequestHandler(_req: http.IncomingMessage, _res: http.ServerResponse) { |
| 30 | + proxy.web(_req, _res); |
| 31 | + } |
| 32 | + function sourceRequestHandler(_req: http.IncomingMessage, _res: http.ServerResponse) { |
| 33 | + _res.writeHead(200, { 'Content-Type': 'text/plain', 'set-cookie': 'test; secure' }); |
| 34 | + _res.end('Response'); |
| 35 | + } |
| 36 | + |
| 37 | + proxyServer = http.createServer(proxyRequestHandler).listen(proxyPort); |
| 38 | + sourceServer = http.createServer(sourceRequestHandler).listen(sourcePort); |
| 39 | + |
| 40 | + http |
| 41 | + .request(proxyUrl, () => { |
| 42 | + done(1); |
| 43 | + }) |
| 44 | + .end(); |
| 45 | + }); |
| 46 | +}); |
0 commit comments