|
| 1 | +import Chance from 'chance' |
| 2 | +import { NextURL } from 'next/dist/server/web/next-url' |
| 3 | +import { NextCookies } from 'next/dist/server/web/spec-extension/cookies' |
| 4 | +import { NextRequest } from 'next/server' |
| 5 | +import { MiddlewareRequest } from '../src/middleware/request' |
| 6 | + |
| 7 | +const chance = new Chance() |
| 8 | + |
| 9 | +describe('MiddlewareRequest', () => { |
| 10 | + let nextRequest, mockHeaders, mockHeaderValue, requestId, geo, ip, url |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + globalThis.Deno = {} |
| 14 | + globalThis.NFRequestContextMap = new Map() |
| 15 | + |
| 16 | + ip = chance.ip() |
| 17 | + url = chance.url() |
| 18 | + |
| 19 | + const context = { |
| 20 | + geo: { |
| 21 | + country: { |
| 22 | + code: chance.country(), |
| 23 | + }, |
| 24 | + subdivision: { |
| 25 | + code: chance.province(), |
| 26 | + }, |
| 27 | + city: chance.city(), |
| 28 | + }, |
| 29 | + ip, |
| 30 | + } |
| 31 | + |
| 32 | + geo = { |
| 33 | + country: context.geo.country?.code, |
| 34 | + region: context.geo.subdivision?.code, |
| 35 | + city: context.geo.city, |
| 36 | + } |
| 37 | + |
| 38 | + const req = new URL(url) |
| 39 | + |
| 40 | + requestId = chance.guid() |
| 41 | + globalThis.NFRequestContextMap.set(requestId, { |
| 42 | + request: req, |
| 43 | + context, |
| 44 | + }) |
| 45 | + |
| 46 | + mockHeaders = new Headers() |
| 47 | + mockHeaderValue = chance.word() |
| 48 | + |
| 49 | + mockHeaders.append('foo', mockHeaderValue) |
| 50 | + mockHeaders.append('x-nf-request-id', requestId) |
| 51 | + |
| 52 | + const request = { |
| 53 | + headers: mockHeaders, |
| 54 | + geo, |
| 55 | + method: 'GET', |
| 56 | + ip: context.ip, |
| 57 | + body: null, |
| 58 | + } |
| 59 | + |
| 60 | + nextRequest = new NextRequest(req, request) |
| 61 | + }) |
| 62 | + |
| 63 | + afterEach(() => { |
| 64 | + nextRequest = null |
| 65 | + requestId = null |
| 66 | + delete globalThis.Deno |
| 67 | + delete globalThis.NFRequestContextMap |
| 68 | + }) |
| 69 | + |
| 70 | + it('throws an error when MiddlewareRequest is run outside of edge environment', () => { |
| 71 | + delete globalThis.Deno |
| 72 | + expect(() => new MiddlewareRequest(nextRequest)).toThrowError( |
| 73 | + 'MiddlewareRequest only works in a Netlify Edge Function environment', |
| 74 | + ) |
| 75 | + }) |
| 76 | + |
| 77 | + it('throws an error when x-nf-request-id header is missing', () => { |
| 78 | + nextRequest.headers.delete('x-nf-request-id') |
| 79 | + expect(() => new MiddlewareRequest(nextRequest)).toThrowError('Missing x-nf-request-id header') |
| 80 | + }) |
| 81 | + |
| 82 | + it('throws an error when request context is missing', () => { |
| 83 | + globalThis.NFRequestContextMap.delete(requestId) |
| 84 | + expect(() => new MiddlewareRequest(nextRequest)).toThrowError( |
| 85 | + `Could not find request context for request id ${requestId}`, |
| 86 | + ) |
| 87 | + }) |
| 88 | + |
| 89 | + it('returns the headers object', () => { |
| 90 | + const mwRequest = new MiddlewareRequest(nextRequest) |
| 91 | + expect(mwRequest.headers).toStrictEqual(mockHeaders) |
| 92 | + }) |
| 93 | + |
| 94 | + it('returns the cookies object', () => { |
| 95 | + const mwRequest = new MiddlewareRequest(nextRequest) |
| 96 | + expect(mwRequest.cookies).toBeInstanceOf(NextCookies) |
| 97 | + }) |
| 98 | + |
| 99 | + it('returns the geo object', () => { |
| 100 | + const mwRequest = new MiddlewareRequest(nextRequest) |
| 101 | + expect(mwRequest.geo).toStrictEqual(geo) |
| 102 | + }) |
| 103 | + |
| 104 | + it('returns the ip object', () => { |
| 105 | + const mwRequest = new MiddlewareRequest(nextRequest) |
| 106 | + expect(mwRequest.ip).toStrictEqual(ip) |
| 107 | + }) |
| 108 | + |
| 109 | + it('returns the nextUrl object', () => { |
| 110 | + const mwRequest = new MiddlewareRequest(nextRequest) |
| 111 | + expect(mwRequest.nextUrl).toBeInstanceOf(NextURL) |
| 112 | + }) |
| 113 | + |
| 114 | + it('returns the url', () => { |
| 115 | + const mwRequest = new MiddlewareRequest(nextRequest) |
| 116 | + expect(mwRequest.url).toEqual(url) |
| 117 | + }) |
| 118 | +}) |
0 commit comments