|
1 | 1 | import { HttpRequest } from "@aws-sdk/protocol-http";
|
| 2 | +import { rejects } from "assert"; |
| 3 | +import { constants, Http2Stream } from "http2"; |
2 | 4 |
|
3 | 5 | import { NodeHttp2Handler } from "./node-http2-handler";
|
4 | 6 | import { createMockHttp2Server, createResponseFunction } from "./server.mock";
|
@@ -100,6 +102,113 @@ describe("NodeHttp2Handler", () => {
|
100 | 102 | mockH2Server2.close();
|
101 | 103 | });
|
102 | 104 |
|
| 105 | + const UNEXPECTEDLY_CLOSED_REGEX = /closed|destroy|cancel|did not get a response/i; |
| 106 | + it("handles goaway frames", async () => { |
| 107 | + const port3 = port + 2; |
| 108 | + const mockH2Server3 = createMockHttp2Server().listen(port3); |
| 109 | + let establishedConnections = 0; |
| 110 | + let numRequests = 0; |
| 111 | + let shouldSendGoAway = true; |
| 112 | + |
| 113 | + mockH2Server3.on("stream", (request: Http2Stream) => { |
| 114 | + // transmit goaway frame without shutting down the connection |
| 115 | + // to simulate an unlikely error mode. |
| 116 | + numRequests += 1; |
| 117 | + if (shouldSendGoAway) { |
| 118 | + request.session.goaway(constants.NGHTTP2_PROTOCOL_ERROR); |
| 119 | + } |
| 120 | + }); |
| 121 | + mockH2Server3.on("connection", () => { |
| 122 | + establishedConnections += 1; |
| 123 | + }); |
| 124 | + const req = new HttpRequest({ ...getMockReqOptions(), port: port3 }); |
| 125 | + expect(establishedConnections).toBe(0); |
| 126 | + expect(numRequests).toBe(0); |
| 127 | + await rejects( |
| 128 | + nodeH2Handler.handle(req, {}), |
| 129 | + UNEXPECTEDLY_CLOSED_REGEX, |
| 130 | + "should be rejected promptly due to goaway frame" |
| 131 | + ); |
| 132 | + expect(establishedConnections).toBe(1); |
| 133 | + expect(numRequests).toBe(1); |
| 134 | + await rejects( |
| 135 | + nodeH2Handler.handle(req, {}), |
| 136 | + UNEXPECTEDLY_CLOSED_REGEX, |
| 137 | + "should be rejected promptly due to goaway frame" |
| 138 | + ); |
| 139 | + expect(establishedConnections).toBe(2); |
| 140 | + expect(numRequests).toBe(2); |
| 141 | + await rejects( |
| 142 | + nodeH2Handler.handle(req, {}), |
| 143 | + UNEXPECTEDLY_CLOSED_REGEX, |
| 144 | + "should be rejected promptly due to goaway frame" |
| 145 | + ); |
| 146 | + expect(establishedConnections).toBe(3); |
| 147 | + expect(numRequests).toBe(3); |
| 148 | + |
| 149 | + // should be able to recover from goaway after reconnecting to a server |
| 150 | + // that doesn't send goaway, and reuse the TCP connection (Http2Session) |
| 151 | + shouldSendGoAway = false; |
| 152 | + mockH2Server3.on("request", createResponseFunction(mockResponse)); |
| 153 | + await nodeH2Handler.handle(req, {}); |
| 154 | + const result = await nodeH2Handler.handle(req, {}); |
| 155 | + const resultReader = result.response.body; |
| 156 | + |
| 157 | + // ...and validate that the mocked response is received |
| 158 | + const responseBody = await new Promise((resolve) => { |
| 159 | + const buffers = []; |
| 160 | + resultReader.on("data", (chunk) => buffers.push(chunk)); |
| 161 | + resultReader.on("end", () => { |
| 162 | + resolve(Buffer.concat(buffers).toString("utf8")); |
| 163 | + }); |
| 164 | + }); |
| 165 | + expect(responseBody).toBe("test"); |
| 166 | + expect(establishedConnections).toBe(4); |
| 167 | + expect(numRequests).toBe(5); |
| 168 | + mockH2Server3.close(); |
| 169 | + }); |
| 170 | + |
| 171 | + it("handles connections destroyed by servers", async () => { |
| 172 | + const port3 = port + 2; |
| 173 | + const mockH2Server3 = createMockHttp2Server().listen(port3); |
| 174 | + let establishedConnections = 0; |
| 175 | + let numRequests = 0; |
| 176 | + |
| 177 | + mockH2Server3.on("stream", (request: Http2Stream) => { |
| 178 | + // transmit goaway frame and then shut down the connection. |
| 179 | + numRequests += 1; |
| 180 | + request.session.destroy(); |
| 181 | + }); |
| 182 | + mockH2Server3.on("connection", () => { |
| 183 | + establishedConnections += 1; |
| 184 | + }); |
| 185 | + const req = new HttpRequest({ ...getMockReqOptions(), port: port3 }); |
| 186 | + expect(establishedConnections).toBe(0); |
| 187 | + expect(numRequests).toBe(0); |
| 188 | + await rejects( |
| 189 | + nodeH2Handler.handle(req, {}), |
| 190 | + UNEXPECTEDLY_CLOSED_REGEX, |
| 191 | + "should be rejected promptly due to goaway frame or destroyed connection" |
| 192 | + ); |
| 193 | + expect(establishedConnections).toBe(1); |
| 194 | + expect(numRequests).toBe(1); |
| 195 | + await rejects( |
| 196 | + nodeH2Handler.handle(req, {}), |
| 197 | + UNEXPECTEDLY_CLOSED_REGEX, |
| 198 | + "should be rejected promptly due to goaway frame or destroyed connection" |
| 199 | + ); |
| 200 | + expect(establishedConnections).toBe(2); |
| 201 | + expect(numRequests).toBe(2); |
| 202 | + await rejects( |
| 203 | + nodeH2Handler.handle(req, {}), |
| 204 | + UNEXPECTEDLY_CLOSED_REGEX, |
| 205 | + "should be rejected promptly due to goaway frame or destroyed connection" |
| 206 | + ); |
| 207 | + expect(establishedConnections).toBe(3); |
| 208 | + expect(numRequests).toBe(3); |
| 209 | + mockH2Server3.close(); |
| 210 | + }); |
| 211 | + |
103 | 212 | it("closes and removes session on sessionTimeout", async (done) => {
|
104 | 213 | const sessionTimeout = 500;
|
105 | 214 | nodeH2Handler = new NodeHttp2Handler({ sessionTimeout });
|
|
0 commit comments