Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
137008e6
Unverified
Commit
137008e6
authored
Nov 02, 2022
by
Kiko Beats
Committed by
GitHub
Nov 02, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[middleware] add tests for body reading methods #37980 (#41718)
parent
460e19f0
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
210 additions
and
7 deletions
+210
-7
package.json
package.json
+1
-0
index.js
packages/next/compiled/undici/index.js
+2
-2
package.json
packages/next/package.json
+1
-1
pnpm-lock.yaml
pnpm-lock.yaml
+29
-4
.gitignore
test/e2e/edge-can-read-request-body/app/.gitignore
+1
-0
middleware.js
test/e2e/edge-can-read-request-body/app/middleware.js
+49
-0
nothing.js
test/e2e/edge-can-read-request-body/app/pages/api/nothing.js
+3
-0
index.test.ts
test/e2e/edge-can-read-request-body/index.test.ts
+124
-0
No files found.
package.json
View file @
137008e6
...
...
@@ -141,6 +141,7 @@
"
faunadb
"
:
"2.6.1"
,
"
firebase
"
:
"7.14.5"
,
"
flat
"
:
"5.0.2"
,
"
form-data
"
:
"4.0.0"
,
"
fs-extra
"
:
"9.0.0"
,
"
get-port
"
:
"5.1.1"
,
"
glob
"
:
"7.1.6"
,
...
...
packages/next/compiled/undici/index.js
View file @
137008e6
This diff is collapsed.
Click to expand it.
packages/next/package.json
View file @
137008e6
...
...
@@ -276,7 +276,7 @@
"timers-browserify"
:
"2.0.12"
,
"tty-browserify"
:
"0.0.1"
,
"ua-parser-js"
:
"0.7.28"
,
"undici"
:
"5.1
0
.0"
,
"undici"
:
"5.1
2
.0"
,
"unistore"
:
"3.4.1"
,
"util"
:
"0.12.4"
,
"uuid"
:
"8.3.2"
,
...
...
pnpm-lock.yaml
View file @
137008e6
...
...
@@ -106,6 +106,7 @@ importers:
faunadb
:
2.6.1
firebase
:
7.14.5
flat
:
5.0.2
form-data
:
4.0.0
fs-extra
:
9.0.0
get-port
:
5.1.1
glob
:
7.1.6
...
...
@@ -267,6 +268,7 @@ importers:
faunadb
:
2.6.1
firebase
:
7.14.5
flat
:
5.0.2
form-data
:
4.0.0
fs-extra
:
9.0.0
get-port
:
5.1.1
glob
:
7.1.6
...
...
@@ -630,7 +632,7 @@ importers:
timers-browserify
:
2.0.12
tty-browserify
:
0.0.1
ua-parser-js
:
0.7.28
undici
:
5.1
0
.0
undici
:
5.1
2
.0
unistore
:
3.4.1
use-sync-external-store
:
1.2.0
util
:
0.12.4
...
...
@@ -826,7 +828,7 @@ importers:
timers-browserify
:
2.0.12
tty-browserify
:
0.0.1
ua-parser-js
:
0.7.28
undici
:
5.1
0
.0
undici
:
5.1
2
.0
unistore
:
3.4.1_react@18.2.0
util
:
0.12.4
uuid
:
8.3.2
...
...
@@ -7960,6 +7962,13 @@ packages:
readable-stream
:
1.1.14
dev
:
true
/busboy/1.6.0
:
resolution
:
{
integrity
:
sha512-8SFQbg/0hQ9xy3UNTB0YEnsNBbWfhf7RtnzpL7TkBiTBRfrQ9Fxcnz7VJsleJpyp6rVLvXiuORqjlHi5q+PYuA==
}
engines
:
{
node
:
'
>=10.16.0'
}
dependencies
:
streamsearch
:
1.1.0
dev
:
true
/byline/5.0.0
:
resolution
:
{
integrity
:
sha512-s6webAy+R4SR8XVuJWt2V2rGvhnrhxN+9S15GNuTK3wKPOXFF6RNc+8ug2XhH+2s4f+uudG4kUVYmYOQWL2g0Q==
}
engines
:
{
node
:
'
>=0.10.0'
}
...
...
@@ -11637,6 +11646,15 @@ packages:
mime-types
:
2.1.30
dev
:
true
/form-data/4.0.0
:
resolution
:
{
integrity
:
sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==
}
engines
:
{
node
:
'
>=
6'
}
dependencies
:
asynckit
:
0.4.0
combined-stream
:
1.0.8
mime-types
:
2.1.30
dev
:
true
/format/0.2.2
:
resolution
:
{
integrity
:
sha1-1hcBB+nv3E7TDJ3DkBbflCtctYs=
}
engines
:
{
node
:
'
>=0.4.x'
}
...
...
@@ -20778,6 +20796,11 @@ packages:
engines
:
{
node
:
'
>=0.8.0'
}
dev
:
true
/streamsearch/1.1.0
:
resolution
:
{
integrity
:
sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==
}
engines
:
{
node
:
'
>=10.0.0'
}
dev
:
true
/string-argv/0.3.1
:
resolution
:
{
integrity
:
sha512-a1uQGz7IyVy9YwhqjZIZu1c8JO8dNIe20xBmSS6qu9kv++k3JGzCVmprbNN5Kn+BgzD5E7YYwg1CcjuJMRNsvg==
}
engines
:
{
node
:
'
>=0.6.19'
}
...
...
@@ -21922,9 +21945,11 @@ packages:
engines
:
{
node
:
'
>=0.10.0'
}
dev
:
true
/undici/5.1
0
.0
:
resolution
:
{
integrity
:
sha512-
c8HsD3IbwmjjbLvoZuRI26TZic+TSEe8FPMLLOkN1AfYRhdjnKBU6yL+IwcSCbdZiX4e5t0lfMDLDCqj4Sq70
g==
}
/undici/5.1
2
.0
:
resolution
:
{
integrity
:
sha512-
zMLamCG62PGjd9HHMpo05bSLvvwWOZgGeiWlN/vlqu3+lRo3elxktVGEyLMX+IO7c2eflLjcW74AlkhEZm15m
g==
}
engines
:
{
node
:
'
>=12.18'
}
dependencies
:
busboy
:
1.6.0
dev
:
true
/unfetch/4.1.0
:
...
...
test/e2e/edge-can-read-request-body/app/.gitignore
0 → 100644
View file @
137008e6
.vercel
test/e2e/edge-can-read-request-body/app/middleware.js
0 → 100644
View file @
137008e6
// @ts-check
import
{
NextResponse
}
from
'next/server'
/**
* @param {NextRequest} req
*/
export
default
async
function
middleware
(
req
)
{
const
res
=
NextResponse
.
next
()
res
.
headers
.
set
(
'x-incoming-content-type'
,
req
.
headers
.
get
(
'content-type'
))
const
handler
=
bodyHandlers
[
req
.
nextUrl
.
searchParams
.
get
(
'middleware-handler'
)]
const
headers
=
await
handler
?.(
req
)
for
(
const
[
key
,
value
]
of
headers
??
[])
{
res
.
headers
.
set
(
key
,
value
)
}
return
res
}
/**
* @typedef {import('next/server').NextRequest} NextRequest
* @typedef {(req: NextRequest) => Promise<[string, string][]>} Handler
* @type {Record<string, Handler>}
*/
const
bodyHandlers
=
{
json
:
async
(
req
)
=>
{
const
json
=
await
req
.
json
()
return
[
[
'x-req-type'
,
'json'
],
[
'x-serialized'
,
JSON
.
stringify
(
json
)],
]
},
text
:
async
(
req
)
=>
{
const
text
=
await
req
.
text
()
return
[
[
'x-req-type'
,
'text'
],
[
'x-serialized'
,
text
],
]
},
formData
:
async
(
req
)
=>
{
const
formData
=
await
req
.
formData
()
return
[
[
'x-req-type'
,
'formData'
],
[
'x-serialized'
,
JSON
.
stringify
(
Object
.
fromEntries
(
formData
))],
]
},
}
test/e2e/edge-can-read-request-body/app/pages/api/nothing.js
0 → 100644
View file @
137008e6
export
default
(
_req
,
res
)
=>
{
res
.
send
(
'ok'
)
}
test/e2e/edge-can-read-request-body/index.test.ts
0 → 100644
View file @
137008e6
import
{
createNext
,
FileRef
}
from
'e2e-utils'
import
{
NextInstance
}
from
'test/lib/next-modes/base'
import
{
fetchViaHTTP
,
renderViaHTTP
}
from
'next-test-utils'
import
FormData
from
'form-data'
import
path
from
'path'
async
function
serialize
(
response
:
Response
)
{
return
{
text
:
await
response
.
text
(),
headers
:
Object
.
fromEntries
(
response
.
headers
),
status
:
response
.
status
,
}
}
describe
(
'Edge can read request body'
,
()
=>
{
let
next
:
NextInstance
beforeAll
(
async
()
=>
{
next
=
await
createNext
({
files
:
new
FileRef
(
path
.
resolve
(
__dirname
,
'./app'
)),
dependencies
:
{},
})
})
afterAll
(()
=>
next
.
destroy
())
it
(
'renders the static page'
,
async
()
=>
{
const
html
=
await
renderViaHTTP
(
next
.
url
,
'/api/nothing'
)
expect
(
html
).
toContain
(
'ok'
)
})
describe
(
'middleware'
,
()
=>
{
it
(
'reads a JSON body'
,
async
()
=>
{
const
response
=
await
fetchViaHTTP
(
next
.
url
,
'/api/nothing?middleware-handler=json'
,
null
,
{
method
:
'POST'
,
body
:
JSON
.
stringify
({
hello
:
'world'
}),
}
)
expect
(
await
serialize
(
response
)).
toMatchObject
({
text
:
expect
.
stringContaining
(
'ok'
),
status
:
200
,
headers
:
{
'x-req-type'
:
'json'
,
'x-serialized'
:
'{"hello":"world"}'
,
},
})
})
it
(
'reads a text body'
,
async
()
=>
{
try
{
const
response
=
await
fetchViaHTTP
(
next
.
url
,
'/api/nothing?middleware-handler=text'
,
null
,
{
method
:
'POST'
,
body
:
JSON
.
stringify
({
hello
:
'world'
}),
}
)
expect
(
await
serialize
(
response
)).
toMatchObject
({
text
:
expect
.
stringContaining
(
'ok'
),
status
:
200
,
headers
:
{
'x-req-type'
:
'text'
,
'x-serialized'
:
'{"hello":"world"}'
,
},
})
}
catch
(
err
)
{
console
.
log
(
'FAILED'
,
err
)
}
})
it
(
'reads an URL encoded form data'
,
async
()
=>
{
const
response
=
await
fetchViaHTTP
(
next
.
url
,
'/api/nothing?middleware-handler=formData'
,
null
,
{
method
:
'POST'
,
headers
:
{
'Content-Type'
:
'application/x-www-form-urlencoded'
,
},
body
:
new
URLSearchParams
({
hello
:
'world'
}).
toString
(),
}
)
expect
(
await
serialize
(
response
)).
toMatchObject
({
text
:
expect
.
stringContaining
(
'ok'
),
status
:
200
,
headers
:
{
'x-req-type'
:
'formData'
,
'x-serialized'
:
'{"hello":"world"}'
,
},
})
})
it
(
'reads a multipart form data'
,
async
()
=>
{
const
formData
=
new
FormData
()
formData
.
append
(
'hello'
,
'world'
)
const
response
=
await
fetchViaHTTP
(
next
.
url
,
'/api/nothing?middleware-handler=formData'
,
null
,
{
method
:
'POST'
,
body
:
formData
,
}
)
expect
(
await
serialize
(
response
)).
toMatchObject
({
text
:
expect
.
stringContaining
(
'ok'
),
status
:
200
,
headers
:
{
'x-req-type'
:
'formData'
,
'x-serialized'
:
'{"hello":"world"}'
,
},
})
})
})
})
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment