Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert HTTPTypesTests and HTTPTypesFoundationTests to Swift Testing #62

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
6 changes: 5 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
// swift-tools-version: 5.7.1
// swift-tools-version: 5.10

import PackageDescription

let package = Package(
name: "swift-http-types",
platforms: [.macOS(.v10_15)],
products: [
.library(name: "HTTPTypes", targets: ["HTTPTypes"]),
.library(name: "HTTPTypesFoundation", targets: ["HTTPTypesFoundation"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.3.0"),
.package(url: "https://github.com/apple/swift-testing.git", from: "0.11.0"),
],
targets: [
.target(name: "HTTPTypes"),
Expand All @@ -23,12 +25,14 @@ let package = Package(
name: "HTTPTypesTests",
dependencies: [
"HTTPTypes",
.product(name: "Testing", package: "swift-testing"),
]
),
.testTarget(
name: "HTTPTypesFoundationTests",
dependencies: [
"HTTPTypesFoundation",
.product(name: "Testing", package: "swift-testing"),
]
),
]
Expand Down
95 changes: 51 additions & 44 deletions Tests/HTTPTypesFoundationTests/HTTPTypesFoundationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,51 @@
//
//===----------------------------------------------------------------------===//

import Foundation
import HTTPTypes
import HTTPTypesFoundation
import XCTest
import Testing
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

final class HTTPTypesFoundationTests: XCTestCase {
func testRequestURLParsing() {
@Suite("HTTPTypesFoundationTests")
struct HTTPTypesFoundationTests {
@Test("Request URL parses correctly")
func requestURLParsing() {
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/")
#expect(request1.scheme == "h")
#expect(request1.authority == "a")
#expect(request1.path == "/")
#expect(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/?")
#expect(request2.scheme == "h")
#expect(request2.authority == "a:4")
#expect(request2.path == "/?")
#expect(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/")
#expect(request3.scheme == "h")
#expect(request3.authority == "a")
#expect(request3.path == "/")
#expect(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")
#expect(request4.scheme == "h")
#expect(request4.authority == "a")
#expect(request4.path == "/p?q")
#expect(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)
XCTAssertEqual(request5.path, "/")
XCTAssertNil(request5.url)
#expect(request5.scheme == "data")
#expect(request5.authority == nil)
#expect(request5.path == "/")
#expect(request5.url == nil)
}

func testRequestToFoundation() throws {
@Test("Request converts to Foundation")
func requestToFoundation() throws {
let request = HTTPRequest(
method: .get, scheme: "https", authority: "www.example.com", path: "/",
headerFields: [
Expand All @@ -64,49 +68,52 @@ final class HTTPTypesFoundationTests: XCTestCase {
]
)

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")
let urlRequest = try #require(URLRequest(httpRequest: request))
#expect(urlRequest.url == URL(string: "https://www.example.com/")!)
#expect(urlRequest.value(forHTTPHeaderField: "aCcEpT") == "*/*")
#expect(urlRequest.value(forHTTPHeaderField: "Accept-Encoding") == "gzip, br")
#expect(urlRequest.value(forHTTPHeaderField: "cookie") == "a=b; c=d")
}

func testRequestFromFoundation() throws {
@Test("Request creates from Foundation")
func requestFromFoundation() 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")
let request = try #require(urlRequest.httpRequest)
#expect(request.method == .post)
#expect(request.scheme == "https")
#expect(request.authority == "www.example.com")
#expect(request.path == "/")
#expect(request.headerFields[.init("x-foo")!] == "Bar")
}

func testResponseToFoundation() throws {
@Test("Response converts to Foundation")
func responseToFoundation() 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")
let urlResponse = try #require(HTTPURLResponse(httpResponse: response, url: URL(string: "https://www.example.com/")!))
#expect(urlResponse.statusCode == 200)
#expect(urlResponse.value(forHTTPHeaderField: "Server") == "HTTPServer/1.0")
}

func testResponseFromFoundation() throws {
@Test("Response creates from Foundation")
func responseFromFoundation() 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")!], "😀")
let response = try #require(urlResponse.httpResponse)
#expect(response.status == .noContent)
#expect(response.headerFields[.init("X-EMOJI")!] == "😀")
}
}
Loading