Commit 78bba565 authored by JJ Kasper's avatar JJ Kasper Committed by Tim Neutkens

Add API tests to serverless (#7774)

* Add failing API tests to serverless

* Add newline

* Fix failing serverless tests
parent 52056955
...@@ -58,7 +58,7 @@ export default class Server { ...@@ -58,7 +58,7 @@ export default class Server {
nextConfig: NextConfig nextConfig: NextConfig
distDir: string distDir: string
publicDir: string publicDir: string
buildManifest: string pagesManifest: string
buildId: string buildId: string
renderOpts: { renderOpts: {
poweredByHeader: boolean poweredByHeader: boolean
...@@ -88,7 +88,11 @@ export default class Server { ...@@ -88,7 +88,11 @@ export default class Server {
this.distDir = join(this.dir, this.nextConfig.distDir) this.distDir = join(this.dir, this.nextConfig.distDir)
// this.pagesDir = join(this.dir, 'pages') // this.pagesDir = join(this.dir, 'pages')
this.publicDir = join(this.dir, CLIENT_PUBLIC_FILES_PATH) 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 // Only serverRuntimeConfig needs the default
// publicRuntimeConfig gets it's default in client/index.js // publicRuntimeConfig gets it's default in client/index.js
...@@ -278,8 +282,12 @@ export default class Server { ...@@ -278,8 +282,12 @@ export default class Server {
pathname: string pathname: string
) { ) {
let params: Params | boolean = false let params: Params | boolean = false
let resolverFunction: any
try {
resolverFunction = await this.resolveApiRequest(pathname)
} catch (err) {}
let resolverFunction = await this.resolveApiRequest(pathname)
if ( if (
this.dynamicRoutes && this.dynamicRoutes &&
this.dynamicRoutes.length > 0 && this.dynamicRoutes.length > 0 &&
...@@ -294,6 +302,17 @@ export default class Server { ...@@ -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( apiResolver(
req, req,
res, res,
...@@ -344,10 +363,8 @@ export default class Server { ...@@ -344,10 +363,8 @@ export default class Server {
} }
private getDynamicRoutes() { private getDynamicRoutes() {
const manifest = require(this.buildManifest) const manifest = require(this.pagesManifest)
const dynamicRoutedPages = Object.keys(manifest.pages).filter( const dynamicRoutedPages = Object.keys(manifest).filter(isDynamicRoute)
isDynamicRoute
)
return getSortedRoutes(dynamicRoutedPages).map(page => ({ return getSortedRoutes(dynamicRoutedPages).map(page => ({
page, page,
match: getRouteMatcher(getRouteRegex(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', () => { ...@@ -133,6 +133,17 @@ describe('Serverless', () => {
expect(text).toMatch(/some interesting title/) 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', () => { describe('With basic usage', () => {
it('should allow etag header support', async () => { it('should allow etag header support', async () => {
const url = `http://localhost:${appPort}/` const url = `http://localhost:${appPort}/`
......
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