Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -16,6 +17,13 @@ let package = Package(
"HTTPTypes"
]
),
.target(
name: "HTTPTypesFoundationNetworking",
dependencies: [
"HTTPTypes",
"HTTPTypesFoundation",
]
),
.testTarget(
name: "HTTPTypesTests",
dependencies: [
Expand All @@ -25,7 +33,15 @@ let package = Package(
.testTarget(
name: "HTTPTypesFoundationTests",
dependencies: [
"HTTPTypesFoundation"
"HTTPTypes",
"HTTPTypesFoundation",
]
),
.testTarget(
name: "HTTPTypesFoundationNetworkingTests",
dependencies: [
"HTTPTypes",
"HTTPTypesFoundationNetworking",
]
),
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -14,6 +14,7 @@

import Foundation
import HTTPTypes
import HTTPTypesFoundation

#if canImport(FoundationNetworking)
import FoundationNetworking
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//===----------------------------------------------------------------------===//
//
// 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

#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

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")!], "😀")
}
}
71 changes: 1 addition & 70 deletions Tests/HTTPTypesFoundationTests/HTTPTypesFoundationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")!)
Expand Down Expand Up @@ -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")!], "😀")
}
}
Loading