Skip to content

Commit 81111cd

Browse files
Added additional tests for socketPath-based requests
Motivation: While going through the existing tests, I identified a few more instances where we could add some testing. Modifications: Added one test that verifies Requests are being decoded correctly, and improved three others to check for path parsing, error throwing, and schema casing respectively. Result: Tests that continue to pass, but that will also catch any incompatible changes in the future.
1 parent 8e60b94 commit 81111cd

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

Sources/AsyncHTTPClient/HTTPHandler.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ extension HTTPClient {
9595
/// Represent HTTP request.
9696
public struct Request {
9797
/// Represent kind of Request
98-
enum Kind {
99-
enum UnixScheme {
98+
enum Kind: Equatable {
99+
enum UnixScheme: Equatable {
100100
case baseURL
101101
case http_unix
102102
case https_unix

Tests/AsyncHTTPClientTests/HTTPClientInternalTests+XCTest.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ extension HTTPClientInternalTests {
4343
("testUploadStreamingIsCalledOnTaskEL", testUploadStreamingIsCalledOnTaskEL),
4444
("testWeCanActuallyExactlySetTheEventLoops", testWeCanActuallyExactlySetTheEventLoops),
4545
("testTaskPromiseBoundToEL", testTaskPromiseBoundToEL),
46+
("testInternalRequestURI", testInternalRequestURI),
4647
]
4748
}
4849
}

Tests/AsyncHTTPClientTests/HTTPClientInternalTests.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,4 +959,31 @@ class HTTPClientInternalTests: XCTestCase {
959959
XCTAssertTrue(task.futureResult.eventLoop === el2)
960960
XCTAssertNoThrow(try task.wait())
961961
}
962+
963+
func testInternalRequestURI() throws {
964+
let request1 = try Request(url: "https://someserver.com:8888/some/path?foo=bar")
965+
XCTAssertEqual(request1.kind, .host)
966+
XCTAssertEqual(request1.socketPath, "")
967+
XCTAssertEqual(request1.uri, "/some/path?foo=bar")
968+
969+
let request2 = try Request(url: "https://someserver.com")
970+
XCTAssertEqual(request2.kind, .host)
971+
XCTAssertEqual(request2.socketPath, "")
972+
XCTAssertEqual(request2.uri, "/")
973+
974+
let request3 = try Request(url: "unix:///tmp/file")
975+
XCTAssertEqual(request3.kind, .unixSocket(.baseURL))
976+
XCTAssertEqual(request3.socketPath, "/tmp/file")
977+
XCTAssertEqual(request3.uri, "/")
978+
979+
let request4 = try Request(url: "http+unix://%2Ftmp%2Ffile/file/path")
980+
XCTAssertEqual(request4.kind, .unixSocket(.http_unix))
981+
XCTAssertEqual(request4.socketPath, "/tmp/file")
982+
XCTAssertEqual(request4.uri, "/file/path")
983+
984+
let request5 = try Request(url: "https+unix://%2Ftmp%2Ffile/file/path")
985+
XCTAssertEqual(request5.kind, .unixSocket(.https_unix))
986+
XCTAssertEqual(request5.socketPath, "/tmp/file")
987+
XCTAssertEqual(request5.uri, "/file/path")
988+
}
962989
}

Tests/AsyncHTTPClientTests/HTTPClientTests.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,18 @@ class HTTPClientTests: XCTestCase {
9898
XCTAssertEqual(request3.url.path, "/tmp/file")
9999
XCTAssertEqual(request3.port, 80)
100100
XCTAssertFalse(request3.useTLS)
101+
102+
let request4 = try Request(url: "http+unix://%2Ftmp%2Ffile/file/path")
103+
XCTAssertEqual(request4.host, "")
104+
XCTAssertEqual(request4.url.host, "/tmp/file")
105+
XCTAssertEqual(request4.url.path, "/file/path")
106+
XCTAssertFalse(request4.useTLS)
107+
108+
let request5 = try Request(url: "https+unix://%2Ftmp%2Ffile/file/path")
109+
XCTAssertEqual(request5.host, "")
110+
XCTAssertEqual(request5.url.host, "/tmp/file")
111+
XCTAssertEqual(request5.url.path, "/file/path")
112+
XCTAssertTrue(request5.useTLS)
101113
}
102114

103115
func testBadRequestURI() throws {
@@ -110,11 +122,16 @@ class HTTPClientTests: XCTestCase {
110122
XCTAssertThrowsError(try Request(url: "https:/foo"), "should throw") { error in
111123
XCTAssertEqual(error as! HTTPClientError, HTTPClientError.emptyHost)
112124
}
125+
XCTAssertThrowsError(try Request(url: "http+unix:///path"), "should throw") { error in
126+
XCTAssertEqual(error as! HTTPClientError, HTTPClientError.missingSocketPath)
127+
}
113128
}
114129

115130
func testSchemaCasing() throws {
116131
XCTAssertNoThrow(try Request(url: "hTTpS://someserver.com:8888/some/path?foo=bar"))
117132
XCTAssertNoThrow(try Request(url: "uNIx:///some/path"))
133+
XCTAssertNoThrow(try Request(url: "hTtP+uNIx://%2Fsome%2Fpath/"))
134+
XCTAssertNoThrow(try Request(url: "hTtPS+uNIx://%2Fsome%2Fpath/"))
118135
}
119136

120137
func testGet() throws {

0 commit comments

Comments
 (0)