-
Notifications
You must be signed in to change notification settings - Fork 105
Add tests for convertRequest (NodeJS sSDK runtime). #711
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
133
smithy-typescript-ssdk-libs/server-node/src/index.spec.ts
This file contains hidden or 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,133 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { mkdtemp } from "fs/promises"; | ||
import { createServer, IncomingMessage, request, RequestOptions, Server, ServerResponse } from "http"; | ||
import * as os from "os"; | ||
import * as path from "path"; | ||
import { Readable } from "stream"; | ||
|
||
import { convertRequest } from "./node"; | ||
|
||
let socketPath: string; | ||
let promiseResolve: ([req, res]: [IncomingMessage, ServerResponse]) => void; | ||
|
||
let server: Server; | ||
beforeAll(async () => { | ||
server = createServer(function (req, res) { | ||
promiseResolve([req, res]); | ||
resToEnd = res; | ||
}); | ||
// Create a temporary named pipe where to run the server and obtain a request | ||
socketPath = path.join(await mkdtemp(path.join(os.tmpdir(), "named-pipe-for-test-")), "server"); | ||
// TODO Add support to Windows by using '\\\\?\\pipe' | ||
// See: https://nodejs.org/api/net.html#identifying-paths-for-ipc-connections | ||
server.listen(socketPath); | ||
}); | ||
|
||
let resToEnd: ServerResponse; | ||
pose marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
function getRequest(options: RequestOptions & { body?: String }): Promise<[IncomingMessage, ServerResponse]> { | ||
pose marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return new Promise((resolve, _) => { | ||
promiseResolve = resolve; | ||
request({ | ||
socketPath, | ||
...options, | ||
}).end(Buffer.from(options.body || [])); | ||
}); | ||
} | ||
|
||
afterAll(() => { | ||
server?.close(); | ||
}); | ||
|
||
async function streamToString(stream: Readable) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @aws-sdk/node-http-handler streamCollector? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the suggestion. I see the following caveats:
@kuhe what do you think? |
||
const chunks = []; | ||
|
||
for await (const chunk of stream) { | ||
chunks.push(Buffer.from(chunk)); | ||
} | ||
|
||
return Buffer.concat(chunks).toString("utf-8"); | ||
} | ||
|
||
describe("convertRequest", () => { | ||
afterEach(async () => { | ||
resToEnd?.end(); | ||
}); | ||
it("converts a simple GET / correctly", async () => { | ||
const [req, _] = await getRequest({ | ||
host: "example.com", | ||
path: "/", | ||
}); | ||
|
||
const convertedReq = convertRequest(req); | ||
expect(convertedReq.hostname).toEqual("example.com"); | ||
expect(convertedReq.method).toEqual("GET"); | ||
expect(convertedReq.path).toEqual("/"); | ||
expect(convertedReq.protocol).toEqual("http:"); | ||
expect(convertedReq.query).toEqual({}); | ||
expect(convertedReq.headers).toEqual({ | ||
connection: "close", | ||
host: "example.com", | ||
}); | ||
expect(await streamToString(convertedReq.body)).toEqual(""); | ||
}); | ||
it("converts a POST with query string correctly", async () => { | ||
const [req, _] = await getRequest({ | ||
method: "POST", | ||
host: "example.com", | ||
path: "/some/endpoint?q=hello&a=world", | ||
body: "hello", | ||
}); | ||
|
||
const convertedReq = convertRequest(req); | ||
expect(convertedReq.hostname).toEqual("example.com"); | ||
expect(convertedReq.method).toEqual("POST"); | ||
expect(convertedReq.path).toEqual("/some/endpoint"); | ||
expect(convertedReq.protocol).toEqual("http:"); | ||
expect(convertedReq.query).toEqual({ | ||
q: "hello", | ||
a: "world", | ||
}); | ||
expect(convertedReq.headers).toEqual({ | ||
connection: "close", | ||
host: "example.com", | ||
"content-length": "5", | ||
}); | ||
expect(await streamToString(convertedReq.body)).toEqual("hello"); | ||
}); | ||
it("converts OPTIONS CORS requests", async () => { | ||
const [req, _] = await getRequest({ | ||
method: "OPTIONS", | ||
host: "example.com", | ||
path: "/some/resource", | ||
headers: { | ||
"Access-Control-Request-Method": "DELETE", | ||
"Access-Control-Request-Headers": "origin, x-requested-with", | ||
Origin: "https://example.com", | ||
}, | ||
}); | ||
const convertedReq = convertRequest(req); | ||
expect(convertedReq.hostname).toEqual("example.com"); | ||
expect(convertedReq.method).toEqual("OPTIONS"); | ||
expect(convertedReq.path).toEqual("/some/resource"); | ||
expect(convertedReq.protocol).toEqual("http:"); | ||
expect(convertedReq.query).toEqual({}); | ||
expect(convertedReq.headers).toEqual({ | ||
"access-control-request-headers": "origin, x-requested-with", | ||
"access-control-request-method": "DELETE", | ||
origin: "https://example.com", | ||
connection: "close", | ||
host: "example.com", | ||
}); | ||
expect(await streamToString(convertedReq.body)).toEqual(""); | ||
}); | ||
}); | ||
|
||
// TODO Implement writeResponse tests | ||
// describe("writeResponse", () => { | ||
// it("converts a simple GET / correctly", async () => {}); | ||
// }); |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why listen to a socket rather than a port? port 0 would bind to any free port.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a huge fan of tests that depend on the TCP stack in general (probably since I have had clash with already used ports many times). It's also true that I can bind to the socket (as you describe) and it's probably going to be still quite fast. Do you think I'm losing performance or how realistic is the scenario under test?