Skip to content

Commit 7c8bae8

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 364d106 commit 7c8bae8

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
@@ -98,8 +98,8 @@ extension HTTPClient {
9898
/// Represent HTTP request.
9999
public struct Request {
100100
/// Represent kind of Request
101-
enum Kind {
102-
enum UnixScheme {
101+
enum Kind: Equatable {
102+
enum UnixScheme: Equatable {
103103
case baseURL
104104
case http_unix
105105
case https_unix

Tests/AsyncHTTPClientTests/HTTPClientInternalTests+XCTest.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ extension HTTPClientInternalTests {
4141
("testUncleanCloseThrows", testUncleanCloseThrows),
4242
("testUploadStreamingIsCalledOnTaskEL", testUploadStreamingIsCalledOnTaskEL),
4343
("testWeCanActuallyExactlySetTheEventLoops", testWeCanActuallyExactlySetTheEventLoops),
44+
("testInternalRequestURI", testInternalRequestURI),
4445
]
4546
}
4647
}

Tests/AsyncHTTPClientTests/HTTPClientInternalTests.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,4 +884,31 @@ class HTTPClientInternalTests: XCTestCase {
884884
XCTAssert(el1 === response.eventLoop)
885885
XCTAssertNoThrow(try response.wait())
886886
}
887+
888+
func testInternalRequestURI() throws {
889+
let request1 = try Request(url: "https://someserver.com:8888/some/path?foo=bar")
890+
XCTAssertEqual(request1.kind, .host)
891+
XCTAssertEqual(request1.socketPath, "")
892+
XCTAssertEqual(request1.uri, "/some/path?foo=bar")
893+
894+
let request2 = try Request(url: "https://someserver.com")
895+
XCTAssertEqual(request2.kind, .host)
896+
XCTAssertEqual(request2.socketPath, "")
897+
XCTAssertEqual(request2.uri, "/")
898+
899+
let request3 = try Request(url: "unix:///tmp/file")
900+
XCTAssertEqual(request3.kind, .unixSocket(.baseURL))
901+
XCTAssertEqual(request3.socketPath, "/tmp/file")
902+
XCTAssertEqual(request3.uri, "/")
903+
904+
let request4 = try Request(url: "http+unix://%2Ftmp%2Ffile/file/path")
905+
XCTAssertEqual(request4.kind, .unixSocket(.http_unix))
906+
XCTAssertEqual(request4.socketPath, "/tmp/file")
907+
XCTAssertEqual(request4.uri, "/file/path")
908+
909+
let request5 = try Request(url: "https+unix://%2Ftmp%2Ffile/file/path")
910+
XCTAssertEqual(request5.kind, .unixSocket(.https_unix))
911+
XCTAssertEqual(request5.socketPath, "/tmp/file")
912+
XCTAssertEqual(request5.uri, "/file/path")
913+
}
887914
}

Tests/AsyncHTTPClientTests/HTTPClientTests.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ class HTTPClientTests: XCTestCase {
8484
XCTAssertEqual(request3.url.path, "/tmp/file")
8585
XCTAssertEqual(request3.port, 80)
8686
XCTAssertFalse(request3.useTLS)
87+
88+
let request4 = try Request(url: "http+unix://%2Ftmp%2Ffile/file/path")
89+
XCTAssertEqual(request4.host, "")
90+
XCTAssertEqual(request4.url.host, "/tmp/file")
91+
XCTAssertEqual(request4.url.path, "/file/path")
92+
XCTAssertFalse(request4.useTLS)
93+
94+
let request5 = try Request(url: "https+unix://%2Ftmp%2Ffile/file/path")
95+
XCTAssertEqual(request5.host, "")
96+
XCTAssertEqual(request5.url.host, "/tmp/file")
97+
XCTAssertEqual(request5.url.path, "/file/path")
98+
XCTAssertTrue(request5.useTLS)
8799
}
88100

89101
func testBadRequestURI() throws {
@@ -96,11 +108,16 @@ class HTTPClientTests: XCTestCase {
96108
XCTAssertThrowsError(try Request(url: "https:/foo"), "should throw") { error in
97109
XCTAssertEqual(error as! HTTPClientError, HTTPClientError.emptyHost)
98110
}
111+
XCTAssertThrowsError(try Request(url: "http+unix:///path"), "should throw") { error in
112+
XCTAssertEqual(error as! HTTPClientError, HTTPClientError.missingSocketPath)
113+
}
99114
}
100115

101116
func testSchemaCasing() throws {
102117
XCTAssertNoThrow(try Request(url: "hTTpS://someserver.com:8888/some/path?foo=bar"))
103118
XCTAssertNoThrow(try Request(url: "uNIx:///some/path"))
119+
XCTAssertNoThrow(try Request(url: "hTtP+uNIx://%2Fsome%2Fpath/"))
120+
XCTAssertNoThrow(try Request(url: "hTtPS+uNIx://%2Fsome%2Fpath/"))
104121
}
105122

106123
func testGet() throws {

0 commit comments

Comments
 (0)