Skip to content

Commit

Permalink
Add test for middleware data prefetch
Browse files Browse the repository at this point in the history
  • Loading branch information
javivelasco committed May 23, 2022
1 parent ae5f08b commit 2460be7
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
5 changes: 5 additions & 0 deletions test/integration/middleware-general/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ export async function middleware(request) {
return NextResponse.next()
}

if (url.pathname === '/ssr-page') {
url.pathname = '/ssr-page-2'
return NextResponse.rewrite(url)
}

// Map metadata by default
return new Response(null, {
headers: {
Expand Down
11 changes: 11 additions & 0 deletions test/integration/middleware-general/pages/ssr-page-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default function SSRPage(props) {
return <h1>{props.message}</h1>
}

export const getServerSideProps = (req) => {
return {
props: {
message: 'Bye Cruel World',
},
}
}
11 changes: 11 additions & 0 deletions test/integration/middleware-general/pages/ssr-page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default function SSRPage(props) {
return <h1>{props.message}</h1>
}

export const getServerSideProps = (req) => {
return {
props: {
message: 'Hello World',
},
}
}
18 changes: 18 additions & 0 deletions test/integration/middleware-general/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe('Middleware Runtime', () => {
beforeAll(async () => {
context.dev = true
context.appPort = await findPort()
context.buildId = 'development'
context.app = await launchApp(context.appDir, context.appPort, {
env: {
MIDDLEWARE_TEST: 'asdf',
Expand Down Expand Up @@ -83,6 +84,11 @@ describe('Middleware Runtime', () => {
stdout: true,
})

context.buildId = await fs.readFile(
join(context.appDir, '.next/BUILD_ID'),
'utf8'
)

context.buildLogs = {
output: build.stdout + build.stderr,
stderr: build.stderr,
Expand Down Expand Up @@ -279,6 +285,18 @@ function tests(context, locale = '') {
)
expect(readMiddlewareError(response)).toContain(urlsError)
})

it('should trigger middleware for data requests', async () => {
const browser = await webdriver(context.appPort, `/ssr-page`)
const text = await browser.elementByCss('h1').text()
expect(text).toEqual('Bye Cruel World')
const res = await fetchViaHTTP(
context.appPort,
`/_next/data/${context.buildId}/en/ssr-page.json`
)
const json = await res.json()
expect(json.pageProps.message).toEqual('Bye Cruel World')
})
}

function readMiddlewareJSON(response) {
Expand Down

0 comments on commit 2460be7

Please sign in to comment.