-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathHTTPTypesFoundationTests.swift
88 lines (78 loc) · 3.02 KB
/
HTTPTypesFoundationTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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")!], "😀")
}
}