Unverified Commit 982b65a3 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure invalid request to static page is handled correctly (#34346)

* Ensure invalid request to static page is handled correctly

* update test
parent 4500cb7d
......@@ -1143,6 +1143,23 @@ export default abstract class Server {
res.statusCode = parseInt(pathname.substr(1), 10)
}
// static pages can only respond to GET/HEAD
// requests so ensure we respond with 405 for
// invalid requests
if (
!is404Page &&
!is500Page &&
pathname !== '/_error' &&
req.method !== 'HEAD' &&
req.method !== 'GET' &&
(typeof components.Component === 'string' || isSSG)
) {
res.statusCode = 405
res.setHeader('Allow', ['GET', 'HEAD'])
await this.renderError(null, req, res, pathname)
return null
}
// handle static page
if (typeof components.Component === 'string') {
return {
......
......@@ -290,6 +290,14 @@ const runTests = (dev = false) => {
expect(await res.text()).toBe('hello from gssp')
})
it('should allow POST request for getServerSideProps page', async () => {
const res = await fetchViaHTTP(next.url, '/', undefined, {
method: 'POST',
})
expect(res.status).toBe(200)
expect(await res.text()).toMatch(/hello.*?world/)
})
it('should render correctly when notFound is false (non-dynamic)', async () => {
const res = await fetchViaHTTP(next.url, '/not-found')
......
......@@ -422,6 +422,14 @@ describe('Prerender', () => {
const runTests = (dev = false) => {
navigateTest(dev)
it('should respond with 405 for POST to static page', async () => {
const res = await fetchViaHTTP(next.url, '/', undefined, {
method: 'POST',
})
expect(res.status).toBe(405)
expect(await res.text()).toContain('Method Not Allowed')
})
it('should SSR normal page correctly', async () => {
const html = await renderViaHTTP(next.url, '/')
expect(html).toMatch(/hello.*?world/)
......
export default function Page() {
return <div>User</div>
}
export function getServerSideProps() {
return {
props: {},
}
}
......@@ -69,6 +69,14 @@ describe('Production Usage', () => {
)
})
it('should respond with 405 for POST to static page', async () => {
const res = await fetchViaHTTP(appPort, '/', undefined, {
method: 'POST',
})
expect(res.status).toBe(405)
expect(await res.text()).toContain('Method Not Allowed')
})
it('should contain generated page count in output', async () => {
const pageCount = 40
expect(output).toContain(`Generating static pages (0/${pageCount})`)
......@@ -369,7 +377,7 @@ describe('Production Usage', () => {
it('should render 200 for POST on page', async () => {
const res = await fetchViaHTTP(
`http://localhost:${appPort}`,
'/about',
'/fully-dynamic',
undefined,
{
method: 'POST',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment