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