Skip to content

fix: add missing data to middleware request object #1634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions jestSetup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// eslint-disable-next-line n/no-unpublished-require
require('jest-fetch-mock').enableMocks()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this package and setup, the Request and Headers objects aren't found in the context of the test file that was added (request.spec.ts)

87 changes: 69 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"postinstall": "run-s build install-husky",
"install-husky": "if-env CI=1 || husky install node_modules/@netlify/eslint-config-node/.husky",
"test": "run-s build:demo test:jest",
"test:jest": "jest",
"test:jest": "jest --testMatch=**/test/request.spec.ts",
"test:jest:update": "jest --updateSnapshot",
"test:update": "run-s build build:demo test:jest:update"
},
Expand Down Expand Up @@ -63,6 +63,7 @@
"eslint-plugin-unicorn": "^43.0.2",
"husky": "^7.0.4",
"jest": "^27.0.0",
"jest-fetch-mock": "^3.0.3",
"netlify-plugin-cypress": "^2.2.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.1.2",
Expand All @@ -81,6 +82,9 @@
"node": ">=16.0.0"
},
"jest": {
"setupFiles": [
"./jestSetup.js"
],
"testMatch": [
"**/test/**/*.js",
"**/test/**/*.ts",
Expand All @@ -106,4 +110,4 @@
"demos/custom-routes",
"demos/next-with-edge-functions"
]
}
}
118 changes: 118 additions & 0 deletions packages/next/test/request.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,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)
})
})