Skip to content

Commit d2d7bde

Browse files
authored
add test where server sends connection: close (#143)
Motivation: We need a test that we can handle servers that repeatedly send connection: closes. Modification: Add a unit test that always makes the server send connection: close. Result: More test coverage.
1 parent 726d0c6 commit d2d7bde

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

Diff for: Tests/AsyncHTTPClientTests/HTTPClientTests+XCTest.swift

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ extension HTTPClientTests {
6767
("testWorksWhenServerClosesConnectionAfterReceivingRequest", testWorksWhenServerClosesConnectionAfterReceivingRequest),
6868
("testSubsequentRequestsWorkWithServerSendingConnectionClose", testSubsequentRequestsWorkWithServerSendingConnectionClose),
6969
("testSubsequentRequestsWorkWithServerAlternatingBetweenKeepAliveAndClose", testSubsequentRequestsWorkWithServerAlternatingBetweenKeepAliveAndClose),
70+
("testRepeatedRequestsWorkWhenServerAlwaysCloses", testRepeatedRequestsWorkWhenServerAlwaysCloses),
7071
]
7172
}
7273
}

Diff for: Tests/AsyncHTTPClientTests/HTTPClientTests.swift

+36
Original file line numberDiff line numberDiff line change
@@ -900,4 +900,40 @@ class HTTPClientTests: XCTestCase {
900900
XCTAssertNil(response?.body)
901901
}
902902
}
903+
904+
func testRepeatedRequestsWorkWhenServerAlwaysCloses() {
905+
let web = NIOHTTP1TestServer(group: self.group)
906+
defer {
907+
XCTAssertNoThrow(try web.stop())
908+
}
909+
910+
let httpClient = HTTPClient(eventLoopGroupProvider: .shared(self.group))
911+
defer {
912+
XCTAssertNoThrow(try httpClient.syncShutdown())
913+
}
914+
915+
for _ in 0..<10 {
916+
let result = httpClient.get(url: "http://localhost:\(web.serverPort)/foo")
917+
XCTAssertNoThrow(XCTAssertEqual(.head(.init(version: .init(major: 1, minor: 1),
918+
method: .GET,
919+
uri: "/foo",
920+
headers: HTTPHeaders([("Host", "localhost"),
921+
// The following line can be removed once
922+
// we have a connection pool.
923+
("Connection", "close"),
924+
("Content-Length", "0")]))),
925+
try web.readInbound()))
926+
XCTAssertNoThrow(XCTAssertEqual(.end(nil),
927+
try web.readInbound()))
928+
XCTAssertNoThrow(try web.writeOutbound(.head(.init(version: .init(major: 1, minor: 1),
929+
status: .ok,
930+
headers: HTTPHeaders([("CoNnEcTiOn", "cLoSe")])))))
931+
XCTAssertNoThrow(try web.writeOutbound(.end(nil)))
932+
933+
var response: HTTPClient.Response?
934+
XCTAssertNoThrow(response = try result.wait())
935+
XCTAssertEqual(.ok, response?.status)
936+
XCTAssertNil(response?.body)
937+
}
938+
}
903939
}

0 commit comments

Comments
 (0)