index.test.ts
This file
Path
src/helper/proxy/index.test.ts
356 lines 路 11.9 KiB 路 TypeScript
Last changed by
chore: let oxfmt sort imports instead of eslint (#5442)Yusuke Wadabefore adoption859e2c2
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357import { proxy } from '.'import { Hono } from '../../hono'
describe('Proxy Middleware', () => { describe('proxy', () => { beforeEach(() => { global.fetch = vi.fn().mockImplementation(async (req) => { if (req.url === 'https://example.com/ok') { return Promise.resolve(new Response('ok')) } else if (req.url === 'https://example.com/disconnect') { const reader = req.body.getReader() let response
req.signal.addEventListener('abort', () => { response = req.signal.reason reader.cancel() })
await reader.read()
return Promise.resolve(new Response(response)) } else if (req.url === 'https://example.com/compressed') { return Promise.resolve( new Response('ok', { headers: { 'Content-Encoding': 'gzip', 'Content-Length': '1', 'Content-Range': 'bytes 0-2/1024', 'X-Response-Id': '456', }, }) ) } else if (req.url === 'https://example.com/uncompressed') { return Promise.resolve( new Response('ok', { headers: { 'Content-Length': '2', 'Content-Range': 'bytes 0-2/1024', 'X-Response-Id': '456', }, }) ) } else if (req.url === 'https://example.com/post' && req.method === 'POST') { return Promise.resolve(new Response(`request body: ${await req.text()}`)) } else if (req.url === 'https://example.com/hop-by-hop') { return Promise.resolve( new Response('ok', { headers: { 'Transfer-Encoding': 'chunked', }, }) ) } else if (req.url === 'https://example.com/connection-listed') { return Promise.resolve( new Response('ok', { headers: { Connection: 'x-connection-scoped, bad name', 'X-Connection-Scoped': 'should not be forwarded', 'X-Normal': 'kept', }, }) ) } else if (req.url === 'https://example.com/set-cookie') { return Promise.resolve( new Response('ok', { headers: { 'Set-Cookie': 'test=123', }, }) ) } return Promise.resolve(new Response('not found', { status: 404 })) }) })
it('compressed', async () => { const app = new Hono() app.get('/proxy/:path', (c) => proxy( new Request(`https://example.com/${c.req.param('path')}`, { headers: { 'X-Request-Id': '123', 'Accept-Encoding': 'gzip', }, }) ) ) const res = await app.request('/proxy/compressed') const req = (global.fetch as ReturnType<typeof vi.fn>).mock.calls[0][0]
expect(req.url).toBe('https://example.com/compressed') expect(req.headers.get('X-Request-Id')).toBe('123') expect(req.headers.get('Accept-Encoding')).toBeNull()
expect(res.status).toBe(200) expect(res.headers.get('X-Response-Id')).toBe('456') expect(res.headers.get('Content-Encoding')).toBeNull() expect(res.headers.get('Content-Length')).toBeNull() expect(res.headers.get('Content-Range')).toBe('bytes 0-2/1024') })
it('uncompressed', async () => { const app = new Hono() app.get('/proxy/:path', (c) => proxy( new Request(`https://example.com/${c.req.param('path')}`, { headers: { 'X-Request-Id': '123', 'Accept-Encoding': 'gzip', }, }) ) ) const res = await app.request('/proxy/uncompressed') const req = (global.fetch as ReturnType<typeof vi.fn>).mock.calls[0][0]
expect(req.url).toBe('https://example.com/uncompressed') expect(req.headers.get('X-Request-Id')).toBe('123') expect(req.headers.get('Accept-Encoding')).toBeNull()
expect(res.status).toBe(200) expect(res.headers.get('X-Response-Id')).toBe('456') expect(res.headers.get('Content-Length')).toBe('2') expect(res.headers.get('Content-Range')).toBe('bytes 0-2/1024') })
it('POST request', async () => { const app = new Hono() app.all('/proxy/:path', (c) => { return proxy(`https://example.com/${c.req.param('path')}`, { ...c.req, headers: { ...c.req.header(), 'X-Request-Id': '123', 'Accept-Encoding': 'gzip', }, }) }) const res = await app.request('/proxy/post', { method: 'POST', body: 'test', }) const req = (global.fetch as ReturnType<typeof vi.fn>).mock.calls[0][0]
expect(req.url).toBe('https://example.com/post')
expect(res.status).toBe(200) expect(await res.text()).toBe('request body: test') })
it('remove hop-by-hop headers', async () => { const app = new Hono() app.get('/proxy/:path', (c) => proxy(`https://example.com/${c.req.param('path')}`, c.req))
const res = await app.request('/proxy/hop-by-hop', { headers: { Host: 'example.com', Connection: 'keep-alive, custom-header', 'Keep-Alive': 'timeout=5, max=1000', 'Proxy-Authorization': 'Basic 123456', 'Custom-Header': 'test', 'Allowed-Custom-Header': 'test', }, }) const req = (global.fetch as ReturnType<typeof vi.fn>).mock.calls[0][0]
expect(req.headers.get('Host')).toBe('example.com') expect(req.headers.get('Connection')).toBeNull() expect(req.headers.get('Keep-Alive')).toBeNull() expect(req.headers.get('Proxy-Authorization')).toBeNull() expect(req.headers.get('Custom-Header')).toBe('test') expect(req.headers.get('Allowed-Custom-Header')).toBe('test')
expect(res.headers.get('Transfer-Encoding')).toBeNull() })
it('remove hop-by-hop headers with strictConnectionProcessing', async () => { const app = new Hono() app.get('/proxy/:path', (c) => proxy(`https://example.com/${c.req.param('path')}`, { ...c.req, strictConnectionProcessing: true, }) )
const res = await app.request('/proxy/hop-by-hop', { headers: { Host: 'example.com', Connection: 'keep-alive, custom-header', 'Keep-Alive': 'timeout=5, max=1000', 'Proxy-Authorization': 'Basic 123456', 'Custom-Header': 'test', 'Allowed-Custom-Header': 'test', }, }) const req = (global.fetch as ReturnType<typeof vi.fn>).mock.calls[0][0]
expect(req.headers.get('Host')).toBe('example.com') expect(req.headers.get('Connection')).toBeNull() expect(req.headers.get('Keep-Alive')).toBeNull() expect(req.headers.get('Proxy-Authorization')).toBeNull() expect(req.headers.get('Custom-Header')).toBeNull() expect(req.headers.get('Allowed-Custom-Header')).toBe('test')
expect(res.headers.get('Transfer-Encoding')).toBeNull() })
it('invalid hop-by-hop headers with strictConnectionProcessing', async () => { const app = new Hono() app.get('/proxy/:path', (c) => proxy(`https://example.com/${c.req.param('path')}`, { ...c.req, strictConnectionProcessing: true, }) )
const res = await app.request('/proxy/hop-by-hop', { headers: { Host: 'example.com', Connection: 'keep-alive, invalid-header invalid-header', 'Keep-Alive': 'timeout=5, max=1000', }, })
expect(res.status).toBe(400) })
it('specify hop-by-hop header by options', async () => { const app = new Hono() app.get('/proxy/:path', (c) => proxy(`https://example.com/${c.req.param('path')}`, { headers: { 'Proxy-Authorization': 'Basic 123456', }, }) )
const res = await app.request('/proxy/hop-by-hop') const req = (global.fetch as ReturnType<typeof vi.fn>).mock.calls[0][0]
expect(req.headers.get('Proxy-Authorization')).toBe('Basic 123456')
expect(res.headers.get('Transfer-Encoding')).toBeNull() })
it('remove response headers listed in the Connection header', async () => { const app = new Hono() app.get('/proxy/:path', (c) => proxy(`https://example.com/${c.req.param('path')}`)) const res = await app.request('/proxy/connection-listed') expect(res.status).toBe(200) expect(res.headers.get('Connection')).toBeNull() expect(res.headers.get('X-Connection-Scoped')).toBeNull() expect(res.headers.get('X-Normal')).toBe('kept') })
it('modify header', async () => { const app = new Hono() app.get('/proxy/:path', (c) => proxy(`https://example.com/${c.req.param('path')}`, { headers: { 'Set-Cookie': 'test=123', }, }).then((res) => { res.headers.delete('Set-Cookie') res.headers.set('X-Response-Id', '456') return res }) ) const res = await app.request('/proxy/set-cookie') expect(res.headers.get('Set-Cookie')).toBeNull() expect(res.headers.get('X-Response-Id')).toBe('456') })
it('does not propagate undefined request headers', async () => { const app = new Hono() app.get('/proxy/:path', (c) => proxy(`https://example.com/${c.req.param('path')}`, { headers: { ...c.req.header(), Authorization: undefined, }, }) ) await app.request('/proxy/ok', { headers: { Authorization: 'Bearer 123', }, }) const req = (global.fetch as ReturnType<typeof vi.fn>).mock.calls[0][0] expect(req.headers.get('Authorization')).toBeNull() })
it('client disconnect', async () => { const app = new Hono() const controller = new AbortController() app.post('/proxy/:path', (c) => proxy(`https://example.com/${c.req.param('path')}`, c.req)) const resPromise = app.request('/proxy/disconnect', { method: 'POST', body: 'test', signal: controller.signal, }) controller.abort('client disconnect') const res = await resPromise expect(await res.text()).toBe('client disconnect') })
it('not found', async () => { const app = new Hono() app.get('/proxy/:path', (c) => proxy(`https://example.com/${c.req.param('path')}`)) const res = await app.request('/proxy/404') expect(res.status).toBe(404) })
it('pass a Request object to proxyInit', async () => { const app = new Hono() app.get('/proxy/:path', (c) => { const req = new Request(c.req.raw, { headers: { 'X-Request-Id': '123', 'Accept-Encoding': 'gzip', }, }) return proxy(`https://example.com/${c.req.param('path')}`, req) }) const res = await app.request('/proxy/compressed') const req = (global.fetch as ReturnType<typeof vi.fn>).mock.calls[0][0]
expect(req.url).toBe('https://example.com/compressed') expect(req.headers.get('X-Request-Id')).toBe('123') expect(req.headers.get('Accept-Encoding')).toBeNull()
expect(res.status).toBe(200) expect(await res.text()).toBe('ok') })
it('Should call the custom fetch method when specified', async () => { const customFetch = vi.fn().mockImplementation(async (req: Request) => { const text = await req.text() return new Response('custom fetch response. message:' + text) }) const app = new Hono() app.post('/', (c) => { return proxy(`https://example.com/`, { customFetch, ...c.req, }) }) const res = await app.request('/', { method: 'POST', body: 'hi', }) expect(res.status).toBe(200) expect(await res.text()).toBe('custom fetch response. message:hi') }) })})