Skip to content

Commit c39555d

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 86db162 commit c39555d

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
@@ -99,8 +99,8 @@ extension HTTPClient {
9999
/// Represent HTTP request.
100100
public struct Request {
101101
/// Represent kind of Request
102-
enum Kind {
103-
enum UnixScheme {
102+
enum Kind: Equatable {
103+
enum UnixScheme: Equatable {
104104
case baseURL
105105
case http_unix
106106
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
@@ -891,4 +891,31 @@ class HTTPClientInternalTests: XCTestCase {
891891
XCTAssert(el1 === response.eventLoop)
892892
XCTAssertNoThrow(try response.wait())
893893
}
894+
895+
func testInternalRequestURI() throws {
896+
let request1 = try Request(url: "https://someserver.com:8888/some/path?foo=bar")
897+
XCTAssertEqual(request1.kind, .host)
898+
XCTAssertEqual(request1.socketPath, "")
899+
XCTAssertEqual(request1.uri, "/some/path?foo=bar")
900+
901+
let request2 = try Request(url: "https://someserver.com")
902+
XCTAssertEqual(request2.kind, .host)
903+
XCTAssertEqual(request2.socketPath, "")
904+
XCTAssertEqual(request2.uri, "/")
905+
906+
let request3 = try Request(url: "unix:///tmp/file")
907+
XCTAssertEqual(request3.kind, .unixSocket(.baseURL))
908+
XCTAssertEqual(request3.socketPath, "/tmp/file")
909+
XCTAssertEqual(request3.uri, "/")
910+
911+
let request4 = try Request(url: "http+unix://%2Ftmp%2Ffile/file/path")
912+
XCTAssertEqual(request4.kind, .unixSocket(.http_unix))
913+
XCTAssertEqual(request4.socketPath, "/tmp/file")
914+
XCTAssertEqual(request4.uri, "/file/path")
915+
916+
let request5 = try Request(url: "https+unix://%2Ftmp%2Ffile/file/path")
917+
XCTAssertEqual(request5.kind, .unixSocket(.https_unix))
918+
XCTAssertEqual(request5.socketPath, "/tmp/file")
919+
XCTAssertEqual(request5.uri, "/file/path")
920+
}
894921
}

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)