Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it("will close request on session when aborted", async () => {
await nodeH2Handler.handle(new HttpRequest(getMockReqOptions()), {});
// @ts-ignore: access private property
const session: ClientHttp2Session = nodeH2Handler.connectionPool.get(
`${protocol}//${hostname}:${port}`
);
const requestSpy = jest.spyOn(session, "request");
const abortController = new AbortController();
// Delay response so that onabort is called earlier
setTimeout(() => {
abortController.abort();
}, 0);
mockH2Server.on(
"request",
async () =>
new Promise(resolve => {
setTimeout(() => {
resolve(createResponseFunction(mockResponse));
}, 1000);
})
);
await expect(
nodeH2Handler.handle(new HttpRequest(getMockReqOptions()), {
it("can be aborted before fetch completes", async () => {
const abortController = new AbortController();
const mockFetch = jest.fn(() => {
return new Promise((resolve, reject) => {});
});
(global as any).fetch = mockFetch;
setTimeout(() => {
abortController.abort();
}, 100);
const fetchHttpHandler = new FetchHttpHandler();
await expect(
fetchHttpHandler.handle({} as any, {
abortSignal: abortController.signal
})
).rejects.toHaveProperty("name", "AbortError");
createResponseFunction(mockResponse)
);
let httpRequest: http.ClientRequest;
let reqAbortSpy: any;
const spy = jest.spyOn(https, "request").mockImplementationOnce(() => {
let calls = spy.mock.calls;
let currentIndex = calls.length - 1;
httpRequest = https.request(
calls[currentIndex][0],
calls[currentIndex][1]
);
reqAbortSpy = jest.spyOn(httpRequest, "abort");
return httpRequest;
});
const nodeHttpHandler = new NodeHttpHandler();
const abortController = new AbortController();
setTimeout(() => {
abortController.abort();
}, 0);
await expect(
nodeHttpHandler.handle(
new HttpRequest({
hostname: "localhost",
method: "GET",
port: (mockHttpsServer.address() as AddressInfo).port,
protocol: "https:",
path: "/",
headers: {}
}),
{