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