Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
g3offrey committed Oct 6, 2021
1 parent 40ecab0 commit d24db08
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 139 deletions.
28 changes: 15 additions & 13 deletions examples/custom-server/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@ import {log} from "@blitzjs/display"
const {PORT = "3000"} = process.env
const dev = process.env.NODE_ENV !== "production"
const app = blitz({dev})
const handle = app.getRequestHandler()

app.prepare().then(() => {
createServer((req, res) => {
const parsedUrl = parse(req.url!, true)
const {pathname} = parsedUrl
app
.prepare()
.then(() => app.getRequestHandler())
.then((requestHandler) => {
createServer((req, res) => {
const parsedUrl = parse(req.url!, true)
const {pathname} = parsedUrl

if (pathname === "/hello") {
res.writeHead(200).end("world")
return
}
if (pathname === "/hello") {
res.writeHead(200).end("world")
return
}

handle(req, res, parsedUrl)
}).listen(PORT, () => {
log.success(`Ready on http://localhost:${PORT}`)
requestHandler(req, res, parsedUrl)
}).listen(PORT, () => {
log.success(`Ready on http://localhost:${PORT}`)
})
})
})
42 changes: 22 additions & 20 deletions nextjs/docs/advanced-features/custom-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,29 @@ const next = require('next')

const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()

app.prepare().then(() => {
createServer((req, res) => {
// Be sure to pass `true` as the second argument to `url.parse`.
// This tells it to parse the query portion of the URL.
const parsedUrl = parse(req.url, true)
const { pathname, query } = parsedUrl

if (pathname === '/a') {
app.render(req, res, '/a', query)
} else if (pathname === '/b') {
app.render(req, res, '/b', query)
} else {
handle(req, res, parsedUrl)
}
}).listen(3000, (err) => {
if (err) throw err
console.log('> Ready on http://localhost:3000')

app
.prepare()
.then(() => app.getRequestHandler())
.then((requestHandler) => {
createServer((req, res) => {
// Be sure to pass `true` as the second argument to `url.parse`.
// This tells it to parse the query portion of the URL.
const parsedUrl = parse(req.url, true)
const { pathname, query } = parsedUrl

if (pathname === '/a') {
app.render(req, res, '/a', query)
} else if (pathname === '/b') {
app.render(req, res, '/b', query)
} else {
requestHandler(req, res, parsedUrl)
}
}).listen(3000, (err) => {
if (err) throw err
console.log('> Ready on http://localhost:3000')
})
})
})
```

> `server.js` doesn't go through babel or webpack. Make sure the syntax and sources this file requires are compatible with the current node version you are running.
Expand Down
8 changes: 1 addition & 7 deletions nextjs/packages/next/server/next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,7 @@ export class NextServer {
async getRequestHandler() {
const requestHandler = await this.getServerRequestHandler()

return async (
req: IncomingMessage,
res: ServerResponse,
parsedUrl?: UrlWithParsedQuery
) => {
return requestHandler(req, res, parsedUrl)
}
return requestHandler
}

setAssetPrefix(assetPrefix: string) {
Expand Down
28 changes: 15 additions & 13 deletions nextjs/test/integration/api-body-parser/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,24 @@ const dir = __dirname
const port = process.env.PORT || 3000

const app = next({ dev, dir })
const handleNextRequests = app.getRequestHandler()

app.prepare().then(() => {
const server = express()
app
.prepare()
.then(() => app.getRequestHandler())
.then((requestHandler) => {
const server = express()

server.use(bodyParser.json({ limit: '5mb' }))
server.use(bodyParser.json({ limit: '5mb' }))

server.all('*', (req, res) => {
handleNextRequests(req, res)
})
server.all('*', (req, res) => {
requestHandler(req, res)
})

server.listen(port, (err) => {
if (err) {
throw err
}
server.listen(port, (err) => {
if (err) {
throw err
}

console.log(`> Ready on http://localhost:${port}`)
console.log(`> Ready on http://localhost:${port}`)
})
})
})
16 changes: 9 additions & 7 deletions nextjs/test/integration/custom-server-types/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ const port = process.env.PORT || 3000
const dev = process.env.NODE_ENV !== 'production'

const app = next({ dev, dir })
const handleNextRequests = app.getRequestHandler()

app.prepare().then(() => {
const server = new http.Server((req, res) => {
handleNextRequests(req, res)
})
app
.prepare()
.then(() => app.getRequestHandler())
.then((requestHandler) => {
const server = new http.Server((req, res) => {
requestHandler(req, res)
})

server.listen(port)
})
server.listen(port)
})
90 changes: 46 additions & 44 deletions nextjs/test/integration/custom-server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,54 @@ const dir = __dirname
const port = process.env.PORT || 3000

const app = next({ dev, dir })
const handleNextRequests = app.getRequestHandler()

app.prepare().then(() => {
const server = new http.Server(async (req, res) => {
if (req.url === '/no-query') {
return app.render(req, res, '/no-query')
}

if (/setAssetPrefix/.test(req.url)) {
app.setAssetPrefix(`http://127.0.0.1:${port}`)
} else if (/setEmptyAssetPrefix/.test(req.url)) {
app.setAssetPrefix(null)
} else {
// This is to support multi-zones support in localhost
// and may be in staging deployments
app.setAssetPrefix('')
}

if (/test-index-hmr/.test(req.url)) {
return app.render(req, res, '/index')
}

if (/dashboard/.test(req.url)) {
return app.render(req, res, '/dashboard')
}

if (/static\/hello\.text/.test(req.url)) {
return app.render(req, res, '/static/hello.text')
}

if (/no-slash/.test(req.url)) {
try {
await app.render(req, res, 'dashboard')
} catch (err) {
res.end(err.message)

app
.prepare()
.then(() => app.getRequestHandler())
.then((requestHandler) => {
const server = new http.Server(async (req, res) => {
if (req.url === '/no-query') {
return app.render(req, res, '/no-query')
}
}

handleNextRequests(req, res)
})
if (/setAssetPrefix/.test(req.url)) {
app.setAssetPrefix(`http://127.0.0.1:${port}`)
} else if (/setEmptyAssetPrefix/.test(req.url)) {
app.setAssetPrefix(null)
} else {
// This is to support multi-zones support in localhost
// and may be in staging deployments
app.setAssetPrefix('')
}

if (/test-index-hmr/.test(req.url)) {
return app.render(req, res, '/index')
}

if (/dashboard/.test(req.url)) {
return app.render(req, res, '/dashboard')
}

server.listen(port, (err) => {
if (err) {
throw err
}
if (/static\/hello\.text/.test(req.url)) {
return app.render(req, res, '/static/hello.text')
}

if (/no-slash/.test(req.url)) {
try {
await app.render(req, res, 'dashboard')
} catch (err) {
res.end(err.message)
}
}

requestHandler(req, res)
})

server.listen(port, (err) => {
if (err) {
throw err
}

console.log(`> Ready on http://localhost:${port}`)
console.log(`> Ready on http://localhost:${port}`)
})
})
})
42 changes: 22 additions & 20 deletions nextjs/test/integration/filesystempublicroutes/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,30 @@ const dir = __dirname
const port = process.env.PORT || 3000

const app = next({ dev, dir })
const handleNextRequests = app.getRequestHandler()

app.prepare().then(() => {
const server = new http.Server((req, res) => {
if (/setAssetPrefix/.test(req.url)) {
app.setAssetPrefix(`http://127.0.0.1:${port}`)
} else if (/setEmptyAssetPrefix/.test(req.url)) {
app.setAssetPrefix(null)
} else {
// This is to support multi-zones support in localhost
// and may be in staging deployments
app.setAssetPrefix('')
}
app
.prepare()
.then(() => app.getRequestHandler())
.then((requestHandler) => {
const server = new http.Server((req, res) => {
if (/setAssetPrefix/.test(req.url)) {
app.setAssetPrefix(`http://127.0.0.1:${port}`)
} else if (/setEmptyAssetPrefix/.test(req.url)) {
app.setAssetPrefix(null)
} else {
// This is to support multi-zones support in localhost
// and may be in staging deployments
app.setAssetPrefix('')
}

handleNextRequests(req, res)
})
requestHandler(req, res)
})

server.listen(port, (err) => {
if (err) {
throw err
}
server.listen(port, (err) => {
if (err) {
throw err
}

console.log(`> Ready on http://localhost:${port}`)
console.log(`> Ready on http://localhost:${port}`)
})
})
})
24 changes: 13 additions & 11 deletions nextjs/test/integration/non-standard-node-env-warning/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@ const dir = __dirname
const port = process.env.PORT || 3000

const app = next({ dev, dir })
const handleNextRequests = app.getRequestHandler()

app.prepare().then(() => {
const server = new http.Server(async (req, res) => {
handleNextRequests(req, res)
})
app
.prepare()
.then(() => app.getRequestHandler())
.then((requestHandler) => {
const server = new http.Server(async (req, res) => {
requestHandler(req, res)
})

server.listen(port, (err) => {
if (err) {
throw err
}
server.listen(port, (err) => {
if (err) {
throw err
}

console.log(`> Ready on http://localhost:${port}`)
console.log(`> Ready on http://localhost:${port}`)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ describe('Required Server Files', () => {

server = http.createServer(async (req, res) => {
try {
await nextApp.getRequestHandler()(req, res)
const requestHandler = await nextApp.getRequestHandler()
await requestHandler(req, res)
} catch (err) {
console.error('top-level', err)
errors.push(err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ describe('Required Server Files', () => {

server = http.createServer(async (req, res) => {
try {
await nextApp.getRequestHandler()(req, res)
const requestHandler = await nextApp.getRequestHandler()
await requestHandler(req, res)
} catch (err) {
console.error('top-level', err)
errors.push(err)
Expand Down
2 changes: 1 addition & 1 deletion nextjs/test/lib/next-test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ export async function killApp(instance) {

export async function startApp(app) {
await app.prepare()
const handler = app.getRequestHandler()
const handler = await app.getRequestHandler()
const server = http.createServer(handler)
server.__app = app

Expand Down
2 changes: 1 addition & 1 deletion test/lib/blitz-test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ export async function killApp(instance) {

export async function startApp(app: any) {
await app.prepare()
const handler = app.getRequestHandler()
const handler = await app.getRequestHandler()
const server = http.createServer(handler)
;(server as any).__app = app

Expand Down

0 comments on commit d24db08

Please sign in to comment.