Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than f0b094d point at it.

Merge commit from forkYusuke Wadabefore adoptionf0b094d

line 1why, one step back
1import { proxy } from '.'

chore: let oxfmt sort imports instead of eslint (#5442)Yusuke Wadabefore adoption859e2c2

line 2why, one step back
2import { Hono } from '../../hono'

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 3–52why, one step back
3
4describe('Proxy Middleware', () => {5 describe('proxy', () => {6 beforeEach(() => {7 global.fetch = vi.fn().mockImplementation(async (req) => {8 if (req.url === 'https://example.com/ok') {9 return Promise.resolve(new Response('ok'))10 } else if (req.url === 'https://example.com/disconnect') {11 const reader = req.body.getReader()12 let response1314 req.signal.addEventListener('abort', () => {15 response = req.signal.reason16 reader.cancel()17 })1819 await reader.read()2021 return Promise.resolve(new Response(response))22 } else if (req.url === 'https://example.com/compressed') {23 return Promise.resolve(24 new Response('ok', {25 headers: {26 'Content-Encoding': 'gzip',27 'Content-Length': '1',28 'Content-Range': 'bytes 0-2/1024',29 'X-Response-Id': '456',30 },31 })32 )33 } else if (req.url === 'https://example.com/uncompressed') {34 return Promise.resolve(35 new Response('ok', {36 headers: {37 'Content-Length': '2',38 'Content-Range': 'bytes 0-2/1024',39 'X-Response-Id': '456',40 },41 })42 )43 } else if (req.url === 'https://example.com/post' && req.method === 'POST') {44 return Promise.resolve(new Response(`request body: ${await req.text()}`))45 } else if (req.url === 'https://example.com/hop-by-hop') {46 return Promise.resolve(47 new Response('ok', {48 headers: {49 'Transfer-Encoding': 'chunked',50 },51 })52 )

Merge commit from forkYusuke Wadabefore adoption720b566

lines 53–62why, one step back
53 } else if (req.url === 'https://example.com/connection-listed') {
54 return Promise.resolve(55 new Response('ok', {56 headers: {57 Connection: 'x-connection-scoped, bad name',58 'X-Connection-Scoped': 'should not be forwarded',59 'X-Normal': 'kept',60 },61 })62 )

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 63–243why, one step back
63 } else if (req.url === 'https://example.com/set-cookie') {
64 return Promise.resolve(65 new Response('ok', {66 headers: {67 'Set-Cookie': 'test=123',68 },69 })70 )71 }72 return Promise.resolve(new Response('not found', { status: 404 }))73 })74 })7576 it('compressed', async () => {77 const app = new Hono()78 app.get('/proxy/:path', (c) =>79 proxy(80 new Request(`https://example.com/${c.req.param('path')}`, {81 headers: {82 'X-Request-Id': '123',83 'Accept-Encoding': 'gzip',84 },85 })86 )87 )88 const res = await app.request('/proxy/compressed')89 const req = (global.fetch as ReturnType<typeof vi.fn>).mock.calls[0][0]9091 expect(req.url).toBe('https://example.com/compressed')92 expect(req.headers.get('X-Request-Id')).toBe('123')93 expect(req.headers.get('Accept-Encoding')).toBeNull()9495 expect(res.status).toBe(200)96 expect(res.headers.get('X-Response-Id')).toBe('456')97 expect(res.headers.get('Content-Encoding')).toBeNull()98 expect(res.headers.get('Content-Length')).toBeNull()99 expect(res.headers.get('Content-Range')).toBe('bytes 0-2/1024')100 })101102 it('uncompressed', async () => {103 const app = new Hono()104 app.get('/proxy/:path', (c) =>105 proxy(106 new Request(`https://example.com/${c.req.param('path')}`, {107 headers: {108 'X-Request-Id': '123',109 'Accept-Encoding': 'gzip',110 },111 })112 )113 )114 const res = await app.request('/proxy/uncompressed')115 const req = (global.fetch as ReturnType<typeof vi.fn>).mock.calls[0][0]116117 expect(req.url).toBe('https://example.com/uncompressed')118 expect(req.headers.get('X-Request-Id')).toBe('123')119 expect(req.headers.get('Accept-Encoding')).toBeNull()120121 expect(res.status).toBe(200)122 expect(res.headers.get('X-Response-Id')).toBe('456')123 expect(res.headers.get('Content-Length')).toBe('2')124 expect(res.headers.get('Content-Range')).toBe('bytes 0-2/1024')125 })126127 it('POST request', async () => {128 const app = new Hono()129 app.all('/proxy/:path', (c) => {130 return proxy(`https://example.com/${c.req.param('path')}`, {131 ...c.req,132 headers: {133 ...c.req.header(),134 'X-Request-Id': '123',135 'Accept-Encoding': 'gzip',136 },137 })138 })139 const res = await app.request('/proxy/post', {140 method: 'POST',141 body: 'test',142 })143 const req = (global.fetch as ReturnType<typeof vi.fn>).mock.calls[0][0]144145 expect(req.url).toBe('https://example.com/post')146147 expect(res.status).toBe(200)148 expect(await res.text()).toBe('request body: test')149 })150151 it('remove hop-by-hop headers', async () => {152 const app = new Hono()153 app.get('/proxy/:path', (c) => proxy(`https://example.com/${c.req.param('path')}`, c.req))154155 const res = await app.request('/proxy/hop-by-hop', {156 headers: {157 Host: 'example.com',158 Connection: 'keep-alive, custom-header',159 'Keep-Alive': 'timeout=5, max=1000',160 'Proxy-Authorization': 'Basic 123456',161 'Custom-Header': 'test',162 'Allowed-Custom-Header': 'test',163 },164 })165 const req = (global.fetch as ReturnType<typeof vi.fn>).mock.calls[0][0]166167 expect(req.headers.get('Host')).toBe('example.com')168 expect(req.headers.get('Connection')).toBeNull()169 expect(req.headers.get('Keep-Alive')).toBeNull()170 expect(req.headers.get('Proxy-Authorization')).toBeNull()171 expect(req.headers.get('Custom-Header')).toBe('test')172 expect(req.headers.get('Allowed-Custom-Header')).toBe('test')173174 expect(res.headers.get('Transfer-Encoding')).toBeNull()175 })176177 it('remove hop-by-hop headers with strictConnectionProcessing', async () => {178 const app = new Hono()179 app.get('/proxy/:path', (c) =>180 proxy(`https://example.com/${c.req.param('path')}`, {181 ...c.req,182 strictConnectionProcessing: true,183 })184 )185186 const res = await app.request('/proxy/hop-by-hop', {187 headers: {188 Host: 'example.com',189 Connection: 'keep-alive, custom-header',190 'Keep-Alive': 'timeout=5, max=1000',191 'Proxy-Authorization': 'Basic 123456',192 'Custom-Header': 'test',193 'Allowed-Custom-Header': 'test',194 },195 })196 const req = (global.fetch as ReturnType<typeof vi.fn>).mock.calls[0][0]197198 expect(req.headers.get('Host')).toBe('example.com')199 expect(req.headers.get('Connection')).toBeNull()200 expect(req.headers.get('Keep-Alive')).toBeNull()201 expect(req.headers.get('Proxy-Authorization')).toBeNull()202 expect(req.headers.get('Custom-Header')).toBeNull()203 expect(req.headers.get('Allowed-Custom-Header')).toBe('test')204205 expect(res.headers.get('Transfer-Encoding')).toBeNull()206 })207208 it('invalid hop-by-hop headers with strictConnectionProcessing', async () => {209 const app = new Hono()210 app.get('/proxy/:path', (c) =>211 proxy(`https://example.com/${c.req.param('path')}`, {212 ...c.req,213 strictConnectionProcessing: true,214 })215 )216217 const res = await app.request('/proxy/hop-by-hop', {218 headers: {219 Host: 'example.com',220 Connection: 'keep-alive, invalid-header invalid-header',221 'Keep-Alive': 'timeout=5, max=1000',222 },223 })224225 expect(res.status).toBe(400)226 })227228 it('specify hop-by-hop header by options', async () => {229 const app = new Hono()230 app.get('/proxy/:path', (c) =>231 proxy(`https://example.com/${c.req.param('path')}`, {232 headers: {233 'Proxy-Authorization': 'Basic 123456',234 },235 })236 )237238 const res = await app.request('/proxy/hop-by-hop')239 const req = (global.fetch as ReturnType<typeof vi.fn>).mock.calls[0][0]240241 expect(req.headers.get('Proxy-Authorization')).toBe('Basic 123456')242243 expect(res.headers.get('Transfer-Encoding')).toBeNull()

Merge commit from forkYusuke Wadabefore adoption720b566

lines 244–253why, one step back
244 })
245246 it('remove response headers listed in the Connection header', async () => {247 const app = new Hono()248 app.get('/proxy/:path', (c) => proxy(`https://example.com/${c.req.param('path')}`))249 const res = await app.request('/proxy/connection-listed')250 expect(res.status).toBe(200)251 expect(res.headers.get('Connection')).toBeNull()252 expect(res.headers.get('X-Connection-Scoped')).toBeNull()253 expect(res.headers.get('X-Normal')).toBe('kept')

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 254–356why, one step back
254 })
255256 it('modify header', async () => {257 const app = new Hono()258 app.get('/proxy/:path', (c) =>259 proxy(`https://example.com/${c.req.param('path')}`, {260 headers: {261 'Set-Cookie': 'test=123',262 },263 }).then((res) => {264 res.headers.delete('Set-Cookie')265 res.headers.set('X-Response-Id', '456')266 return res267 })268 )269 const res = await app.request('/proxy/set-cookie')270 expect(res.headers.get('Set-Cookie')).toBeNull()271 expect(res.headers.get('X-Response-Id')).toBe('456')272 })273274 it('does not propagate undefined request headers', async () => {275 const app = new Hono()276 app.get('/proxy/:path', (c) =>277 proxy(`https://example.com/${c.req.param('path')}`, {278 headers: {279 ...c.req.header(),280 Authorization: undefined,281 },282 })283 )284 await app.request('/proxy/ok', {285 headers: {286 Authorization: 'Bearer 123',287 },288 })289 const req = (global.fetch as ReturnType<typeof vi.fn>).mock.calls[0][0]290 expect(req.headers.get('Authorization')).toBeNull()291 })292293 it('client disconnect', async () => {294 const app = new Hono()295 const controller = new AbortController()296 app.post('/proxy/:path', (c) => proxy(`https://example.com/${c.req.param('path')}`, c.req))297 const resPromise = app.request('/proxy/disconnect', {298 method: 'POST',299 body: 'test',300 signal: controller.signal,301 })302 controller.abort('client disconnect')303 const res = await resPromise304 expect(await res.text()).toBe('client disconnect')305 })306307 it('not found', async () => {308 const app = new Hono()309 app.get('/proxy/:path', (c) => proxy(`https://example.com/${c.req.param('path')}`))310 const res = await app.request('/proxy/404')311 expect(res.status).toBe(404)312 })313314 it('pass a Request object to proxyInit', async () => {315 const app = new Hono()316 app.get('/proxy/:path', (c) => {317 const req = new Request(c.req.raw, {318 headers: {319 'X-Request-Id': '123',320 'Accept-Encoding': 'gzip',321 },322 })323 return proxy(`https://example.com/${c.req.param('path')}`, req)324 })325 const res = await app.request('/proxy/compressed')326 const req = (global.fetch as ReturnType<typeof vi.fn>).mock.calls[0][0]327328 expect(req.url).toBe('https://example.com/compressed')329 expect(req.headers.get('X-Request-Id')).toBe('123')330 expect(req.headers.get('Accept-Encoding')).toBeNull()331332 expect(res.status).toBe(200)333 expect(await res.text()).toBe('ok')334 })335336 it('Should call the custom fetch method when specified', async () => {337 const customFetch = vi.fn().mockImplementation(async (req: Request) => {338 const text = await req.text()339 return new Response('custom fetch response. message:' + text)340 })341 const app = new Hono()342 app.post('/', (c) => {343 return proxy(`https://example.com/`, {344 customFetch,345 ...c.req,346 })347 })348 const res = await app.request('/', {349 method: 'POST',350 body: 'hi',351 })352 expect(res.status).toBe(200)353 expect(await res.text()).toBe('custom fetch response. message:hi')354 })355 })356})