Skip to content

Commit 6900f67

Browse files
committed
Split parts with FoundationNetworking dependency into its own library
1 parent ef18d82 commit 6900f67

File tree

7 files changed

+103
-71
lines changed

7 files changed

+103
-71
lines changed

Package.swift

+16
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ let package = Package(
77
products: [
88
.library(name: "HTTPTypes", targets: ["HTTPTypes"]),
99
.library(name: "HTTPTypesFoundation", targets: ["HTTPTypesFoundation"]),
10+
.library(name: "HTTPTypesFoundationNetworking", targets: ["HTTPTypesFoundationNetworking"]),
1011
],
1112
targets: [
1213
.target(name: "HTTPTypes"),
@@ -16,6 +17,13 @@ let package = Package(
1617
"HTTPTypes"
1718
]
1819
),
20+
.target(
21+
name: "HTTPTypesFoundationNetworking",
22+
dependencies: [
23+
"HTTPTypes",
24+
"HTTPTypesFoundation"
25+
]
26+
),
1927
.testTarget(
2028
name: "HTTPTypesTests",
2129
dependencies: [
@@ -25,8 +33,16 @@ let package = Package(
2533
.testTarget(
2634
name: "HTTPTypesFoundationTests",
2735
dependencies: [
36+
"HTTPTypes",
2837
"HTTPTypesFoundation"
2938
]
3039
),
40+
.testTarget(
41+
name: "HTTPTypesFoundationNetworkingTests",
42+
dependencies: [
43+
"HTTPTypes",
44+
"HTTPTypesFoundationNetworking"
45+
]
46+
),
3147
]
3248
)

Sources/HTTPTypesFoundation/URLRequest+HTTPTypes.swift renamed to Sources/HTTPTypesFoundationNetworking/URLRequest+HTTPTypes.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift open source project
44
//
5-
// Copyright (c) 2023 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2023-2024 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0
77
//
88
// See LICENSE.txt for license information
@@ -14,6 +14,7 @@
1414

1515
import Foundation
1616
import HTTPTypes
17+
import HTTPTypesFoundation
1718

1819
#if canImport(FoundationNetworking)
1920
import FoundationNetworking
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import HTTPTypes
16+
import HTTPTypesFoundationNetworking
17+
import XCTest
18+
19+
final class HTTPTypesFoundationNetworkingTests: XCTestCase {
20+
func testRequestToFoundation() throws {
21+
let request = HTTPRequest(
22+
method: .get,
23+
scheme: "https",
24+
authority: "www.example.com",
25+
path: "/",
26+
headerFields: [
27+
.accept: "*/*",
28+
.acceptEncoding: "gzip",
29+
.acceptEncoding: "br",
30+
.cookie: "a=b",
31+
.cookie: "c=d",
32+
]
33+
)
34+
35+
let urlRequest = try XCTUnwrap(URLRequest(httpRequest: request))
36+
XCTAssertEqual(urlRequest.url, URL(string: "https://www.example.com/")!)
37+
XCTAssertEqual(urlRequest.value(forHTTPHeaderField: "aCcEpT"), "*/*")
38+
XCTAssertEqual(urlRequest.value(forHTTPHeaderField: "Accept-Encoding"), "gzip, br")
39+
XCTAssertEqual(urlRequest.value(forHTTPHeaderField: "cookie"), "a=b; c=d")
40+
}
41+
42+
func testRequestFromFoundation() throws {
43+
var urlRequest = URLRequest(url: URL(string: "https://www.example.com/")!)
44+
urlRequest.httpMethod = "POST"
45+
urlRequest.setValue("Bar", forHTTPHeaderField: "X-Foo")
46+
47+
let request = try XCTUnwrap(urlRequest.httpRequest)
48+
XCTAssertEqual(request.method, .post)
49+
XCTAssertEqual(request.scheme, "https")
50+
XCTAssertEqual(request.authority, "www.example.com")
51+
XCTAssertEqual(request.path, "/")
52+
XCTAssertEqual(request.headerFields[.init("x-foo")!], "Bar")
53+
}
54+
55+
func testResponseToFoundation() throws {
56+
let response = HTTPResponse(
57+
status: .ok,
58+
headerFields: [
59+
.server: "HTTPServer/1.0"
60+
]
61+
)
62+
63+
let urlResponse = try XCTUnwrap(
64+
HTTPURLResponse(httpResponse: response, url: URL(string: "https://www.example.com/")!)
65+
)
66+
XCTAssertEqual(urlResponse.statusCode, 200)
67+
XCTAssertEqual(urlResponse.value(forHTTPHeaderField: "Server"), "HTTPServer/1.0")
68+
}
69+
70+
func testResponseFromFoundation() throws {
71+
let urlResponse = HTTPURLResponse(
72+
url: URL(string: "https://www.example.com/")!,
73+
statusCode: 204,
74+
httpVersion: nil,
75+
headerFields: [
76+
"X-Emoji": "😀"
77+
]
78+
)!
79+
80+
let response = try XCTUnwrap(urlResponse.httpResponse)
81+
XCTAssertEqual(response.status, .noContent)
82+
XCTAssertEqual(response.headerFields[.init("X-EMOJI")!], "😀")
83+
}
84+
}

Tests/HTTPTypesFoundationTests/HTTPTypesFoundationTests.swift

+1-70
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift open source project
44
//
5-
// Copyright (c) 2023 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2023-2024 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0
77
//
88
// See LICENSE.txt for license information
@@ -16,10 +16,6 @@ import HTTPTypes
1616
import HTTPTypesFoundation
1717
import XCTest
1818

19-
#if canImport(FoundationNetworking)
20-
import FoundationNetworking
21-
#endif
22-
2319
final class HTTPTypesFoundationTests: XCTestCase {
2420
func testRequestURLParsing() {
2521
let request1 = HTTPRequest(url: URL(string: "h://a")!)
@@ -82,69 +78,4 @@ final class HTTPTypesFoundationTests: XCTestCase {
8278
XCTAssertEqual(request4.path, "/")
8379
XCTAssertEqual(request4.url?.absoluteString, "https://127.0.0.1:443/")
8480
}
85-
86-
func testRequestToFoundation() throws {
87-
let request = HTTPRequest(
88-
method: .get,
89-
scheme: "https",
90-
authority: "www.example.com",
91-
path: "/",
92-
headerFields: [
93-
.accept: "*/*",
94-
.acceptEncoding: "gzip",
95-
.acceptEncoding: "br",
96-
.cookie: "a=b",
97-
.cookie: "c=d",
98-
]
99-
)
100-
101-
let urlRequest = try XCTUnwrap(URLRequest(httpRequest: request))
102-
XCTAssertEqual(urlRequest.url, URL(string: "https://www.example.com/")!)
103-
XCTAssertEqual(urlRequest.value(forHTTPHeaderField: "aCcEpT"), "*/*")
104-
XCTAssertEqual(urlRequest.value(forHTTPHeaderField: "Accept-Encoding"), "gzip, br")
105-
XCTAssertEqual(urlRequest.value(forHTTPHeaderField: "cookie"), "a=b; c=d")
106-
}
107-
108-
func testRequestFromFoundation() throws {
109-
var urlRequest = URLRequest(url: URL(string: "https://www.example.com/")!)
110-
urlRequest.httpMethod = "POST"
111-
urlRequest.setValue("Bar", forHTTPHeaderField: "X-Foo")
112-
113-
let request = try XCTUnwrap(urlRequest.httpRequest)
114-
XCTAssertEqual(request.method, .post)
115-
XCTAssertEqual(request.scheme, "https")
116-
XCTAssertEqual(request.authority, "www.example.com")
117-
XCTAssertEqual(request.path, "/")
118-
XCTAssertEqual(request.headerFields[.init("x-foo")!], "Bar")
119-
}
120-
121-
func testResponseToFoundation() throws {
122-
let response = HTTPResponse(
123-
status: .ok,
124-
headerFields: [
125-
.server: "HTTPServer/1.0"
126-
]
127-
)
128-
129-
let urlResponse = try XCTUnwrap(
130-
HTTPURLResponse(httpResponse: response, url: URL(string: "https://www.example.com/")!)
131-
)
132-
XCTAssertEqual(urlResponse.statusCode, 200)
133-
XCTAssertEqual(urlResponse.value(forHTTPHeaderField: "Server"), "HTTPServer/1.0")
134-
}
135-
136-
func testResponseFromFoundation() throws {
137-
let urlResponse = HTTPURLResponse(
138-
url: URL(string: "https://www.example.com/")!,
139-
statusCode: 204,
140-
httpVersion: nil,
141-
headerFields: [
142-
"X-Emoji": "😀"
143-
]
144-
)!
145-
146-
let response = try XCTUnwrap(urlResponse.httpResponse)
147-
XCTAssertEqual(response.status, .noContent)
148-
XCTAssertEqual(response.headerFields[.init("X-EMOJI")!], "😀")
149-
}
15081
}

0 commit comments

Comments
 (0)