-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apollo-server-lambda: Test against a mock ALB; upgrade to fix it (#5291)
We want ASL to work behind ALB, not just API Gateway, so let's test against both. Upgrading `@vendia/serverless-express` gives us better support for ALB query strings. Based on #5290 by @kamaz. Co-authored-by: Kamaz <kamaz@xsimple.org>
- Loading branch information
Showing
7 changed files
with
87 additions
and
38 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
2 changes: 1 addition & 1 deletion
2
packages/apollo-server-lambda/src/__tests__/ApolloServer.test.ts
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
53 changes: 53 additions & 0 deletions
53
packages/apollo-server-lambda/src/__tests__/mockALBServer.ts
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,53 @@ | ||
import url from 'url'; | ||
import { IncomingMessage, ServerResponse } from 'http'; | ||
import { | ||
Context as LambdaContext, | ||
ALBHandler, | ||
ALBEvent, | ||
ALBResult, | ||
} from 'aws-lambda'; | ||
|
||
export function createMockServer(handler: ALBHandler) { | ||
return (req: IncomingMessage, res: ServerResponse) => { | ||
let body = ''; | ||
req.on('data', (chunk) => (body += chunk)); | ||
// this is an unawaited async function, but anything that causes it to | ||
// reject should cause a test to fail | ||
req.on('end', async () => { | ||
const urlObject = url.parse(req.url || '', true); | ||
const queryStringParameters = Object.entries(urlObject.query) | ||
.map(([key, value]) => { | ||
return [key, encodeURIComponent(value as string)]; | ||
}) | ||
.reduce((acc, [key, value]) => ({ [key]: value, ...acc }), {}); | ||
const event = { | ||
httpMethod: req.method, | ||
body: body, | ||
path: urlObject.pathname, | ||
queryStringParameters, | ||
requestContext: { | ||
elb: {}, | ||
}, | ||
headers: req.headers, | ||
}; | ||
const result = (await handler( | ||
event as unknown as ALBEvent, | ||
{} as LambdaContext, // the fields aren't actually used now | ||
() => { | ||
throw Error("we don't use callback"); | ||
}, | ||
)) as ALBResult; | ||
res.statusCode = result.statusCode!; | ||
Object.entries(result.multiValueHeaders ?? {}).forEach( | ||
([key, values]) => { | ||
res.setHeader( | ||
key, | ||
values.map((v) => v.toString()), | ||
); | ||
}, | ||
); | ||
res.write(result.body); | ||
res.end(); | ||
}); | ||
}; | ||
} |
File renamed without changes.