-
Notifications
You must be signed in to change notification settings - Fork 86
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
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
930e809
fix: add missing data to middleware request object
ericapisani b8fdb1d
docs: add limitations of edge middleware in local dev
ericapisani d8c55af
Merge branch 'main' into ep-inconsistent-edge-func-behaviour
ericapisani 6dac0ae
test: add test coverage for MiddlewareRequest
ericapisani 5d7027d
fix: revert the change I made to focus on a test file
ericapisani 154bff2
test: remove some of the hard coded strings
ericapisani d925520
Merge branch 'main' into ep-inconsistent-edge-func-behaviour
ericapisani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without this package and setup, the |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,8 @@ import { copy, copyFile, emptyDir, ensureDir, readJSON, readJson, writeJSON, wri | |
import type { MiddlewareManifest } from 'next/dist/build/webpack/plugins/middleware-plugin' | ||
import type { RouteHas } from 'next/dist/lib/load-custom-routes' | ||
|
||
import { getRequiredServerFiles } from './config' | ||
|
||
// This is the format as of [email protected] | ||
interface EdgeFunctionDefinitionV1 { | ||
env: string[] | ||
|
@@ -198,6 +200,11 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => { | |
|
||
await copy(getEdgeTemplatePath('../edge-shared'), join(edgeFunctionRoot, 'edge-shared')) | ||
|
||
const { publish } = netlifyConfig.build | ||
const nextConfigFile = await getRequiredServerFiles(publish) | ||
const nextConfig = nextConfigFile.config | ||
await writeJSON(join(edgeFunctionRoot, 'edge-shared', 'nextConfig.json'), nextConfig) | ||
|
||
if (!process.env.NEXT_DISABLE_EDGE_IMAGES) { | ||
console.log( | ||
'Using Netlify Edge Functions for image format detection. Set env var "NEXT_DISABLE_EDGE_IMAGES=true" to disable.', | ||
|
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When you say "such as", does that mean there are other things missing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't yet done an audit of all the properties that are affected by this so I'm not 100% certain that these are the only properties affected on the
nextUrl
object.Something to add here as well but the pathname (as reported by the user who originally raised this issue with us) was also incorrect before these changes and should be mentioned.