|
| 1 | +import type { Mockttp } from 'mockttp'; |
| 2 | +import { getLocal } from 'mockttp'; |
| 3 | +import request from 'supertest'; |
| 4 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 5 | + |
| 6 | +import { createApp, createProxyMiddleware } from './test-kit.js'; |
| 7 | + |
| 8 | +describe('ipv6 integration', () => { |
| 9 | + let targetServer: Mockttp; |
| 10 | + |
| 11 | + beforeEach(async () => { |
| 12 | + targetServer = getLocal(); |
| 13 | + await targetServer.start(); |
| 14 | + }); |
| 15 | + |
| 16 | + afterEach(async () => { |
| 17 | + await targetServer.stop(); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should proxy to ipv6 target using bracket notation with port', async () => { |
| 21 | + await targetServer.forGet('/api').thenCallback((req) => ({ |
| 22 | + statusCode: 200, |
| 23 | + body: req.path, |
| 24 | + })); |
| 25 | + |
| 26 | + const proxy = createProxyMiddleware({ |
| 27 | + changeOrigin: true, |
| 28 | + target: `http://[::1]:${targetServer.port}`, |
| 29 | + }); |
| 30 | + |
| 31 | + const app = createApp(proxy); |
| 32 | + const response = await request(app).get('/api').expect(200); |
| 33 | + |
| 34 | + expect(response.text).toBe('/api'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should proxy to unspecified ipv6 target using bracket notation with port', async () => { |
| 38 | + await targetServer.forGet('/api').thenCallback((req) => ({ |
| 39 | + statusCode: 200, |
| 40 | + body: req.path, |
| 41 | + })); |
| 42 | + |
| 43 | + const proxy = createProxyMiddleware({ |
| 44 | + changeOrigin: true, |
| 45 | + target: `http://[::]:${targetServer.port}`, |
| 46 | + }); |
| 47 | + |
| 48 | + const app = createApp(proxy); |
| 49 | + const response = await request(app).get('/api').expect(200); |
| 50 | + |
| 51 | + expect(response.text).toBe('/api'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should proxy to ipv6 target and preserve query params', async () => { |
| 55 | + let receivedPath: string | undefined; |
| 56 | + |
| 57 | + await targetServer.forGet('/api').thenCallback((req) => { |
| 58 | + receivedPath = req.path; |
| 59 | + return { |
| 60 | + statusCode: 200, |
| 61 | + body: req.url.includes('?') ? req.url.split('?')[1] : '', |
| 62 | + }; |
| 63 | + }); |
| 64 | + |
| 65 | + const proxy = createProxyMiddleware({ |
| 66 | + changeOrigin: true, |
| 67 | + target: `http://[::1]:${targetServer.port}`, |
| 68 | + }); |
| 69 | + |
| 70 | + const app = createApp(proxy); |
| 71 | + const response = await request(app).get('/api?foo=bar&baz=qux').expect(200); |
| 72 | + |
| 73 | + expect(receivedPath).toBe('/api?foo=bar&baz=qux'); |
| 74 | + expect(response.text).toBe('foo=bar&baz=qux'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should proxy to ipv6 target and preserve search params with special characters', async () => { |
| 78 | + let receivedPath: string | undefined; |
| 79 | + |
| 80 | + await targetServer.forGet('/api').thenCallback((req) => { |
| 81 | + receivedPath = req.path; |
| 82 | + return { |
| 83 | + statusCode: 200, |
| 84 | + body: req.url.includes('?') ? req.url.split('?')[1] : '', |
| 85 | + }; |
| 86 | + }); |
| 87 | + |
| 88 | + const proxy = createProxyMiddleware({ |
| 89 | + changeOrigin: true, |
| 90 | + target: `http://[::1]:${targetServer.port}`, |
| 91 | + }); |
| 92 | + |
| 93 | + const app = createApp(proxy); |
| 94 | + const response = await request(app).get('/api?q=hello%20world&page=1').expect(200); |
| 95 | + |
| 96 | + expect(receivedPath).toBe('/api?q=hello%20world&page=1'); |
| 97 | + expect(response.text).toBe('q=hello%20world&page=1'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should forward requests to ipv6 target using forward option', async () => { |
| 101 | + let forwardedPath: string | undefined; |
| 102 | + |
| 103 | + await targetServer.forPost('/api').thenCallback((req) => { |
| 104 | + forwardedPath = req.path; |
| 105 | + return { statusCode: 200 }; |
| 106 | + }); |
| 107 | + |
| 108 | + const proxy = createProxyMiddleware({ |
| 109 | + changeOrigin: true, |
| 110 | + target: `http://[::1]:${targetServer.port}`, |
| 111 | + forward: `http://[::1]:${targetServer.port}`, |
| 112 | + }); |
| 113 | + |
| 114 | + const app = createApp(proxy); |
| 115 | + await request(app).post('/api').expect(200); |
| 116 | + |
| 117 | + expect(forwardedPath).toBe('/api'); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should proxy to ipv6 target resolved via router function (no static target)', async () => { |
| 121 | + await targetServer.forGet('/api').thenCallback((req) => ({ |
| 122 | + statusCode: 200, |
| 123 | + body: req.path, |
| 124 | + })); |
| 125 | + |
| 126 | + const proxy = createProxyMiddleware({ |
| 127 | + changeOrigin: true, |
| 128 | + target: 'http://example.com', // dummy target, will be overridden by router |
| 129 | + router: () => `http://[::1]:${targetServer.port}`, |
| 130 | + }); |
| 131 | + |
| 132 | + const app = createApp(proxy); |
| 133 | + const response = await request(app).get('/api').expect(200); |
| 134 | + |
| 135 | + expect(response.text).toBe('/api'); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should proxy to ipv6 target resolved via async router function', async () => { |
| 139 | + await targetServer.forGet('/api').thenCallback((req) => ({ |
| 140 | + statusCode: 200, |
| 141 | + body: req.path, |
| 142 | + })); |
| 143 | + |
| 144 | + const proxy = createProxyMiddleware({ |
| 145 | + changeOrigin: true, |
| 146 | + target: 'http://example.com', // dummy target, will be overridden by router |
| 147 | + router: async () => `http://[::1]:${targetServer.port}`, |
| 148 | + }); |
| 149 | + |
| 150 | + const app = createApp(proxy); |
| 151 | + const response = await request(app).get('/api').expect(200); |
| 152 | + |
| 153 | + expect(response.text).toBe('/api'); |
| 154 | + }); |
| 155 | + |
| 156 | + it('should proxy to ipv6 target with base path and auth option', async () => { |
| 157 | + let receivedPath: string | undefined; |
| 158 | + let authorizationHeader: string | undefined; |
| 159 | + |
| 160 | + await targetServer.forGet('/api').thenCallback((req) => { |
| 161 | + receivedPath = req.path; |
| 162 | + const authHeader = req.headers.authorization; |
| 163 | + authorizationHeader = Array.isArray(authHeader) ? authHeader[0] : authHeader; |
| 164 | + |
| 165 | + return { |
| 166 | + statusCode: 200, |
| 167 | + }; |
| 168 | + }); |
| 169 | + |
| 170 | + const proxy = createProxyMiddleware({ |
| 171 | + changeOrigin: true, |
| 172 | + target: `http://[::1]:${targetServer.port}/api`, |
| 173 | + auth: 'user:pass', |
| 174 | + }); |
| 175 | + |
| 176 | + const app = createApp(proxy); |
| 177 | + await request(app).get('/').expect(200); |
| 178 | + |
| 179 | + expect(receivedPath).toBe('/api'); |
| 180 | + expect(authorizationHeader).toBe('Basic dXNlcjpwYXNz'); // cspell:disable-line |
| 181 | + }); |
| 182 | +}); |
0 commit comments