Skip to content

Commit

Permalink
feat: change NextInstance.fetch signature
Browse files Browse the repository at this point in the history
  • Loading branch information
wyattjoh committed Jan 4, 2023
1 parent ca73cbf commit 377716a
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 91 deletions.
13 changes: 6 additions & 7 deletions test/e2e/app-dir/app-middleware/app-middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { createNextDescribe, FileRef } from 'e2e-utils'
import cheerio from 'cheerio'
import path from 'path'
import { withQuery } from 'next-test-utils'

createNextDescribe(
'app-dir with middleware',
Expand Down Expand Up @@ -31,7 +32,7 @@ createNextDescribe(
},
])('Mutate request headers for $title', ({ path, toJson }) => {
it(`Adds new headers`, async () => {
const res = await next.fetch(path, null, {
const res = await next.fetch(path, {
headers: {
'x-from-client': 'hello-from-client',
},
Expand All @@ -44,10 +45,9 @@ createNextDescribe(

it(`Deletes headers`, async () => {
const res = await next.fetch(
path,
{
withQuery(path, {
'remove-headers': 'x-from-client1,x-from-client2',
},
}),
{
headers: {
'x-from-client1': 'hello-from-client',
Expand Down Expand Up @@ -78,11 +78,10 @@ createNextDescribe(

it(`Updates headers`, async () => {
const res = await next.fetch(
path,
{
withQuery(path, {
'update-headers':
'x-from-client1=new-value1,x-from-client2=new-value2',
},
}),
{
headers: {
'x-from-client1': 'old-value1',
Expand Down
46 changes: 19 additions & 27 deletions test/e2e/app-dir/app-static/app-static.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ createNextDescribe(
})

it('should force SSR correctly for headers usage', async () => {
const res = await next.fetch('/force-static', undefined, {
const res = await next.fetch('/force-static', {
headers: {
Cookie: 'myCookie=cookieValue',
another: 'header',
Expand Down Expand Up @@ -315,7 +315,7 @@ createNextDescribe(
const validParams = ['tim', 'seb', 'styfle']

for (const param of validParams) {
const res = await next.fetch(`/blog/${param}`, undefined, {
const res = await next.fetch(`/blog/${param}`, {
redirect: 'manual',
})
expect(res.status).toBe(200)
Expand All @@ -330,7 +330,7 @@ createNextDescribe(
const invalidParams = ['timm', 'non-existent']

for (const param of invalidParams) {
const invalidRes = await next.fetch(`/blog/${param}`, undefined, {
const invalidRes = await next.fetch(`/blog/${param}`, {
redirect: 'manual',
})
expect(invalidRes.status).toBe(404)
Expand All @@ -340,23 +340,19 @@ createNextDescribe(

it('should work with forced dynamic path', async () => {
for (const slug of ['first', 'second']) {
const res = await next.fetch(
`/dynamic-no-gen-params-ssr/${slug}`,
undefined,
{ redirect: 'manual' }
)
const res = await next.fetch(`/dynamic-no-gen-params-ssr/${slug}`, {
redirect: 'manual',
})
expect(res.status).toBe(200)
expect(await res.text()).toContain(`${slug}`)
}
})

it('should work with dynamic path no generateStaticParams', async () => {
for (const slug of ['first', 'second']) {
const res = await next.fetch(
`/dynamic-no-gen-params/${slug}`,
undefined,
{ redirect: 'manual' }
)
const res = await next.fetch(`/dynamic-no-gen-params/${slug}`, {
redirect: 'manual',
})
expect(res.status).toBe(200)
expect(await res.text()).toContain(`${slug}`)
}
Expand All @@ -383,13 +379,9 @@ createNextDescribe(
]

for (const params of paramsToCheck) {
const res = await next.fetch(
`/blog/${params.author}/${params.slug}`,
undefined,
{
redirect: 'manual',
}
)
const res = await next.fetch(`/blog/${params.author}/${params.slug}`, {
redirect: 'manual',
})
expect(res.status).toBe(200)
const html = await res.text()
const $ = cheerio.load(html)
Expand Down Expand Up @@ -436,7 +428,7 @@ createNextDescribe(

it('should ssr dynamically when detected automatically with fetch cache option', async () => {
const pathname = '/ssr-auto/cache-no-store'
const initialRes = await next.fetch(pathname, undefined, {
const initialRes = await next.fetch(pathname, {
redirect: 'manual',
})
expect(initialRes.status).toBe(200)
Expand All @@ -449,7 +441,7 @@ createNextDescribe(

expect(initialHtml).toContain('Example Domain')

const secondRes = await next.fetch(pathname, undefined, {
const secondRes = await next.fetch(pathname, {
redirect: 'manual',
})
expect(secondRes.status).toBe(200)
Expand All @@ -465,7 +457,7 @@ createNextDescribe(
})

it('should render not found pages correctly and fallback to the default one', async () => {
const res = await next.fetch(`/blog/shu/hi`, undefined, {
const res = await next.fetch(`/blog/shu/hi`, {
redirect: 'manual',
})
expect(res.status).toBe(404)
Expand All @@ -477,7 +469,7 @@ createNextDescribe(
// TODO-APP: support fetch revalidate case for dynamic rendering
it.skip('should ssr dynamically when detected automatically with fetch revalidate option', async () => {
const pathname = '/ssr-auto/fetch-revalidate-zero'
const initialRes = await next.fetch(pathname, undefined, {
const initialRes = await next.fetch(pathname, {
redirect: 'manual',
})
expect(initialRes.status).toBe(200)
Expand All @@ -490,7 +482,7 @@ createNextDescribe(

expect(initialHtml).toContain('Example Domain')

const secondRes = await next.fetch(pathname, undefined, {
const secondRes = await next.fetch(pathname, {
redirect: 'manual',
})
expect(secondRes.status).toBe(200)
Expand All @@ -506,7 +498,7 @@ createNextDescribe(
})

it('should ssr dynamically when forced via config', async () => {
const initialRes = await next.fetch('/ssr-forced', undefined, {
const initialRes = await next.fetch('/ssr-forced', {
redirect: 'manual',
})
expect(initialRes.status).toBe(200)
Expand All @@ -517,7 +509,7 @@ createNextDescribe(
expect(initial$('#page').text()).toBe('/ssr-forced')
const initialDate = initial$('#date').text()

const secondRes = await next.fetch('/ssr-forced', undefined, {
const secondRes = await next.fetch('/ssr-forced', {
redirect: 'manual',
})
expect(secondRes.status).toBe(200)
Expand Down
38 changes: 15 additions & 23 deletions test/e2e/app-dir/app/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ createNextDescribe(
const controller1 = new AbortController()
const controller2 = new AbortController()
next
.fetch('/slow-page-no-loading', undefined, {
.fetch('/slow-page-no-loading', {
signal: controller1.signal,
})
.catch(() => {})
next
.fetch('/slow-page-no-loading', undefined, {
.fetch('/slow-page-no-loading', {
signal: controller2.signal,
})
.catch(() => {})
Expand All @@ -35,7 +35,7 @@ createNextDescribe(

const controller3 = new AbortController()
next
.fetch('/slow-page-no-loading', undefined, {
.fetch('/slow-page-no-loading', {
signal: controller3.signal,
})
.catch(() => {})
Expand Down Expand Up @@ -66,28 +66,20 @@ createNextDescribe(
}

it('should use application/octet-stream for flight', async () => {
const res = await next.fetch(
'/dashboard/deployments/123',
{},
{
headers: {
['RSC'.toString()]: '1',
},
}
)
const res = await next.fetch('/dashboard/deployments/123', {
headers: {
['RSC'.toString()]: '1',
},
})
expect(res.headers.get('Content-Type')).toBe('application/octet-stream')
})

it('should use application/octet-stream for flight with edge runtime', async () => {
const res = await next.fetch(
'/dashboard',
{},
{
headers: {
['RSC'.toString()]: '1',
},
}
)
const res = await next.fetch('/dashboard', {
headers: {
['RSC'.toString()]: '1',
},
})
expect(res.headers.get('Content-Type')).toBe('application/octet-stream')
})

Expand Down Expand Up @@ -1895,7 +1887,7 @@ createNextDescribe(
})
;(isDev ? describe.skip : describe)('Subresource Integrity', () => {
function fetchWithPolicy(policy: string | null) {
return next.fetch('/dashboard', undefined, {
return next.fetch('/dashboard', {
headers: policy
? {
'Content-Security-Policy': policy,
Expand Down Expand Up @@ -2405,7 +2397,7 @@ createNextDescribe(
describe('bots', () => {
if (!isNextDeploy) {
it('should block rendering for bots and return 404 status', async () => {
const res = await next.fetch('/not-found/servercomponent', '', {
const res = await next.fetch('/not-found/servercomponent', {
headers: {
'User-Agent': 'Googlebot',
},
Expand Down
10 changes: 3 additions & 7 deletions test/e2e/app-dir/asset-prefix/asset-prefix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,9 @@ createNextDescribe(
},
({ next }) => {
it('should redirect route when requesting it directly', async () => {
const res = await next.fetch(
'/a/',
{},
{
redirect: 'manual',
}
)
const res = await next.fetch('/a/', {
redirect: 'manual',
})
expect(res.status).toBe(308)
expect(res.headers.get('location')).toBe(next.url + '/a')
})
Expand Down
18 changes: 7 additions & 11 deletions test/e2e/app-dir/rsc-basic/rsc-basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ describe('app dir - rsc basics', () => {
})

it('should render css-in-js suspense boundary correctly', async () => {
await next.fetch('/css-in-js/suspense', null, {}).then(async (response) => {
await next.fetch('/css-in-js/suspense').then(async (response) => {
const results = []

await resolveStreamResponse(response, (chunk: string) => {
Expand Down Expand Up @@ -393,23 +393,19 @@ describe('app dir - rsc basics', () => {

it('should support streaming for flight response', async () => {
await next
.fetch(
'/',
{},
{
headers: {
['RSC'.toString()]: '1',
},
}
)
.fetch('/', {
headers: {
['RSC'.toString()]: '1',
},
})
.then(async (response) => {
const result = await resolveStreamResponse(response)
expect(result).toContain('component:index.server')
})
})

it('should support partial hydration with inlined server data', async () => {
await next.fetch('/partial-hydration', null, {}).then(async (response) => {
await next.fetch('/partial-hydration').then(async (response) => {
let gotFallback = false
let gotData = false
let gotInlinedData = false
Expand Down
10 changes: 3 additions & 7 deletions test/e2e/app-dir/trailingslash/trailingslash.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,9 @@ createNextDescribe(
},
({ next }) => {
it('should redirect route when requesting it directly', async () => {
const res = await next.fetch(
'/a',
{},
{
redirect: 'manual',
}
)
const res = await next.fetch('/a', {
redirect: 'manual',
})
expect(res.status).toBe(308)
expect(res.headers.get('location')).toBe(next.url + '/a/')
})
Expand Down
13 changes: 9 additions & 4 deletions test/lib/next-modes/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import os from 'os'
import path from 'path'
import fs from 'fs-extra'
import treeKill from 'tree-kill'
import { NextConfig } from 'next'
import type { NextConfig } from 'next'
import { FileRef } from '../e2e-utils'
import { ChildProcess } from 'child_process'
import { createNextInstall } from '../create-next-install'
Expand Down Expand Up @@ -414,12 +414,17 @@ export class NextInstance {
}

/**
* Fetch the HTML for the provided page.
* Performs a fetch request to the NextInstance with the options provided.
*
* @param pathname the pathname on the NextInstance to fetch
* @param opts the optional options to pass to the underlying fetch
* @returns the fetch response
*/
public async fetch(
...args: Parameters<OmitFirstArgument<typeof fetchViaHTTP>>
pathname: string,
opts?: import('node-fetch').RequestInit
) {
return fetchViaHTTP(this.url, ...args)
return fetchViaHTTP(this.url, pathname, null, opts)
}

public on(event: Event, cb: (...args: any[]) => any) {
Expand Down
Loading

0 comments on commit 377716a

Please sign in to comment.