Skip to content

fix(credential-provider-imds): destroy request handle on promise resolve/reject #2452

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 6 commits into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { ProviderError } from "@aws-sdk/property-provider";
import { createServer } from "http";
import http, { createServer } from "http";
import nock from "nock";

import { httpRequest } from "./httpRequest";

describe("httpRequest", () => {
const requestSpy = jest.spyOn(http, "request");
let port: number;
const host = "localhost";
const path = "/";
Expand All @@ -26,13 +27,18 @@ describe("httpRequest", () => {
port = await getOpenPort();
});

afterEach(() => {
jest.clearAllMocks();
});

describe("returns response", () => {
it("defaults to method GET", async () => {
const expectedResponse = "expectedResponse";
const scope = nock(`http://${host}:${port}`).get(path).reply(200, expectedResponse);

const response = await httpRequest({ host, path, port });
expect(response.toString()).toStrictEqual(expectedResponse);
expect(requestSpy.mock.results[0].value.socket).toHaveProperty("destroyed", true);

scope.done();
});
Expand All @@ -44,6 +50,7 @@ describe("httpRequest", () => {

const response = await httpRequest({ host, path, port, method });
expect(response.toString()).toStrictEqual(expectedResponse);
expect(requestSpy.mock.results[0].value.socket).toHaveProperty("destroyed", true);

scope.done();
});
Expand All @@ -57,6 +64,7 @@ describe("httpRequest", () => {
await expect(httpRequest({ host, path, port })).rejects.toStrictEqual(
Object.assign(new ProviderError("Error response received from instance metadata service"), { statusCode })
);
expect(requestSpy.mock.results[0].value.socket).toHaveProperty("destroyed", true);

scope.done();
});
Expand All @@ -68,6 +76,7 @@ describe("httpRequest", () => {
await expect(httpRequest({ host, path, port })).rejects.toStrictEqual(
new ProviderError("Unable to connect to instance metadata service")
);
expect(requestSpy.mock.results[0].value.socket).toHaveProperty("destroyed", true);

scope.done();
});
Expand All @@ -91,6 +100,7 @@ describe("httpRequest", () => {
await expect(httpRequest({ host, path, port, timeout })).rejects.toStrictEqual(
new ProviderError("TimeoutError from instance metadata service")
);
expect(requestSpy.mock.results[0].value.socket).toHaveProperty("destroyed", true);

scope.done();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ export function httpRequest(options: RequestOptions): Promise<Buffer> {

req.on("error", (err) => {
reject(Object.assign(new ProviderError("Unable to connect to instance metadata service"), err));
req.destroy();
});

req.on("timeout", () => {
reject(new ProviderError("TimeoutError from instance metadata service"));
req.destroy();
});

req.on("response", (res: IncomingMessage) => {
Expand All @@ -23,6 +25,7 @@ export function httpRequest(options: RequestOptions): Promise<Buffer> {
reject(
Object.assign(new ProviderError("Error response received from instance metadata service"), { statusCode })
);
req.destroy();
}

const chunks: Array<Buffer> = [];
Expand All @@ -31,6 +34,7 @@ export function httpRequest(options: RequestOptions): Promise<Buffer> {
});
res.on("end", () => {
resolve(Buffer.concat(chunks));
req.destroy();
});
});

Expand Down