forked from swift-server/swift-openapi-async-http-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest_AsyncHTTPClientTransport.swift
137 lines (128 loc) · 4.36 KB
/
Test_AsyncHTTPClientTransport.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftOpenAPIGenerator open source project
//
// Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import XCTest
import OpenAPIRuntime
import NIOCore
import NIOPosix
import AsyncHTTPClient
@testable import OpenAPIAsyncHTTPClient
class Test_AsyncHTTPClientTransport: XCTestCase {
static var testData: Data {
get throws {
try XCTUnwrap(#"[{}]"#.data(using: .utf8))
}
}
static var testBuffer: ByteBuffer {
ByteBuffer(string: #"[{}]"#)
}
static var testUrl: URL {
get throws {
try XCTUnwrap(URL(string: "http://example.com/api/v1/hello/Maria?greeting=Howdy"))
}
}
func testConvertRequest() throws {
let request: OpenAPIRuntime.Request = .init(
path: "/hello%20world/Maria",
query: "greeting=Howdy",
method: .post,
headerFields: [
.init(name: "content-type", value: "application/json")
],
body: try Self.testData
)
let httpRequest = try AsyncHTTPClientTransport.convertRequest(
request,
baseURL: try XCTUnwrap(URL(string: "http://example.com/api/v1"))
)
XCTAssertEqual(httpRequest.url, "http://example.com/api/v1/hello%20world/Maria?greeting=Howdy")
XCTAssertEqual(httpRequest.method, .POST)
XCTAssertEqual(
httpRequest.headers,
[
"content-type": "application/json"
]
)
// TODO: Not sure how to test that httpRequest.body is what we expect, can't
// find an API for reading it back.
}
func testConvertResponse() async throws {
let httpResponse = HTTPClientResponse(
status: .ok,
headers: [
"content-type": "application/json"
],
body: .bytes(Self.testBuffer)
)
let response = try await AsyncHTTPClientTransport.convertResponse(httpResponse)
XCTAssertEqual(response.statusCode, 200)
XCTAssertEqual(
response.headerFields,
[
.init(name: "content-type", value: "application/json")
]
)
XCTAssertEqual(response.body, try Self.testData)
}
func testSend() async throws {
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let httpClient = HTTPClient(
eventLoopGroupProvider: .shared(eventLoopGroup),
configuration: .init()
)
defer {
try! httpClient.syncShutdown()
}
let transport = AsyncHTTPClientTransport(
configuration: .init(client: httpClient),
requestSender: TestSender.test
)
let request: OpenAPIRuntime.Request = .init(
path: "/api/v1/hello/Maria",
method: .get,
headerFields: [
.init(name: "x-request", value: "yes")
]
)
let response = try await transport.send(
request,
baseURL: Self.testUrl,
operationID: "sayHello"
)
XCTAssertEqual(response.statusCode, 200)
}
}
struct TestSender: HTTPRequestSending {
var sendClosure:
@Sendable (AsyncHTTPClientTransport.Request, HTTPClient, TimeAmount) async throws -> AsyncHTTPClientTransport
.Response
func send(
request: AsyncHTTPClientTransport.Request,
with client: HTTPClient,
timeout: TimeAmount
) async throws -> AsyncHTTPClientTransport.Response {
try await sendClosure(request, client, timeout)
}
static var test: Self {
TestSender { request, _, _ in
XCTAssertEqual(request.headers.first(name: "x-request"), "yes")
return HTTPClientResponse(
status: .ok,
headers: [
"content-type": "application/json"
],
body: .bytes(Test_AsyncHTTPClientTransport.testBuffer)
)
}
}
}