-
-
Notifications
You must be signed in to change notification settings - Fork 457
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
There are only a few minor issues with the current implementation to get this going, namely: - Allowing POST, PATCH etc. in Cloudfront. - Checking if the NextJS page is a React component or not. The change to `compatLayer.js` is needed so that if a request without a body is sent to an API the Stream still has some content. Without a `_read() is not implemented` is raised which generates an HTTP 400 response without a body. The stream test case is removed since the stream already has EOF pushed to it. Don't think there is a use case where the input is a stream itself. #116
- Loading branch information
1 parent
1989fe1
commit fe618a7
Showing
9 changed files
with
178 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
.../serverless-nextjs-plugin/__tests__/fixtures/single-api/.next/serverless/pages/api/api.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default (req, res) => {{ test: 'test' }} |
18 changes: 18 additions & 0 deletions
18
packages/serverless-nextjs-plugin/__tests__/fixtures/single-api/serverless.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
service: single-api-fixture | ||
|
||
provider: | ||
name: aws | ||
runtime: nodejs8.10 | ||
|
||
stage: dev | ||
region: eu-west-1 | ||
|
||
plugins: | ||
- index # this works because jest modulePaths adds plugin path, see package.json | ||
|
||
package: | ||
# exclude everything | ||
# page handlers are automatically included by the plugin | ||
exclude: | ||
- ./** |
111 changes: 111 additions & 0 deletions
111
packages/serverless-nextjs-plugin/__tests__/single-api.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
const nextBuild = require("next/dist/build"); | ||
const path = require("path"); | ||
const AdmZip = require("adm-zip"); | ||
const readCloudFormationUpdateTemplate = require("../utils/test/readCloudFormationUpdateTemplate"); | ||
const testableServerless = require("../utils/test/testableServerless"); | ||
|
||
jest.mock("next/dist/build"); | ||
|
||
describe("single api", () => { | ||
const fixturePath = path.join(__dirname, "./fixtures/single-api"); | ||
|
||
let cloudFormationUpdateResources; | ||
|
||
beforeAll(async () => { | ||
nextBuild.default.mockResolvedValue(); | ||
|
||
await testableServerless(fixturePath, "package"); | ||
|
||
const cloudFormationUpdateTemplate = await readCloudFormationUpdateTemplate( | ||
fixturePath | ||
); | ||
|
||
cloudFormationUpdateResources = cloudFormationUpdateTemplate.Resources; | ||
}); | ||
|
||
describe("Page lambda function", () => { | ||
let pageLambda; | ||
|
||
beforeAll(() => { | ||
pageLambda = cloudFormationUpdateResources.ApiDashapiLambdaFunction; | ||
}); | ||
|
||
it("creates lambda resource", () => { | ||
expect(pageLambda).toBeDefined(); | ||
}); | ||
|
||
it("has correct handler", () => { | ||
expect(pageLambda.Properties.Handler).toEqual( | ||
"sls-next-build/api/api.render" | ||
); | ||
}); | ||
}); | ||
|
||
describe("Api Gateway", () => { | ||
let apiGateway; | ||
|
||
beforeAll(() => { | ||
apiGateway = cloudFormationUpdateResources.ApiGatewayRestApi; | ||
}); | ||
|
||
it("creates api resource", () => { | ||
expect(apiGateway).toBeDefined(); | ||
}); | ||
|
||
describe("Page route", () => { | ||
it("creates page route resource with correct path", () => { | ||
const apiResource = cloudFormationUpdateResources.ApiGatewayResourceApi; | ||
|
||
const apiPostResource = | ||
cloudFormationUpdateResources.ApiGatewayResourceApiApi; | ||
|
||
expect(apiResource).toBeDefined(); | ||
expect(apiPostResource).toBeDefined(); | ||
expect(apiResource.Properties.PathPart).toEqual("api"); | ||
expect(apiPostResource.Properties.PathPart).toEqual("api"); | ||
}); | ||
|
||
it("creates GET http method", () => { | ||
const httpMethod = | ||
cloudFormationUpdateResources.ApiGatewayMethodApiApiGet; | ||
|
||
expect(httpMethod).toBeDefined(); | ||
expect(httpMethod.Properties.HttpMethod).toEqual("GET"); | ||
expect(httpMethod.Properties.ResourceId.Ref).toEqual( | ||
"ApiGatewayResourceApiApi" | ||
); | ||
}); | ||
|
||
it("creates HEAD http method", () => { | ||
const httpMethod = | ||
cloudFormationUpdateResources.ApiGatewayMethodApiApiHead; | ||
|
||
expect(httpMethod).toBeDefined(); | ||
expect(httpMethod.Properties.HttpMethod).toEqual("HEAD"); | ||
expect(httpMethod.Properties.ResourceId.Ref).toEqual( | ||
"ApiGatewayResourceApiApi" | ||
); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("Zip artifact", () => { | ||
let zipEntryNames; | ||
|
||
beforeAll(() => { | ||
const zip = new AdmZip( | ||
`${fixturePath}/.serverless/single-api-fixture.zip` | ||
); | ||
const zipEntries = zip.getEntries(); | ||
zipEntryNames = zipEntries.map(ze => ze.entryName); | ||
}); | ||
|
||
it("contains next compiled page", () => { | ||
expect(zipEntryNames).toContain(`sls-next-build/api/api.original.js`); | ||
}); | ||
|
||
it("contains plugin handler", () => { | ||
expect(zipEntryNames).toContain(`sls-next-build/api/api.js`); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters