14
14
15
15
import HTTPTypes
16
16
import Hummingbird
17
+ import HummingbirdCore
17
18
import HummingbirdXCT
18
19
import NIOCore
19
20
import NIOHTTP1
20
21
import OpenAPIRuntime
22
+ import NIOHTTPTypes
21
23
import XCTest
22
24
23
25
@testable import OpenAPIHummingbird
@@ -29,26 +31,25 @@ extension HTTPField.Name {
29
31
30
32
final class HBOpenAPITransportTests : XCTestCase {
31
33
func test_requestConversion( ) async throws {
32
- let app = HBApplication ( testing : . live )
34
+ let router = HBRouter ( )
33
35
34
- app . router. post ( " /hello/:name " ) { hbRequest -> HBResponse in
36
+ router. post ( " /hello/:name " ) { hbRequest, context -> HBResponse in
35
37
// Hijack the request handler to test the request-conversion functions.
36
38
let expectedRequest = HTTPRequest (
37
39
method: . post,
38
- scheme: nil ,
39
- authority: nil ,
40
+ scheme: " http " ,
41
+ authority: " localhost " ,
40
42
path: " /hello/Maria?greeting=Howdy " ,
41
43
headerFields: [
42
44
. xMumble: " mumble " ,
43
45
. connection: " keep-alive " ,
44
- . host: " localhost " ,
45
46
. contentLength: " 4 " ,
46
47
]
47
48
)
48
49
let expectedRequestMetadata = ServerRequestMetadata (
49
50
pathParameters: [ " name " : " Maria " ]
50
51
)
51
- let ( request, body) = try hbRequest. makeOpenAPIRequest ( )
52
+ let ( request, body) = try hbRequest. makeOpenAPIRequest ( context : context )
52
53
let collectedBody : [ UInt8 ]
53
54
if let body = body {
54
55
collectedBody = try await . init( collecting: body, upTo: . max)
@@ -57,80 +58,89 @@ final class HBOpenAPITransportTests: XCTestCase {
57
58
}
58
59
XCTAssertEqual ( request, expectedRequest)
59
60
XCTAssertEqual ( collectedBody, [ UInt8] ( " 👋 " . utf8) )
60
- XCTAssertEqual ( hbRequest . makeOpenAPIRequestMetadata ( ) , expectedRequestMetadata)
61
+ XCTAssertEqual ( context . makeOpenAPIRequestMetadata ( ) , expectedRequestMetadata)
61
62
62
63
// Use the response-conversion to create the HBRequest for returning.
63
64
let response = HTTPResponse ( status: . created, headerFields: [ . xMumble: " mumble " ] )
64
65
return HBResponse ( response, body: . init( [ UInt8] ( " 👋 " . utf8) ) )
65
66
}
66
67
67
- try app. XCTStart ( )
68
- defer { app. XCTStop ( ) }
69
-
70
- try app. XCTExecute (
71
- uri: " /hello/Maria?greeting=Howdy " ,
72
- method: . POST,
73
- headers: [ " X-Mumble " : " mumble " ] ,
74
- body: ByteBuffer ( string: " 👋 " )
75
- ) { hbResponse in
76
- // Check the HBResponse (created from the Response) is what meets expectations.
77
- XCTAssertEqual ( hbResponse. status, . created)
78
- XCTAssertEqual ( hbResponse. headers. first ( name: " X-Mumble " ) , " mumble " )
79
- XCTAssertEqual ( try String ( buffer: XCTUnwrap ( hbResponse. body) ) , " 👋 " )
68
+ let app = HBApplication ( responder: router. buildResponder ( ) )
69
+
70
+ try await app. test ( . live) { client in
71
+ try await client. XCTExecute (
72
+ uri: " /hello/Maria?greeting=Howdy " ,
73
+ method: . post,
74
+ headers: [
75
+ . xMumble: " mumble " ,
76
+ ] ,
77
+ body: ByteBuffer ( string: " 👋 " )
78
+ ) { hbResponse in
79
+ // Check the HBResponse (created from the Response) is what meets expectations.
80
+ XCTAssertEqual ( hbResponse. status, . created)
81
+ XCTAssertEqual ( hbResponse. headers [ . xMumble] , " mumble " )
82
+ XCTAssertEqual ( try String ( buffer: XCTUnwrap ( hbResponse. body) ) , " 👋 " )
83
+ }
80
84
}
81
85
}
82
86
83
87
func test_largeBody( ) async throws {
84
- let app = HBApplication ( testing: . live)
85
- app. server. addChannelHandler ( BreakupHTTPBodyChannelHandler ( ) )
86
- let bytes = ( 0 ..< 1_000_000 ) . map { _ in UInt8 . random ( in: 0 ... 255 ) }
88
+ let router = HBRouter ( )
89
+ let bytes = ( 0 ..< 1_000_000 ) . map { _ in UInt8 . random ( in: 0 ... 255 ) }
87
90
let byteBuffer = ByteBuffer ( bytes: bytes)
88
91
89
- app . router. post ( " /hello/:name " ) { hbRequest -> HBResponse in
92
+ router. post ( " /hello/:name " ) { hbRequest, context -> HBResponse in
90
93
// Hijack the request handler to test the request-conversion functions.
91
94
let expectedRequest = HTTPRequest (
92
95
method: . post,
93
- scheme: nil ,
94
- authority: nil ,
96
+ scheme: " http " ,
97
+ authority: " localhost " ,
95
98
path: " /hello/Maria?greeting=Howdy " ,
96
99
headerFields: [
97
100
. connection: " keep-alive " ,
98
- . host: " localhost " ,
99
101
. contentLength: " 1000000 " ,
100
102
]
101
103
)
102
104
let expectedRequestMetadata = ServerRequestMetadata (
103
105
pathParameters: [ " name " : " Maria " ]
104
106
)
105
- let ( request, body) = try hbRequest. makeOpenAPIRequest ( )
107
+ let ( request, body) = try hbRequest. makeOpenAPIRequest ( context : context )
106
108
XCTAssertEqual ( request, expectedRequest)
107
- XCTAssertEqual ( hbRequest . makeOpenAPIRequestMetadata ( ) , expectedRequestMetadata)
109
+ XCTAssertEqual ( context . makeOpenAPIRequestMetadata ( ) , expectedRequestMetadata)
108
110
109
111
// Use the response-conversion to create the HBRequest for returning.
110
112
let response = HTTPResponse ( status: . ok)
111
113
return HBResponse ( response, body: body)
112
114
}
113
115
114
- try app. XCTStart ( )
115
- defer { app. XCTStop ( ) }
116
-
117
- try app. XCTExecute (
118
- uri: " /hello/Maria?greeting=Howdy " ,
119
- method: . POST,
120
- body: byteBuffer
121
- ) { hbResponse in
122
- // Check the HBResponse (created from the Response) is what meets expectations.
123
- XCTAssertEqual ( hbResponse. status, . ok)
124
- XCTAssertEqual ( byteBuffer, hbResponse. body)
116
+ let app = HBApplication (
117
+ router: router,
118
+ server: . http1(
119
+ additionalChannelHandlers: [
120
+ BreakupHTTPBodyChannelHandler ( )
121
+ ]
122
+ )
123
+ )
124
+
125
+ try await app. test ( . live) { client in
126
+ try await client. XCTExecute (
127
+ uri: " /hello/Maria?greeting=Howdy " ,
128
+ method: . post,
129
+ body: byteBuffer
130
+ ) { hbResponse in
131
+ // Check the HBResponse (created from the Response) is what meets expectations.
132
+ XCTAssertEqual ( hbResponse. status, . ok)
133
+ XCTAssertEqual ( byteBuffer, hbResponse. body)
134
+ }
125
135
}
126
136
}
127
137
}
128
138
129
139
/// To test streaming we need to break up the HTTP body into multiple chunks. This channel handler
130
140
/// breaks up the incoming HTTP body into multiple chunks
131
141
class BreakupHTTPBodyChannelHandler : ChannelInboundHandler , RemovableChannelHandler {
132
- typealias InboundIn = HTTPServerRequestPart
133
- typealias InboundOut = HTTPServerRequestPart
142
+ typealias InboundIn = HTTPRequestPart
143
+ typealias InboundOut = HTTPRequestPart
134
144
135
145
func channelRead( context: ChannelHandlerContext , data: NIOAny ) {
136
146
let part = unwrapInboundIn ( data)
0 commit comments