|
| 1 | +import { NodeHttp2Handler } from "./node-http2-handler"; |
| 2 | +import { HttpRequest } from "@aws-sdk/protocol-http"; |
| 3 | +import { createMockHttp2Server, createResponseFunction } from "./server.mock"; |
| 4 | +import { AbortController } from "@aws-sdk/abort-controller"; |
| 5 | + |
| 6 | +describe("NodeHttp2Handler", () => { |
| 7 | + let nodeH2Handler: NodeHttp2Handler; |
| 8 | + |
| 9 | + const protocol = "http:"; |
| 10 | + const hostname = "localhost"; |
| 11 | + const port = 45321; |
| 12 | + const mockH2Server = createMockHttp2Server().listen(port); |
| 13 | + const getMockReqOptions = () => ({ |
| 14 | + protocol, |
| 15 | + hostname, |
| 16 | + port, |
| 17 | + method: "GET", |
| 18 | + path: "/", |
| 19 | + headers: {} |
| 20 | + }); |
| 21 | + |
| 22 | + const mockResponse = { |
| 23 | + statusCode: 200, |
| 24 | + headers: {}, |
| 25 | + body: "test" |
| 26 | + }; |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + nodeH2Handler = new NodeHttp2Handler(); |
| 30 | + mockH2Server.on("request", createResponseFunction(mockResponse)); |
| 31 | + }); |
| 32 | + |
| 33 | + afterEach(() => { |
| 34 | + mockH2Server.removeAllListeners("request"); |
| 35 | + // @ts-ignore: access private property |
| 36 | + const connectionPool = nodeH2Handler.connectionPool; |
| 37 | + for (const [, session] of connectionPool) { |
| 38 | + session.destroy(); |
| 39 | + } |
| 40 | + connectionPool.clear(); |
| 41 | + }); |
| 42 | + |
| 43 | + afterAll(() => { |
| 44 | + mockH2Server.close(); |
| 45 | + }); |
| 46 | + |
| 47 | + describe("connectionPool", () => { |
| 48 | + it("is empty on initialization", () => { |
| 49 | + // @ts-ignore: access private property |
| 50 | + expect(nodeH2Handler.connectionPool.size).toBe(0); |
| 51 | + }); |
| 52 | + |
| 53 | + it("creates and stores session when request is made", async () => { |
| 54 | + await nodeH2Handler.handle(new HttpRequest(getMockReqOptions()), {}); |
| 55 | + |
| 56 | + // @ts-ignore: access private property |
| 57 | + expect(nodeH2Handler.connectionPool.size).toBe(1); |
| 58 | + expect( |
| 59 | + // @ts-ignore: access private property |
| 60 | + nodeH2Handler.connectionPool.get(`${protocol}//${hostname}:${port}`) |
| 61 | + ).toBeDefined(); |
| 62 | + }); |
| 63 | + |
| 64 | + it("reuses existing session if request is made on same authority again", async () => { |
| 65 | + await nodeH2Handler.handle(new HttpRequest(getMockReqOptions()), {}); |
| 66 | + // @ts-ignore: access private property |
| 67 | + expect(nodeH2Handler.connectionPool.size).toBe(1); |
| 68 | + |
| 69 | + // @ts-ignore: access private property |
| 70 | + const session: ClientHttp2Session = nodeH2Handler.connectionPool.get( |
| 71 | + `${protocol}//${hostname}:${port}` |
| 72 | + ); |
| 73 | + const requestSpy = jest.spyOn(session, "request"); |
| 74 | + |
| 75 | + await nodeH2Handler.handle(new HttpRequest(getMockReqOptions()), {}); |
| 76 | + // @ts-ignore: access private property |
| 77 | + expect(nodeH2Handler.connectionPool.size).toBe(1); |
| 78 | + expect(requestSpy.mock.calls.length).toBe(1); |
| 79 | + }); |
| 80 | + |
| 81 | + it("creates new session if request is made on new authority", async () => { |
| 82 | + await nodeH2Handler.handle(new HttpRequest(getMockReqOptions()), {}); |
| 83 | + // @ts-ignore: access private property |
| 84 | + expect(nodeH2Handler.connectionPool.size).toBe(1); |
| 85 | + |
| 86 | + const port2 = port + 1; |
| 87 | + const mockH2Server2 = createMockHttp2Server().listen(port2); |
| 88 | + mockH2Server2.on("request", createResponseFunction(mockResponse)); |
| 89 | + |
| 90 | + await nodeH2Handler.handle( |
| 91 | + new HttpRequest({ ...getMockReqOptions(), port: port2 }), |
| 92 | + {} |
| 93 | + ); |
| 94 | + // @ts-ignore: access private property |
| 95 | + expect(nodeH2Handler.connectionPool.size).toBe(2); |
| 96 | + expect( |
| 97 | + // @ts-ignore: access private property |
| 98 | + nodeH2Handler.connectionPool.get(`${protocol}//${hostname}:${port2}`) |
| 99 | + ).toBeDefined(); |
| 100 | + |
| 101 | + mockH2Server2.close(); |
| 102 | + }); |
| 103 | + |
| 104 | + it("closes and removes session on sessionTimeout", async done => { |
| 105 | + const sessionTimeout = 500; |
| 106 | + nodeH2Handler = new NodeHttp2Handler({ sessionTimeout }); |
| 107 | + await nodeH2Handler.handle(new HttpRequest(getMockReqOptions()), {}); |
| 108 | + |
| 109 | + const authority = `${protocol}//${hostname}:${port}`; |
| 110 | + // @ts-ignore: access private property |
| 111 | + const session: ClientHttp2Session = nodeH2Handler.connectionPool.get( |
| 112 | + authority |
| 113 | + ); |
| 114 | + expect(session.closed).toBe(false); |
| 115 | + setTimeout(() => { |
| 116 | + expect(session.closed).toBe(true); |
| 117 | + // @ts-ignore: access private property |
| 118 | + expect(nodeH2Handler.connectionPool.get(authority)).not.toBeDefined(); |
| 119 | + done(); |
| 120 | + }, sessionTimeout + 100); |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + describe("destroy", () => { |
| 125 | + it("destroys sessions and clears connectionPool", async () => { |
| 126 | + await nodeH2Handler.handle(new HttpRequest(getMockReqOptions()), {}); |
| 127 | + |
| 128 | + // @ts-ignore: access private property |
| 129 | + const session: ClientHttp2Session = nodeH2Handler.connectionPool.get( |
| 130 | + `${protocol}//${hostname}:${port}` |
| 131 | + ); |
| 132 | + |
| 133 | + // @ts-ignore: access private property |
| 134 | + expect(nodeH2Handler.connectionPool.size).toBe(1); |
| 135 | + expect(session.destroyed).toBe(false); |
| 136 | + nodeH2Handler.destroy(); |
| 137 | + // @ts-ignore: access private property |
| 138 | + expect(nodeH2Handler.connectionPool.size).toBe(0); |
| 139 | + expect(session.destroyed).toBe(true); |
| 140 | + }); |
| 141 | + }); |
| 142 | + |
| 143 | + describe("abortSignal", () => { |
| 144 | + it("will not create session if request already aborted", async () => { |
| 145 | + // @ts-ignore: access private property |
| 146 | + expect(nodeH2Handler.connectionPool.size).toBe(0); |
| 147 | + await expect( |
| 148 | + nodeH2Handler.handle(new HttpRequest(getMockReqOptions()), { |
| 149 | + abortSignal: { |
| 150 | + aborted: true |
| 151 | + } |
| 152 | + }) |
| 153 | + ).rejects.toHaveProperty("name", "AbortError"); |
| 154 | + // @ts-ignore: access private property |
| 155 | + expect(nodeH2Handler.connectionPool.size).toBe(0); |
| 156 | + }); |
| 157 | + |
| 158 | + it("will not create request on session if request already aborted", async () => { |
| 159 | + await nodeH2Handler.handle(new HttpRequest(getMockReqOptions()), {}); |
| 160 | + |
| 161 | + // @ts-ignore: access private property |
| 162 | + const session: ClientHttp2Session = nodeH2Handler.connectionPool.get( |
| 163 | + `${protocol}//${hostname}:${port}` |
| 164 | + ); |
| 165 | + const requestSpy = jest.spyOn(session, "request"); |
| 166 | + |
| 167 | + await expect( |
| 168 | + nodeH2Handler.handle(new HttpRequest(getMockReqOptions()), { |
| 169 | + abortSignal: { |
| 170 | + aborted: true |
| 171 | + } |
| 172 | + }) |
| 173 | + ).rejects.toHaveProperty("name", "AbortError"); |
| 174 | + expect(requestSpy.mock.calls.length).toBe(0); |
| 175 | + }); |
| 176 | + |
| 177 | + it("will close request on session when aborted", async () => { |
| 178 | + await nodeH2Handler.handle(new HttpRequest(getMockReqOptions()), {}); |
| 179 | + |
| 180 | + // @ts-ignore: access private property |
| 181 | + const session: ClientHttp2Session = nodeH2Handler.connectionPool.get( |
| 182 | + `${protocol}//${hostname}:${port}` |
| 183 | + ); |
| 184 | + const requestSpy = jest.spyOn(session, "request"); |
| 185 | + |
| 186 | + const abortController = new AbortController(); |
| 187 | + // Delay response so that onabort is called earlier |
| 188 | + setTimeout(() => { |
| 189 | + abortController.abort(); |
| 190 | + }, 0); |
| 191 | + mockH2Server.on( |
| 192 | + "request", |
| 193 | + async () => |
| 194 | + new Promise(resolve => { |
| 195 | + setTimeout(() => { |
| 196 | + resolve(createResponseFunction(mockResponse)); |
| 197 | + }, 1000); |
| 198 | + }) |
| 199 | + ); |
| 200 | + |
| 201 | + await expect( |
| 202 | + nodeH2Handler.handle(new HttpRequest(getMockReqOptions()), { |
| 203 | + abortSignal: abortController.signal |
| 204 | + }) |
| 205 | + ).rejects.toHaveProperty("name", "AbortError"); |
| 206 | + expect(requestSpy.mock.calls.length).toBe(1); |
| 207 | + }); |
| 208 | + }); |
| 209 | +}); |
0 commit comments