Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: improve tests that are supposed to throw #1430

Merged
merged 1 commit into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 16 additions & 37 deletions Tests/NIOHTTP1Tests/HTTPDecoderLengthTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -443,13 +443,9 @@ class HTTPDecoderLengthTest: XCTestCase {
XCTAssertNoThrow(try channel.pipeline.addHandler(ByteToMessageHandler(HTTPRequestDecoder())).wait())

// Send a GET with the invalid headers.
do {
try channel.writeInbound(ByteBuffer(string: "POST / HTTP/1.1\r\nTransfer-Encoding: chunked\r\nContent-Length: 4\r\n\r\n"))
XCTFail("Did not throw")
} catch HTTPParserError.unexpectedContentLength {
// ok
} catch {
XCTFail("Unexpected error: \(error)")
let request = ByteBuffer(string: "POST / HTTP/1.1\r\nTransfer-Encoding: chunked\r\nContent-Length: 4\r\n\r\n")
XCTAssertThrowsError(try channel.writeInbound(request)) { error in
XCTAssertEqual(HTTPParserError.unexpectedContentLength, error as? HTTPParserError)
}

// Must spin the loop.
Expand All @@ -467,13 +463,9 @@ class HTTPDecoderLengthTest: XCTestCase {
uri: "/"))).isFull)

// Send a 200 OK with the invalid headers.
do {
try channel.writeInbound(ByteBuffer(string: "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\nContent-Length: 4\r\n\r\n"))
XCTFail("Did not throw")
} catch HTTPParserError.unexpectedContentLength {
// ok
} catch {
XCTFail("Unexpected error: \(error)")
let response = "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\nContent-Length: 4\r\n\r\n"
XCTAssertThrowsError(try channel.writeInbound(ByteBuffer(string: response))) { error in
XCTAssertEqual(HTTPParserError.unexpectedContentLength, error as? HTTPParserError)
}

// Must spin the loop.
Expand All @@ -485,15 +477,10 @@ class HTTPDecoderLengthTest: XCTestCase {
XCTAssertNoThrow(try channel.pipeline.addHandler(ByteToMessageHandler(HTTPRequestDecoder())).wait())

// Send a GET with the invalid headers.
do {
try channel.writeInbound(ByteBuffer(string: "POST / HTTP/1.1\r\nContent-Length: \(contentLengthField)\r\n\r\n"))
XCTFail("Did not throw")
} catch HTTPParserError.invalidContentLength {
// ok
} catch HTTPParserError.unexpectedContentLength {
// also ok
} catch {
XCTFail("Unexpected error: \(error)")
let request = "POST / HTTP/1.1\r\nContent-Length: \(contentLengthField)\r\n\r\n"
XCTAssertThrowsError(try channel.writeInbound(ByteBuffer(string: request))) { error in
XCTAssert(HTTPParserError.unexpectedContentLength == error as? HTTPParserError ||
HTTPParserError.invalidContentLength == error as? HTTPParserError)
}

// Must spin the loop.
Expand All @@ -520,13 +507,9 @@ class HTTPDecoderLengthTest: XCTestCase {

// Send two POSTs with repeated content length, one with one field and one with two.
// Both should error.
do {
try channel.writeInbound(ByteBuffer(string: "POST / HTTP/1.1\r\nContent-Length: 4, 4\r\n\r\n"))
XCTFail("Did not throw")
} catch HTTPParserError.invalidContentLength {
// ok
} catch {
XCTFail("Unexpected error \(error)")
let request = "POST / HTTP/1.1\r\nContent-Length: 4, 4\r\n\r\n"
XCTAssertThrowsError(try channel.writeInbound(ByteBuffer(string: request))) { error in
XCTAssertEqual(HTTPParserError.invalidContentLength, error as? HTTPParserError)
}

// Must spin the loop.
Expand All @@ -539,13 +522,9 @@ class HTTPDecoderLengthTest: XCTestCase {
// the spec. Regardless, we match it.
XCTAssertNoThrow(try channel.pipeline.addHandler(ByteToMessageHandler(HTTPRequestDecoder())).wait())

do {
try channel.writeInbound(ByteBuffer(string: "POST / HTTP/1.1\r\nContent-Length: 4\r\nContent-Length: 4\r\n\r\n"))
XCTFail("Did not throw")
} catch HTTPParserError.unexpectedContentLength {
// ok
} catch {
XCTFail("Unexpected error \(error)")
let request = "POST / HTTP/1.1\r\nContent-Length: 4\r\nContent-Length: 4\r\n\r\n"
XCTAssertThrowsError(try channel.writeInbound(ByteBuffer(string: request))) { error in
XCTAssertEqual(HTTPParserError.unexpectedContentLength, error as? HTTPParserError)
}

// Must spin the loop.
Expand Down
83 changes: 24 additions & 59 deletions Tests/NIOHTTP1Tests/HTTPDecoderTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,18 @@ class HTTPDecoderTest: XCTestCase {

func testDoesNotDecodeRealHTTP09Request() throws {
XCTAssertNoThrow(try channel.pipeline.addHandler(ByteToMessageHandler(HTTPRequestDecoder())).wait())

// This is an invalid HTTP/0.9 simple request (too many CRLFs), but we need to
// trigger https://github.com/nodejs/http-parser/issues/386 or http_parser won't
// actually parse this at all.
var buffer = channel.allocator.buffer(capacity: 64)
buffer.writeStaticString("GET /a-file\r\n\r\n")

do {
try channel.writeInbound(buffer)
XCTFail("Did not error")
} catch HTTPParserError.invalidVersion {
// ok
} catch {
XCTFail("Unexpected error: \(error)")

XCTAssertThrowsError(try channel.writeInbound(buffer)) { error in
XCTAssertEqual(.invalidVersion, error as? HTTPParserError)
}

loop.run()
self.loop.run()
}

func testDoesNotDecodeFakeHTTP09Request() throws {
Expand All @@ -60,16 +55,11 @@ class HTTPDecoderTest: XCTestCase {
var buffer = channel.allocator.buffer(capacity: 64)
buffer.writeStaticString("GET / HTTP/0.9\r\nHost: whatever\r\n\r\n")

do {
try channel.writeInbound(buffer)
XCTFail("Did not error")
} catch HTTPParserError.invalidVersion {
// ok
} catch {
XCTFail("Unexpected error: \(error)")
XCTAssertThrowsError(try channel.writeInbound(buffer)) { error in
XCTAssertEqual(.invalidVersion, error as? HTTPParserError)
}

loop.run()
self.loop.run()
}

func testDoesNotDecodeHTTP2XRequest() throws {
Expand All @@ -80,16 +70,11 @@ class HTTPDecoderTest: XCTestCase {
var buffer = channel.allocator.buffer(capacity: 64)
buffer.writeStaticString("GET / HTTP/2.0\r\nHost: whatever\r\n\r\n")

do {
try channel.writeInbound(buffer)
XCTFail("Did not error")
} catch HTTPParserError.invalidVersion {
// ok
} catch {
XCTFail("Unexpected error: \(error)")
XCTAssertThrowsError(try channel.writeInbound(buffer)) { error in
XCTAssertEqual(.invalidVersion, error as? HTTPParserError)
}

loop.run()
self.loop.run()
}

func testToleratesHTTP13Request() throws {
Expand All @@ -116,16 +101,11 @@ class HTTPDecoderTest: XCTestCase {
var buffer = channel.allocator.buffer(capacity: 64)
buffer.writeStaticString("This is file data\n")

do {
try channel.writeInbound(buffer)
XCTFail("Did not error")
} catch HTTPParserError.invalidConstant {
// ok
} catch {
XCTFail("Unexpected error: \(error)")
XCTAssertThrowsError(try channel.writeInbound(buffer)) { error in
XCTAssertEqual(.invalidConstant, error as? HTTPParserError)
}

loop.run()
self.loop.run()
}

func testDoesNotDecodeFakeHTTP09Response() throws {
Expand All @@ -139,16 +119,11 @@ class HTTPDecoderTest: XCTestCase {
var buffer = channel.allocator.buffer(capacity: 64)
buffer.writeStaticString("HTTP/0.9 200 OK\r\nServer: whatever\r\n\r\n")

do {
try channel.writeInbound(buffer)
XCTFail("Did not error")
} catch HTTPParserError.invalidVersion {
// ok
} catch {
XCTFail("Unexpected error: \(error)")
XCTAssertThrowsError(try channel.writeInbound(buffer)) { error in
XCTAssertEqual(.invalidVersion, error as? HTTPParserError)
}

loop.run()
self.loop.run()
}

func testDoesNotDecodeHTTP2XResponse() throws {
Expand All @@ -163,16 +138,11 @@ class HTTPDecoderTest: XCTestCase {
var buffer = channel.allocator.buffer(capacity: 64)
buffer.writeStaticString("HTTP/2.0 200 OK\r\nServer: whatever\r\n\r\n")

do {
try channel.writeInbound(buffer)
XCTFail("Did not error")
} catch HTTPParserError.invalidVersion {
// ok
} catch {
XCTFail("Unexpected error: \(error)")
XCTAssertThrowsError(try channel.writeInbound(buffer)) { error in
XCTAssertEqual(.invalidVersion, error as? HTTPParserError)
}

loop.run()
self.loop.run()
}

func testToleratesHTTP13Response() throws {
Expand Down Expand Up @@ -844,16 +814,11 @@ class HTTPDecoderTest: XCTestCase {
"Transfer-Encoding: gzip, chunked\r\n\r\n" +
"3\r\na=1\r\n0\r\n\r\n")

do {
try channel.writeInbound(buffer)
XCTFail("Did not error")
} catch HTTPParserError.unexpectedContentLength {
// ok
} catch {
XCTFail("Unexpected error: \(error)")
XCTAssertThrowsError(try channel.writeInbound(buffer)) { error in
XCTAssertEqual(.unexpectedContentLength, error as? HTTPParserError)
}

loop.run()
self.loop.run()
}

func testTrimsTrailingOWS() throws {
Expand Down
12 changes: 3 additions & 9 deletions Tests/NIOHTTP1Tests/HTTPRequestEncoderTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,9 @@ class HTTPRequestEncoderTests: XCTestCase {
}

private func assertOutboundContainsOnly(_ channel: EmbeddedChannel, _ expected: String) {
do {
if let buffer = try channel.readOutbound(as: ByteBuffer.self) {
buffer.assertContainsOnly(expected)
} else {
fatalError("Could not read ByteBuffer from channel")
}
} catch {
XCTFail("unexpected error: \(error)")
}
XCTAssertNoThrow(XCTAssertNotNil(try channel.readOutbound(as: ByteBuffer.self).map { buffer in
buffer.assertContainsOnly(expected)
}, "couldn't read ByteBuffer"))
}
}

9 changes: 2 additions & 7 deletions Tests/NIOHTTP1Tests/HTTPServerProtocolErrorHandlerTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,8 @@ class HTTPServerProtocolErrorHandlerTest: XCTestCase {
XCTAssertNoThrow(try channel.pipeline.configureHTTPServerPipeline(withErrorHandling: true).wait())

channel.pipeline.fireErrorCaught(DummyError.error)
do {
try channel.throwIfErrorCaught()
XCTFail("No error caught")
} catch DummyError.error {
// ok
} catch {
XCTFail("Unexpected error: \(error)")
XCTAssertThrowsError(try channel.throwIfErrorCaught()) { error in
XCTAssertEqual(DummyError.error, error as? DummyError)
}

XCTAssertNoThrow(try channel.finish())
Expand Down
41 changes: 10 additions & 31 deletions Tests/NIOHTTP1Tests/HTTPServerUpgradeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,7 @@ extension ChannelPipeline {
}

func assertContains<Handler: ChannelHandler>(handlerType: Handler.Type) throws {
do {
_ = try self.context(handlerType: handlerType).wait()
} catch ChannelPipelineError.notFound {
XCTFail("Did not find handler")
}
XCTAssertNoThrow(try self.context(handlerType: handlerType).wait(), "did not find handler")
}

fileprivate func removeUpgrader() throws {
Expand Down Expand Up @@ -425,11 +421,8 @@ class HTTPServerUpgradeTestCase: XCTestCase {

XCTAssertNoThrow(try channel.pipeline.addHandler(handler).wait())

do {
try channel.writeInbound(data)
XCTFail("Writing of bad data did not error")
} catch HTTPServerUpgradeErrors.invalidHTTPOrdering {
// Nothing to see here.
XCTAssertThrowsError(try channel.writeInbound(data)) { error in
XCTAssertEqual(.invalidHTTPOrdering, error as? HTTPServerUpgradeErrors)
}

// The handler removed itself from the pipeline and passed the unexpected
Expand Down Expand Up @@ -924,13 +917,9 @@ class HTTPServerUpgradeTestCase: XCTestCase {
XCTAssertEqual(upgradingProtocol, "myproto")
XCTAssertNoThrow(try channel.pipeline.assertContainsUpgrader())
XCTAssertNoThrow(XCTAssertNil(try channel.readOutbound(as: ByteBuffer.self)))
do {
try channel.throwIfErrorCaught()
XCTFail("Did not throw")
} catch No.no {
// ok
} catch {
XCTFail("Unexpected error: \(error)")

XCTAssertThrowsError(try channel.throwIfErrorCaught()) { error in
XCTAssertEqual(.no, error as? No)
}

// Ok, now we can upgrade. Upgrader should be out of the pipeline, and we should have seen the 101 response.
Expand Down Expand Up @@ -985,13 +974,8 @@ class HTTPServerUpgradeTestCase: XCTestCase {
XCTAssertNoThrow(try channel.pipeline.assertDoesNotContainUpgrader())
XCTAssertNoThrow(XCTAssertNil(try channel.readOutbound(as: ByteBuffer.self)))

do {
try channel.throwIfErrorCaught()
XCTFail("Did not throw")
} catch No.no {
// ok
} catch {
XCTFail("Unexpected error: \(error)")
XCTAssertThrowsError(try channel.throwIfErrorCaught()) { error in
XCTAssertEqual(.no, error as? No)
}

switch try channel.readInbound(as: HTTPServerRequestPart.self) {
Expand Down Expand Up @@ -1065,13 +1049,8 @@ class HTTPServerUpgradeTestCase: XCTestCase {
XCTAssertNoThrow(try channel.pipeline.assertDoesNotContainUpgrader())
XCTAssertNoThrow(XCTAssertNil(try channel.readOutbound(as: ByteBuffer.self)))

do {
try channel.throwIfErrorCaught()
XCTFail("Did not throw")
} catch No.no {
// ok
} catch {
XCTFail("Unexpected error: \(error)")
XCTAssertThrowsError(try channel.throwIfErrorCaught()) { error in
XCTAssertEqual(.no, error as? No)
}

switch try channel.readInbound(as: HTTPServerRequestPart.self) {
Expand Down
Loading