This repository was archived by the owner on Apr 7, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathHTTPTests.swift
More file actions
115 lines (100 loc) · 4.87 KB
/
HTTPTests.swift
File metadata and controls
115 lines (100 loc) · 4.87 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import HTTP
import XCTest
class HTTPTests: XCTestCase {
func testCookieParse() throws {
/// from https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies
guard let (name, value) = HTTPCookieValue.parse("id=a3fWa; Expires=Wed, 21 Oct 2015 07:28:00 GMT; Secure; HttpOnly") else {
throw HTTPError(identifier: "cookie", reason: "Could not parse test cookie")
}
XCTAssertEqual(name, "id")
XCTAssertEqual(value.string, "a3fWa")
XCTAssertEqual(value.expires, Date(rfc1123: "Wed, 21 Oct 2015 07:28:00 GMT"))
XCTAssertEqual(value.isSecure, true)
XCTAssertEqual(value.isHTTPOnly, true)
guard let cookie: (name: String, value: HTTPCookieValue) = HTTPCookieValue.parse("vapor=; Secure; HttpOnly") else {
throw HTTPError(identifier: "cookie", reason: "Could not parse test cookie")
}
XCTAssertEqual(cookie.name, "vapor")
XCTAssertEqual(cookie.value.string, "")
XCTAssertEqual(cookie.value.isSecure, true)
XCTAssertEqual(cookie.value.isHTTPOnly, true)
}
func testCookieIsSerializedCorrectly() throws {
var httpReq = HTTPRequest(method: .GET, url: "/")
guard let (name, value) = HTTPCookieValue.parse("id=value; Expires=Wed, 21 Oct 2015 07:28:00 GMT; Secure; HttpOnly") else {
throw HTTPError(identifier: "cookie", reason: "Could not parse test cookie")
}
httpReq.cookies = HTTPCookies(dictionaryLiteral: (name, value))
XCTAssertEqual(httpReq.headers.firstValue(name: .cookie), "id=value")
}
func testAcceptHeader() throws {
let httpReq = HTTPRequest(method: .GET, url: "/", headers: ["Accept": "text/html, application/json, application/xml;q=0.9, */*;q=0.8"])
XCTAssertTrue(httpReq.accept.mediaTypes.contains(.html))
XCTAssertEqual(httpReq.accept.comparePreference(for: .html, to: .xml), .orderedAscending)
XCTAssertEqual(httpReq.accept.comparePreference(for: .plainText, to: .html), .orderedDescending)
XCTAssertEqual(httpReq.accept.comparePreference(for: .html, to: .json), .orderedSame)
}
func testRemotePeer() throws {
let worker = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let client = try HTTPClient.connect(hostname: "httpbin.org", on: worker).wait()
let httpReq = HTTPRequest(method: .GET, url: "/")
let httpRes = try client.send(httpReq).wait()
XCTAssertEqual(httpRes.remotePeer.port, 80)
}
func testLargeResponseClose() throws {
struct LargeResponder: HTTPServerResponder {
func respond(to request: HTTPRequest, on worker: Worker) -> EventLoopFuture<HTTPResponse> {
let res = HTTPResponse(
status: .ok,
body: String(repeating: "0", count: 2_000_000)
)
return worker.future(res)
}
}
let worker = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let server = try HTTPServer.start(
hostname: "localhost",
port: 8080,
responder: LargeResponder(),
on: worker
) { error in
XCTFail("\(error)")
}.wait()
let client = try HTTPClient.connect(hostname: "localhost", port: 8080, on: worker).wait()
var req = HTTPRequest(method: .GET, url: "/")
req.headers.replaceOrAdd(name: .connection, value: "close")
let res = try client.send(req).wait()
XCTAssertEqual(res.body.count, 2_000_000)
try server.close().wait()
try server.onClose.wait()
}
func testUpgradeFail() throws {
let worker = MultiThreadedEventLoopGroup(numberOfThreads: 1)
struct FakeUpgrader: HTTPClientProtocolUpgrader {
typealias UpgradeResult = String
func buildUpgradeRequest() -> HTTPRequestHead {
return .init(version: .init(major: 1, minor: 1), method: .GET, uri: "/")
}
func isValidUpgradeResponse(_ upgradeResponse: HTTPResponseHead) -> Bool {
return true
}
func upgrade(ctx: ChannelHandlerContext, upgradeResponse: HTTPResponseHead) -> EventLoopFuture<String> {
return ctx.eventLoop.future("hello")
}
}
do {
_ = try HTTPClient.upgrade(hostname: "foo", upgrader: FakeUpgrader(), on: worker).wait()
XCTFail("expected error")
} catch {
XCTAssert(error is ChannelError)
}
}
static let allTests = [
("testCookieParse", testCookieParse),
("testAcceptHeader", testAcceptHeader),
("testRemotePeer", testRemotePeer),
("testCookieIsSerializedCorrectly", testCookieIsSerializedCorrectly),
("testLargeResponseClose", testLargeResponseClose),
("testUpgradeFail", testUpgradeFail),
]
}