Skip to content

Commit

Permalink
use Request.json
Browse files Browse the repository at this point in the history
  • Loading branch information
Schniz committed Feb 14, 2022
1 parent 0a841a5 commit ab61023
Showing 1 changed file with 2 additions and 21 deletions.
23 changes: 2 additions & 21 deletions test/production/reading-request-body-in-middleware/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,15 @@ describe('reading request body in middleware', () => {
beforeAll(async () => {
next = await createNext({
files: {
'src/readBody.js': `
export async function readBody(reader, input = reader.read(), body = "") {
const { value, done } = await input;
const inputText = new TextDecoder().decode(value);
body += inputText;
if (done) {
return body;
}
const next = await reader.read();
return readBody(reader, next, body);
}
`,

'pages/_middleware.js': `
const { NextResponse } = require('next/server');
import { readBody } from '../src/readBody';
export default async function middleware(request) {
if (!request.body) {
return new Response('No body', { status: 400 });
}
const reader = await request.body.getReader();
const body = await readBody(reader);
const json = JSON.parse(body);
const json = await request.json();
if (request.nextUrl.searchParams.has("next")) {
return NextResponse.next();
Expand All @@ -52,16 +36,13 @@ describe('reading request body in middleware', () => {

'pages/nested/_middleware.js': `
const { NextResponse } = require('next/server');
import { readBody } from '../../src/readBody';
export default async function middleware(request) {
if (!request.body) {
return new Response('No body', { status: 400 });
}
const reader = await request.body.getReader();
const body = await readBody(reader);
const json = JSON.parse(body);
const json = await request.json();
return new Response(JSON.stringify({
root: false,
Expand Down

0 comments on commit ab61023

Please sign in to comment.