test that massive chunked encoding chunks behave sensibly (#1407)

Motivation:

Was talking about chunked encoding with @adtrevor, so I thought let's
actually capture it in a test.

Modifications:

- add a test

Result:

- more tests
This commit is contained in:
Johannes Weiss 2020-02-19 13:24:24 +00:00 committed by GitHub
parent 1832cff669
commit 4022c91507
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 0 deletions

View File

@ -56,6 +56,7 @@ extension HTTPDecoderTest {
("testOneRequestTwoResponses", testOneRequestTwoResponses),
("testRefusesRequestSmugglingAttempt", testRefusesRequestSmugglingAttempt),
("testTrimsTrailingOWS", testTrimsTrailingOWS),
("testMassiveChunkDoesNotBufferAndGivesUsHoweverMuchIsAvailable", testMassiveChunkDoesNotBufferAndGivesUsHoweverMuchIsAvailable),
]
}
}

View File

@ -874,4 +874,33 @@ class HTTPDecoderTest: XCTestCase {
return
}
}
func testMassiveChunkDoesNotBufferAndGivesUsHoweverMuchIsAvailable() {
XCTAssertNoThrow(try self.channel.pipeline.addHandler(ByteToMessageHandler(HTTPRequestDecoder())).wait())
var buffer = self.channel.allocator.buffer(capacity: 64)
buffer.writeString("POST / HTTP/1.1\r\nHost: localhost\r\ntransfer-encoding: chunked\r\n\r\n" +
"FFFFFFFFFFFFF\r\nfoo")
XCTAssertNoThrow(try self.channel.writeInbound(buffer))
var maybeHead: HTTPServerRequestPart?
var maybeBodyChunk: HTTPServerRequestPart?
XCTAssertNoThrow(maybeHead = try self.channel.readInbound())
XCTAssertNoThrow(maybeBodyChunk = try self.channel.readInbound())
XCTAssertNoThrow(XCTAssertNil(try self.channel.readInbound(as: HTTPServerRequestPart.self)))
guard case .some(.head(let head)) = maybeHead, case .some(.body(let body)) = maybeBodyChunk else {
XCTFail("didn't receive head & body")
return
}
XCTAssertEqual(.POST, head.method)
XCTAssertEqual("foo", String(decoding: body.readableBytesView, as: Unicode.UTF8.self))
XCTAssertThrowsError(try self.channel.finish()) { error in
XCTAssertEqual(.invalidEOFState, error as? HTTPParserError)
}
}
}