From fca6be00f7ff26f29b710aba16e8e041b54d381d Mon Sep 17 00:00:00 2001 From: Guoye Zhang Date: Fri, 6 Dec 2024 17:17:04 -0800 Subject: [PATCH 1/3] Split parts with FoundationNetworking dependency into its own library --- Package.swift | 16 ++++ .../HTTPTypes+ISOLatin1.swift | 0 .../URLRequest+HTTPTypes.swift | 3 +- .../URLResponse+HTTPTypes.swift | 0 .../URLSession+HTTPTypes.swift | 0 .../HTTPTypesFoundationTests.swift | 84 +++++++++++++++++++ .../HTTPTypesFoundationTests.swift | 71 +--------------- 7 files changed, 103 insertions(+), 71 deletions(-) rename Sources/{HTTPTypesFoundation => HTTPTypesFoundationNetworking}/HTTPTypes+ISOLatin1.swift (100%) rename Sources/{HTTPTypesFoundation => HTTPTypesFoundationNetworking}/URLRequest+HTTPTypes.swift (96%) rename Sources/{HTTPTypesFoundation => HTTPTypesFoundationNetworking}/URLResponse+HTTPTypes.swift (100%) rename Sources/{HTTPTypesFoundation => HTTPTypesFoundationNetworking}/URLSession+HTTPTypes.swift (100%) create mode 100644 Tests/HTTPTypesFoundationNetworkingTests/HTTPTypesFoundationTests.swift diff --git a/Package.swift b/Package.swift index 575ce46..59a1723 100644 --- a/Package.swift +++ b/Package.swift @@ -7,6 +7,7 @@ let package = Package( products: [ .library(name: "HTTPTypes", targets: ["HTTPTypes"]), .library(name: "HTTPTypesFoundation", targets: ["HTTPTypesFoundation"]), + .library(name: "HTTPTypesFoundationNetworking", targets: ["HTTPTypesFoundationNetworking"]), ], targets: [ .target(name: "HTTPTypes"), @@ -16,6 +17,13 @@ let package = Package( "HTTPTypes" ] ), + .target( + name: "HTTPTypesFoundationNetworking", + dependencies: [ + "HTTPTypes", + "HTTPTypesFoundation" + ] + ), .testTarget( name: "HTTPTypesTests", dependencies: [ @@ -25,8 +33,16 @@ let package = Package( .testTarget( name: "HTTPTypesFoundationTests", dependencies: [ + "HTTPTypes", "HTTPTypesFoundation" ] ), + .testTarget( + name: "HTTPTypesFoundationNetworkingTests", + dependencies: [ + "HTTPTypes", + "HTTPTypesFoundationNetworking" + ] + ), ] ) diff --git a/Sources/HTTPTypesFoundation/HTTPTypes+ISOLatin1.swift b/Sources/HTTPTypesFoundationNetworking/HTTPTypes+ISOLatin1.swift similarity index 100% rename from Sources/HTTPTypesFoundation/HTTPTypes+ISOLatin1.swift rename to Sources/HTTPTypesFoundationNetworking/HTTPTypes+ISOLatin1.swift diff --git a/Sources/HTTPTypesFoundation/URLRequest+HTTPTypes.swift b/Sources/HTTPTypesFoundationNetworking/URLRequest+HTTPTypes.swift similarity index 96% rename from Sources/HTTPTypesFoundation/URLRequest+HTTPTypes.swift rename to Sources/HTTPTypesFoundationNetworking/URLRequest+HTTPTypes.swift index 624f8a3..96180ed 100644 --- a/Sources/HTTPTypesFoundation/URLRequest+HTTPTypes.swift +++ b/Sources/HTTPTypesFoundationNetworking/URLRequest+HTTPTypes.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 @@ -14,6 +14,7 @@ import Foundation import HTTPTypes +import HTTPTypesFoundation #if canImport(FoundationNetworking) import FoundationNetworking diff --git a/Sources/HTTPTypesFoundation/URLResponse+HTTPTypes.swift b/Sources/HTTPTypesFoundationNetworking/URLResponse+HTTPTypes.swift similarity index 100% rename from Sources/HTTPTypesFoundation/URLResponse+HTTPTypes.swift rename to Sources/HTTPTypesFoundationNetworking/URLResponse+HTTPTypes.swift diff --git a/Sources/HTTPTypesFoundation/URLSession+HTTPTypes.swift b/Sources/HTTPTypesFoundationNetworking/URLSession+HTTPTypes.swift similarity index 100% rename from Sources/HTTPTypesFoundation/URLSession+HTTPTypes.swift rename to Sources/HTTPTypesFoundationNetworking/URLSession+HTTPTypes.swift diff --git a/Tests/HTTPTypesFoundationNetworkingTests/HTTPTypesFoundationTests.swift b/Tests/HTTPTypesFoundationNetworkingTests/HTTPTypesFoundationTests.swift new file mode 100644 index 0000000..8c89683 --- /dev/null +++ b/Tests/HTTPTypesFoundationNetworkingTests/HTTPTypesFoundationTests.swift @@ -0,0 +1,84 @@ +//===----------------------------------------------------------------------===// +// +// 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 HTTPTypesFoundationNetworking +import XCTest + +final class HTTPTypesFoundationNetworkingTests: XCTestCase { + func testRequestToFoundation() throws { + let request = HTTPRequest( + method: .get, + scheme: "https", + authority: "www.example.com", + path: "/", + headerFields: [ + .accept: "*/*", + .acceptEncoding: "gzip", + .acceptEncoding: "br", + .cookie: "a=b", + .cookie: "c=d", + ] + ) + + let urlRequest = try XCTUnwrap(URLRequest(httpRequest: request)) + XCTAssertEqual(urlRequest.url, URL(string: "https://www.example.com/")!) + XCTAssertEqual(urlRequest.value(forHTTPHeaderField: "aCcEpT"), "*/*") + XCTAssertEqual(urlRequest.value(forHTTPHeaderField: "Accept-Encoding"), "gzip, br") + XCTAssertEqual(urlRequest.value(forHTTPHeaderField: "cookie"), "a=b; c=d") + } + + func testRequestFromFoundation() throws { + var urlRequest = URLRequest(url: URL(string: "https://www.example.com/")!) + urlRequest.httpMethod = "POST" + urlRequest.setValue("Bar", forHTTPHeaderField: "X-Foo") + + let request = try XCTUnwrap(urlRequest.httpRequest) + XCTAssertEqual(request.method, .post) + XCTAssertEqual(request.scheme, "https") + XCTAssertEqual(request.authority, "www.example.com") + XCTAssertEqual(request.path, "/") + XCTAssertEqual(request.headerFields[.init("x-foo")!], "Bar") + } + + func testResponseToFoundation() throws { + let response = HTTPResponse( + status: .ok, + headerFields: [ + .server: "HTTPServer/1.0" + ] + ) + + let urlResponse = try XCTUnwrap( + HTTPURLResponse(httpResponse: response, url: URL(string: "https://www.example.com/")!) + ) + XCTAssertEqual(urlResponse.statusCode, 200) + XCTAssertEqual(urlResponse.value(forHTTPHeaderField: "Server"), "HTTPServer/1.0") + } + + func testResponseFromFoundation() throws { + let urlResponse = HTTPURLResponse( + url: URL(string: "https://www.example.com/")!, + statusCode: 204, + httpVersion: nil, + headerFields: [ + "X-Emoji": "😀" + ] + )! + + let response = try XCTUnwrap(urlResponse.httpResponse) + XCTAssertEqual(response.status, .noContent) + XCTAssertEqual(response.headerFields[.init("X-EMOJI")!], "😀") + } +} diff --git a/Tests/HTTPTypesFoundationTests/HTTPTypesFoundationTests.swift b/Tests/HTTPTypesFoundationTests/HTTPTypesFoundationTests.swift index 627d58c..2247ef6 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 @@ -16,10 +16,6 @@ import HTTPTypes import HTTPTypesFoundation import XCTest -#if canImport(FoundationNetworking) -import FoundationNetworking -#endif - final class HTTPTypesFoundationTests: XCTestCase { func testRequestURLParsing() { let request1 = HTTPRequest(url: URL(string: "h://a")!) @@ -82,69 +78,4 @@ final class HTTPTypesFoundationTests: XCTestCase { XCTAssertEqual(request4.path, "/") XCTAssertEqual(request4.url?.absoluteString, "https://127.0.0.1:443/") } - - func testRequestToFoundation() throws { - let request = HTTPRequest( - method: .get, - scheme: "https", - authority: "www.example.com", - path: "/", - headerFields: [ - .accept: "*/*", - .acceptEncoding: "gzip", - .acceptEncoding: "br", - .cookie: "a=b", - .cookie: "c=d", - ] - ) - - let urlRequest = try XCTUnwrap(URLRequest(httpRequest: request)) - XCTAssertEqual(urlRequest.url, URL(string: "https://www.example.com/")!) - XCTAssertEqual(urlRequest.value(forHTTPHeaderField: "aCcEpT"), "*/*") - XCTAssertEqual(urlRequest.value(forHTTPHeaderField: "Accept-Encoding"), "gzip, br") - XCTAssertEqual(urlRequest.value(forHTTPHeaderField: "cookie"), "a=b; c=d") - } - - func testRequestFromFoundation() throws { - var urlRequest = URLRequest(url: URL(string: "https://www.example.com/")!) - urlRequest.httpMethod = "POST" - urlRequest.setValue("Bar", forHTTPHeaderField: "X-Foo") - - let request = try XCTUnwrap(urlRequest.httpRequest) - XCTAssertEqual(request.method, .post) - XCTAssertEqual(request.scheme, "https") - XCTAssertEqual(request.authority, "www.example.com") - XCTAssertEqual(request.path, "/") - XCTAssertEqual(request.headerFields[.init("x-foo")!], "Bar") - } - - func testResponseToFoundation() throws { - let response = HTTPResponse( - status: .ok, - headerFields: [ - .server: "HTTPServer/1.0" - ] - ) - - let urlResponse = try XCTUnwrap( - HTTPURLResponse(httpResponse: response, url: URL(string: "https://www.example.com/")!) - ) - XCTAssertEqual(urlResponse.statusCode, 200) - XCTAssertEqual(urlResponse.value(forHTTPHeaderField: "Server"), "HTTPServer/1.0") - } - - func testResponseFromFoundation() throws { - let urlResponse = HTTPURLResponse( - url: URL(string: "https://www.example.com/")!, - statusCode: 204, - httpVersion: nil, - headerFields: [ - "X-Emoji": "😀" - ] - )! - - let response = try XCTUnwrap(urlResponse.httpResponse) - XCTAssertEqual(response.status, .noContent) - XCTAssertEqual(response.headerFields[.init("X-EMOJI")!], "😀") - } } From 1cf2fe688c44f5ee1f9a625f35f25a3720c19fb7 Mon Sep 17 00:00:00 2001 From: Guoye Zhang Date: Fri, 6 Dec 2024 20:04:19 -0800 Subject: [PATCH 2/3] Fix Linux --- .../HTTPTypesFoundationTests.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Tests/HTTPTypesFoundationNetworkingTests/HTTPTypesFoundationTests.swift b/Tests/HTTPTypesFoundationNetworkingTests/HTTPTypesFoundationTests.swift index 8c89683..3d52cef 100644 --- a/Tests/HTTPTypesFoundationNetworkingTests/HTTPTypesFoundationTests.swift +++ b/Tests/HTTPTypesFoundationNetworkingTests/HTTPTypesFoundationTests.swift @@ -16,6 +16,10 @@ import HTTPTypes import HTTPTypesFoundationNetworking import XCTest +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif + final class HTTPTypesFoundationNetworkingTests: XCTestCase { func testRequestToFoundation() throws { let request = HTTPRequest( From 978490073b4ef87dfc5211b3a9fe7cb52374df36 Mon Sep 17 00:00:00 2001 From: Guoye Zhang Date: Fri, 6 Dec 2024 20:04:54 -0800 Subject: [PATCH 3/3] Fix format --- Package.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Package.swift b/Package.swift index 59a1723..207945c 100644 --- a/Package.swift +++ b/Package.swift @@ -21,7 +21,7 @@ let package = Package( name: "HTTPTypesFoundationNetworking", dependencies: [ "HTTPTypes", - "HTTPTypesFoundation" + "HTTPTypesFoundation", ] ), .testTarget( @@ -34,14 +34,14 @@ let package = Package( name: "HTTPTypesFoundationTests", dependencies: [ "HTTPTypes", - "HTTPTypesFoundation" + "HTTPTypesFoundation", ] ), .testTarget( name: "HTTPTypesFoundationNetworkingTests", dependencies: [ "HTTPTypes", - "HTTPTypesFoundationNetworking" + "HTTPTypesFoundationNetworking", ] ), ]