|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +import { createFetchHandler } from './fetch-handler'; |
| 4 | + |
| 5 | +async function dispatch(environment: string | undefined, request: Request) { |
| 6 | + let receivedRequest: Request | undefined; |
| 7 | + |
| 8 | + const fetch = createFetchHandler(environment, (request: Request) => { |
| 9 | + receivedRequest = request; |
| 10 | + return new Response(); |
| 11 | + }); |
| 12 | + |
| 13 | + await fetch(request); |
| 14 | + |
| 15 | + if (!receivedRequest) { |
| 16 | + throw new Error('Expected the wrapped fetch to be called'); |
| 17 | + } |
| 18 | + |
| 19 | + return receivedRequest; |
| 20 | +} |
| 21 | + |
| 22 | +describe('createFetchHandler', () => { |
| 23 | + for (const environment of ['staging', 'production']) { |
| 24 | + it(`should force an https request URL in ${environment} when x-forwarded-proto is missing`, async () => { |
| 25 | + const request = await dispatch( |
| 26 | + environment, |
| 27 | + new Request('http://example.com/foo'), |
| 28 | + ); |
| 29 | + |
| 30 | + expect(request.url).toBe('https://example.com/foo'); |
| 31 | + }); |
| 32 | + |
| 33 | + it(`should force an https request URL in ${environment} when x-forwarded-proto is http`, async () => { |
| 34 | + const request = await dispatch( |
| 35 | + environment, |
| 36 | + new Request('http://example.com/foo', { |
| 37 | + headers: { |
| 38 | + 'x-forwarded-proto': 'http', |
| 39 | + }, |
| 40 | + }), |
| 41 | + ); |
| 42 | + |
| 43 | + expect(request.url).toBe('https://example.com/foo'); |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + it('should force an https request URL when NODE_ENV is unset or unrecognised', async () => { |
| 48 | + for (const environment of [undefined, '', 'prod']) { |
| 49 | + const request = await dispatch( |
| 50 | + environment, |
| 51 | + new Request('http://example.com/foo'), |
| 52 | + ); |
| 53 | + |
| 54 | + expect(request.url).toBe('https://example.com/foo'); |
| 55 | + } |
| 56 | + }); |
| 57 | + |
| 58 | + for (const environment of ['development', 'testing']) { |
| 59 | + it(`should keep an http request URL in ${environment}`, async () => { |
| 60 | + const request = await dispatch( |
| 61 | + environment, |
| 62 | + new Request('http://example.com/foo'), |
| 63 | + ); |
| 64 | + |
| 65 | + expect(request.url).toBe('http://example.com/foo'); |
| 66 | + }); |
| 67 | + |
| 68 | + it(`should still honour x-forwarded-proto in ${environment}`, async () => { |
| 69 | + const request = await dispatch( |
| 70 | + environment, |
| 71 | + new Request('http://example.com/foo', { |
| 72 | + headers: { |
| 73 | + 'x-forwarded-proto': 'https', |
| 74 | + }, |
| 75 | + }), |
| 76 | + ); |
| 77 | + |
| 78 | + expect(request.url).toBe('https://example.com/foo'); |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + it('should apply x-forwarded-host to the request URL', async () => { |
| 83 | + const request = await dispatch( |
| 84 | + 'production', |
| 85 | + new Request('http://internal.host/foo', { |
| 86 | + headers: { |
| 87 | + 'x-forwarded-host': 'example.com', |
| 88 | + }, |
| 89 | + }), |
| 90 | + ); |
| 91 | + |
| 92 | + expect(request.url).toBe('https://example.com/foo'); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should force the accept header in every environment', async () => { |
| 96 | + for (const environment of ['development', 'production']) { |
| 97 | + const request = await dispatch( |
| 98 | + environment, |
| 99 | + new Request('http://example.com/foo', { |
| 100 | + headers: { |
| 101 | + accept: 'text/html', |
| 102 | + }, |
| 103 | + }), |
| 104 | + ); |
| 105 | + |
| 106 | + expect(request.headers.get('accept')).toBe( |
| 107 | + 'application/activity+json', |
| 108 | + ); |
| 109 | + } |
| 110 | + }); |
| 111 | +}); |
0 commit comments