提交 78bba565 编写于 作者: J JJ Kasper 提交者: Tim Neutkens

Add API tests to serverless (#7774)

* Add failing API tests to serverless

* Add newline

* Fix failing serverless tests
上级 52056955
......@@ -58,7 +58,7 @@ export default class Server {
nextConfig: NextConfig
distDir: string
publicDir: string
buildManifest: string
pagesManifest: string
buildId: string
renderOpts: {
poweredByHeader: boolean
......@@ -88,7 +88,11 @@ export default class Server {
this.distDir = join(this.dir, this.nextConfig.distDir)
// this.pagesDir = join(this.dir, 'pages')
this.publicDir = join(this.dir, CLIENT_PUBLIC_FILES_PATH)
this.buildManifest = join(this.distDir, BUILD_MANIFEST)
this.pagesManifest = join(
this.distDir,
this.nextConfig.target || 'server',
PAGES_MANIFEST
)
// Only serverRuntimeConfig needs the default
// publicRuntimeConfig gets it's default in client/index.js
......@@ -278,8 +282,12 @@ export default class Server {
pathname: string
) {
let params: Params | boolean = false
let resolverFunction: any
try {
resolverFunction = await this.resolveApiRequest(pathname)
} catch (err) {}
let resolverFunction = await this.resolveApiRequest(pathname)
if (
this.dynamicRoutes &&
this.dynamicRoutes.length > 0 &&
......@@ -294,6 +302,17 @@ export default class Server {
}
}
if (!resolverFunction) {
return this.render404(req, res)
}
if (!this.renderOpts.dev && this.nextConfig.target === 'serverless') {
const mod = require(resolverFunction)
if (typeof mod.default === 'function') {
return mod.default(req, res)
}
}
apiResolver(
req,
res,
......@@ -344,10 +363,8 @@ export default class Server {
}
private getDynamicRoutes() {
const manifest = require(this.buildManifest)
const dynamicRoutedPages = Object.keys(manifest.pages).filter(
isDynamicRoute
)
const manifest = require(this.pagesManifest)
const dynamicRoutedPages = Object.keys(manifest).filter(isDynamicRoute)
return getSortedRoutes(dynamicRoutedPages).map(page => ({
page,
match: getRouteMatcher(getRouteRegex(page)),
......
export default (req, res) => {
res.send('hello world')
}
export default (req, res) => {
res.json({ post: req.query.id })
}
......@@ -133,6 +133,17 @@ describe('Serverless', () => {
expect(text).toMatch(/some interesting title/)
})
it('should reply on API request successfully', async () => {
const content = await renderViaHTTP(appPort, '/api/hello')
expect(content).toMatch(/hello world/)
})
it('should reply on dynamic API request successfully', async () => {
const result = await renderViaHTTP(appPort, '/api/posts/post-1')
const { post } = JSON.parse(result)
expect(post).toBe('post-1')
})
describe('With basic usage', () => {
it('should allow etag header support', async () => {
const url = `http://localhost:${appPort}/`
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册