From c8a3450d87cec31df43a12d35ba5873ef13d3742 Mon Sep 17 00:00:00 2001 From: Guoye Zhang Date: Thu, 12 Dec 2024 13:40:06 -0800 Subject: [PATCH 1/3] Move HTTPRequest.url to the main library --- .../HTTPRequest+URL.swift | 7 +- .../HTTPTypesFoundationTests.swift | 64 +------------- Tests/HTTPTypesTests/HTTPTypesURLTests.swift | 86 +++++++++++++++++++ 3 files changed, 92 insertions(+), 65 deletions(-) rename Sources/{HTTPTypesFoundation => HTTPTypes}/HTTPRequest+URL.swift (98%) create mode 100644 Tests/HTTPTypesTests/HTTPTypesURLTests.swift diff --git a/Sources/HTTPTypesFoundation/HTTPRequest+URL.swift b/Sources/HTTPTypes/HTTPRequest+URL.swift similarity index 98% rename from Sources/HTTPTypesFoundation/HTTPRequest+URL.swift rename to Sources/HTTPTypes/HTTPRequest+URL.swift index dec2bc2..3f9abdf 100644 --- a/Sources/HTTPTypesFoundation/HTTPRequest+URL.swift +++ b/Sources/HTTPTypes/HTTPRequest+URL.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift open source project // -// Copyright (c) 2023 Apple Inc. and the Swift project authors +// Copyright (c) 2023-2024 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 // // See LICENSE.txt for license information @@ -12,8 +12,11 @@ // //===----------------------------------------------------------------------===// +#if canImport(FoundationEssentials) +import FoundationEssentials +#else import Foundation -import HTTPTypes +#endif #if canImport(CoreFoundation) import CoreFoundation diff --git a/Tests/HTTPTypesFoundationTests/HTTPTypesFoundationTests.swift b/Tests/HTTPTypesFoundationTests/HTTPTypesFoundationTests.swift index 627d58c..27128f0 100644 --- a/Tests/HTTPTypesFoundationTests/HTTPTypesFoundationTests.swift +++ b/Tests/HTTPTypesFoundationTests/HTTPTypesFoundationTests.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift open source project // -// Copyright (c) 2023 Apple Inc. and the Swift project authors +// Copyright (c) 2023-2024 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 // // See LICENSE.txt for license information @@ -21,68 +21,6 @@ import FoundationNetworking #endif final class HTTPTypesFoundationTests: XCTestCase { - func testRequestURLParsing() { - let request1 = HTTPRequest(url: URL(string: "h://a")!) - XCTAssertEqual(request1.scheme, "h") - XCTAssertEqual(request1.authority, "a") - XCTAssertEqual(request1.path, "/") - XCTAssertEqual(request1.url?.absoluteString, "h://a/") - - let request2 = HTTPRequest(url: URL(string: "h://a:4?")!) - XCTAssertEqual(request2.scheme, "h") - XCTAssertEqual(request2.authority, "a:4") - XCTAssertEqual(request2.path, "/?") - XCTAssertEqual(request2.url?.absoluteString, "h://a:4/?") - - let request3 = HTTPRequest(url: URL(string: "h://a/")!) - XCTAssertEqual(request3.scheme, "h") - XCTAssertEqual(request3.authority, "a") - XCTAssertEqual(request3.path, "/") - XCTAssertEqual(request3.url?.absoluteString, "h://a/") - - let request4 = HTTPRequest(url: URL(string: "h://a/p?q#1")!) - XCTAssertEqual(request4.scheme, "h") - XCTAssertEqual(request4.authority, "a") - XCTAssertEqual(request4.path, "/p?q") - XCTAssertEqual(request4.url?.absoluteString, "h://a/p?q") - - let request5 = HTTPRequest(url: URL(string: "data:,Hello%2C%20World%21")!) - XCTAssertEqual(request5.scheme, "data") - XCTAssertNil(request5.authority) - #if canImport(CoreFoundation) - XCTAssertEqual(request5.path, "/") - #else // canImport(CoreFoundation) - XCTAssertEqual(request5.path, ",Hello%2C%20World%21") - #endif // canImport(CoreFoundation) - XCTAssertNil(request5.url) - } - - func testRequestURLAuthorityParsing() { - let request1 = HTTPRequest(url: URL(string: "https://[::1]")!) - XCTAssertEqual(request1.scheme, "https") - XCTAssertEqual(request1.authority, "[::1]") - XCTAssertEqual(request1.path, "/") - XCTAssertEqual(request1.url?.absoluteString, "https://[::1]/") - - let request2 = HTTPRequest(url: URL(string: "https://[::1]:443")!) - XCTAssertEqual(request2.scheme, "https") - XCTAssertEqual(request2.authority, "[::1]:443") - XCTAssertEqual(request2.path, "/") - XCTAssertEqual(request2.url?.absoluteString, "https://[::1]:443/") - - let request3 = HTTPRequest(url: URL(string: "https://127.0.0.1")!) - XCTAssertEqual(request3.scheme, "https") - XCTAssertEqual(request3.authority, "127.0.0.1") - XCTAssertEqual(request3.path, "/") - XCTAssertEqual(request3.url?.absoluteString, "https://127.0.0.1/") - - let request4 = HTTPRequest(url: URL(string: "https://127.0.0.1:443")!) - XCTAssertEqual(request4.scheme, "https") - XCTAssertEqual(request4.authority, "127.0.0.1:443") - XCTAssertEqual(request4.path, "/") - XCTAssertEqual(request4.url?.absoluteString, "https://127.0.0.1:443/") - } - func testRequestToFoundation() throws { let request = HTTPRequest( method: .get, diff --git a/Tests/HTTPTypesTests/HTTPTypesURLTests.swift b/Tests/HTTPTypesTests/HTTPTypesURLTests.swift new file mode 100644 index 0000000..bd0219b --- /dev/null +++ b/Tests/HTTPTypesTests/HTTPTypesURLTests.swift @@ -0,0 +1,86 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift open source project +// +// Copyright (c) 2024 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of Swift project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +import HTTPTypes +import XCTest + +#if canImport(FoundationEssentials) +import FoundationEssentials +#else +import Foundation +#endif + +final class HTTPTypesURLTests: XCTestCase { + func testRequestURLParsing() { + let request1 = HTTPRequest(url: URL(string: "h://a")!) + XCTAssertEqual(request1.scheme, "h") + XCTAssertEqual(request1.authority, "a") + XCTAssertEqual(request1.path, "/") + XCTAssertEqual(request1.url?.absoluteString, "h://a/") + + let request2 = HTTPRequest(url: URL(string: "h://a:4?")!) + XCTAssertEqual(request2.scheme, "h") + XCTAssertEqual(request2.authority, "a:4") + XCTAssertEqual(request2.path, "/?") + XCTAssertEqual(request2.url?.absoluteString, "h://a:4/?") + + let request3 = HTTPRequest(url: URL(string: "h://a/")!) + XCTAssertEqual(request3.scheme, "h") + XCTAssertEqual(request3.authority, "a") + XCTAssertEqual(request3.path, "/") + XCTAssertEqual(request3.url?.absoluteString, "h://a/") + + let request4 = HTTPRequest(url: URL(string: "h://a/p?q#1")!) + XCTAssertEqual(request4.scheme, "h") + XCTAssertEqual(request4.authority, "a") + XCTAssertEqual(request4.path, "/p?q") + XCTAssertEqual(request4.url?.absoluteString, "h://a/p?q") + + let request5 = HTTPRequest(url: URL(string: "data:,Hello%2C%20World%21")!) + XCTAssertEqual(request5.scheme, "data") + XCTAssertNil(request5.authority) + #if canImport(CoreFoundation) + XCTAssertEqual(request5.path, "/") + #else // canImport(CoreFoundation) + XCTAssertEqual(request5.path, ",Hello%2C%20World%21") + #endif // canImport(CoreFoundation) + XCTAssertNil(request5.url) + } + + func testRequestURLAuthorityParsing() { + let request1 = HTTPRequest(url: URL(string: "https://[::1]")!) + XCTAssertEqual(request1.scheme, "https") + XCTAssertEqual(request1.authority, "[::1]") + XCTAssertEqual(request1.path, "/") + XCTAssertEqual(request1.url?.absoluteString, "https://[::1]/") + + let request2 = HTTPRequest(url: URL(string: "https://[::1]:443")!) + XCTAssertEqual(request2.scheme, "https") + XCTAssertEqual(request2.authority, "[::1]:443") + XCTAssertEqual(request2.path, "/") + XCTAssertEqual(request2.url?.absoluteString, "https://[::1]:443/") + + let request3 = HTTPRequest(url: URL(string: "https://127.0.0.1")!) + XCTAssertEqual(request3.scheme, "https") + XCTAssertEqual(request3.authority, "127.0.0.1") + XCTAssertEqual(request3.path, "/") + XCTAssertEqual(request3.url?.absoluteString, "https://127.0.0.1/") + + let request4 = HTTPRequest(url: URL(string: "https://127.0.0.1:443")!) + XCTAssertEqual(request4.scheme, "https") + XCTAssertEqual(request4.authority, "127.0.0.1:443") + XCTAssertEqual(request4.path, "/") + XCTAssertEqual(request4.url?.absoluteString, "https://127.0.0.1:443/") + } +} From 67ca7f3c3ab5acbaf3de303618ad8bd451669e6f Mon Sep 17 00:00:00 2001 From: Guoye Zhang Date: Thu, 12 Dec 2024 13:45:12 -0800 Subject: [PATCH 2/3] Fix build --- Sources/HTTPTypes/HTTPRequest+URL.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/HTTPTypes/HTTPRequest+URL.swift b/Sources/HTTPTypes/HTTPRequest+URL.swift index 3f9abdf..50fae0b 100644 --- a/Sources/HTTPTypes/HTTPRequest+URL.swift +++ b/Sources/HTTPTypes/HTTPRequest+URL.swift @@ -19,6 +19,7 @@ import Foundation #endif #if canImport(CoreFoundation) +import Foundation import CoreFoundation #endif // canImport(CoreFoundation) From 561103acc2732b191b840f117e5ded50b72d7835 Mon Sep 17 00:00:00 2001 From: Guoye Zhang Date: Mon, 16 Dec 2024 12:51:24 -0800 Subject: [PATCH 3/3] Reexport HTTPTypes from HTTPTypesFoundation --- .../HTTPTypesFoundation/HTTPTypesFoundation.swift | 15 +++++++++++++++ .../HTTPTypesFoundationTests.swift | 1 - 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 Sources/HTTPTypesFoundation/HTTPTypesFoundation.swift diff --git a/Sources/HTTPTypesFoundation/HTTPTypesFoundation.swift b/Sources/HTTPTypesFoundation/HTTPTypesFoundation.swift new file mode 100644 index 0000000..e440fa7 --- /dev/null +++ b/Sources/HTTPTypesFoundation/HTTPTypesFoundation.swift @@ -0,0 +1,15 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift open source project +// +// Copyright (c) 2024 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of Swift project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +@_exported import HTTPTypes diff --git a/Tests/HTTPTypesFoundationTests/HTTPTypesFoundationTests.swift b/Tests/HTTPTypesFoundationTests/HTTPTypesFoundationTests.swift index 27128f0..c64ae23 100644 --- a/Tests/HTTPTypesFoundationTests/HTTPTypesFoundationTests.swift +++ b/Tests/HTTPTypesFoundationTests/HTTPTypesFoundationTests.swift @@ -12,7 +12,6 @@ // //===----------------------------------------------------------------------===// -import HTTPTypes import HTTPTypesFoundation import XCTest