mirror of
https://github.com/apple/swift-http-types
synced 2026-11-26 04:57:48 +00:00
Request.url should return nil for situations where it is not applicable (#84)
Regular CONNECT requests and OPTIONS * requests do not have URLs
This commit is contained in:
1 parent
74191f0834
commit
40ca463b5f
2 files changed
+30
No files matched your search
@@ -24,6 +24,11 @@ extension HTTPRequest {
|
||||
/// fields.
|
||||
public var url: URL? {
|
||||
get {
|
||||
if (self.method == .connect && self.extendedConnectProtocol == nil)
|
||||
|| (self.method == .options && self.path == "*")
|
||||
{
|
||||
return nil
|
||||
}
|
||||
if let schemeField = self.pseudoHeaderFields.scheme,
|
||||
let authorityField = self.pseudoHeaderFields.authority,
|
||||
let pathField = self.pseudoHeaderFields.path
|
||||
|
||||
@@ -83,6 +83,31 @@ final class HTTPTypesFoundationTests: XCTestCase {
|
||||
XCTAssertEqual(request4.url?.absoluteString, "https://127.0.0.1:443/")
|
||||
}
|
||||
|
||||
func testNilRequestURL() {
|
||||
let request1 = HTTPRequest(
|
||||
method: .connect,
|
||||
scheme: "https",
|
||||
authority: "www.example.com:443",
|
||||
path: "www.example.com:443"
|
||||
)
|
||||
XCTAssertNil(request1.url)
|
||||
|
||||
var request2 = HTTPRequest(
|
||||
method: .connect,
|
||||
scheme: "https",
|
||||
authority: "www.example.com",
|
||||
path: "/"
|
||||
)
|
||||
request2.extendedConnectProtocol = "websocket"
|
||||
XCTAssertEqual(request2.url?.absoluteString, "https://www.example.com/")
|
||||
|
||||
let request3 = HTTPRequest(method: .options, scheme: "https", authority: "www.example.com", path: "*")
|
||||
XCTAssertNil(request3.url)
|
||||
|
||||
let request4 = HTTPRequest(method: .options, scheme: "https", authority: "www.example.com", path: "/")
|
||||
XCTAssertEqual(request4.url?.absoluteString, "https://www.example.com/")
|
||||
}
|
||||
|
||||
func testRequestToFoundation() throws {
|
||||
let request = HTTPRequest(
|
||||
method: .get,
|
||||
|
||||
Reference in new issue
Block a user