forked from vapor/vapor
/
ServerTests.swift
716 lines (592 loc) · 25.4 KB
/
ServerTests.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
import Vapor
import XCTest
import protocol AsyncHTTPClient.HTTPClientResponseDelegate
final class ServerTests: XCTestCase {
func testPortOverride() throws {
let env = Environment(
name: "testing",
arguments: ["vapor", "serve", "--port", "8123"]
)
let app = Application(env)
defer { app.shutdown() }
app.get("foo") { req in
return "bar"
}
try app.start()
let res = try app.client.get("http://127.0.0.1:8123/foo").wait()
XCTAssertEqual(res.body?.string, "bar")
}
func testSocketPathOverride() throws {
let socketPath = "/tmp/\(UUID().uuidString).vapor.socket"
let env = Environment(
name: "testing",
arguments: ["vapor", "serve", "--unix-socket", socketPath]
)
let app = Application(env)
defer { app.shutdown() }
app.get("foo") { req in
return "bar"
}
try app.start()
let res = try app.client.get(.init(scheme: .httpUnixDomainSocket, host: socketPath, path: "/foo")).wait()
XCTAssertEqual(res.body?.string, "bar")
// no server should be bound to the port despite one being set on the configuration.
XCTAssertThrowsError(try app.client.get("http://127.0.0.1:8080/foo").wait())
}
func testIncompatibleStartupOptions() throws {
func checkForError(_ app: Application) {
XCTAssertThrowsError(try app.start()) { error in
XCTAssertNotNil(error as? ServeCommand.Error)
guard let serveError = error as? ServeCommand.Error else {
XCTFail("\(error) is not a ServeCommandError")
return
}
XCTAssertEqual(ServeCommand.Error.incompatibleFlags, serveError)
}
app.shutdown()
}
var app = Application(Environment(
name: "testing",
arguments: ["vapor", "serve", "--port", "8123", "--unix-socket", "/path/to/socket"]
))
checkForError(app)
app = Application(Environment(
name: "testing",
arguments: ["vapor", "serve", "--hostname", "localhost", "--unix-socket", "/path/to/socket"]
))
checkForError(app)
app = Application(Environment(
name: "testing",
arguments: ["vapor", "serve", "--bind", "localhost:8123", "--unix-socket", "/path/to/socket"]
))
checkForError(app)
app = Application(Environment(
name: "testing",
arguments: ["vapor", "serve", "--bind", "localhost:8123", "--hostname", "1.2.3.4"]
))
checkForError(app)
app = Application(Environment(
name: "testing",
arguments: ["vapor", "serve", "--bind", "localhost:8123", "--port", "8081"]
))
checkForError(app)
app = Application(Environment(
name: "testing",
arguments: ["vapor", "serve", "--bind", "localhost:8123", "--port", "8081", "--unix-socket", "/path/to/socket"]
))
checkForError(app)
app = Application(Environment(
name: "testing",
arguments: ["vapor", "serve", "--bind", "localhost:8123", "--hostname", "1.2.3.4", "--unix-socket", "/path/to/socket"]
))
checkForError(app)
app = Application(Environment(
name: "testing",
arguments: ["vapor", "serve", "--hostname", "1.2.3.4", "--port", "8081", "--unix-socket", "/path/to/socket"]
))
checkForError(app)
app = Application(Environment(
name: "testing",
arguments: ["vapor", "serve", "--bind", "localhost:8123", "--hostname", "1.2.3.4", "--port", "8081", "--unix-socket", "/path/to/socket"]
))
checkForError(app)
}
@available(*, deprecated)
func testDeprecatedServerStartMethods() throws {
/// TODO: This test may be removed in the next major version
class OldServer: Server {
var onShutdown: EventLoopFuture<Void> {
preconditionFailure("We should never get here.")
}
func shutdown() { }
var hostname:String? = ""
var port:Int? = 0
// only implements the old requirement
func start(hostname: String?, port: Int?) throws {
self.hostname = hostname
self.port = port
}
}
// Ensure we always start with something other than what we expect when calling start
var oldServer = OldServer()
XCTAssertNotNil(oldServer.hostname)
XCTAssertNotNil(oldServer.port)
// start() should set the hostname and port to nil
oldServer = OldServer()
try oldServer.start()
XCTAssertNil(oldServer.hostname)
XCTAssertNil(oldServer.port)
// start(hostname: ..., port: ...) should set the hostname and port appropriately
oldServer = OldServer()
try oldServer.start(hostname: "1.2.3.4", port: 123)
XCTAssertEqual(oldServer.hostname, "1.2.3.4")
XCTAssertEqual(oldServer.port, 123)
// start(address: .hostname(..., port: ...)) should set the hostname and port appropriately
oldServer = OldServer()
try oldServer.start(address: .hostname("localhost", port: 8080))
XCTAssertEqual(oldServer.hostname, "localhost")
XCTAssertEqual(oldServer.port, 8080)
// start(address: .unixDomainSocket(path: ...)) should throw
oldServer = OldServer()
XCTAssertThrowsError(try oldServer.start(address: .unixDomainSocket(path: "/path")))
class NewServer: Server {
var onShutdown: EventLoopFuture<Void> {
preconditionFailure("We should never get here.")
}
func shutdown() { }
var hostname: String? = ""
var port: Int? = 0
var socketPath: String? = ""
func start(address: BindAddress?) throws {
switch address {
case .none:
self.hostname = nil
self.port = nil
self.socketPath = nil
case .hostname(let hostname, let port):
self.hostname = hostname
self.port = port
self.socketPath = nil
case .unixDomainSocket(let path):
self.hostname = nil
self.port = nil
self.socketPath = path
}
}
}
// Ensure we always start with something other than what we expect when calling start
var newServer = NewServer()
XCTAssertNotNil(newServer.hostname)
XCTAssertNotNil(newServer.port)
XCTAssertNotNil(newServer.socketPath)
// start() should set the hostname and port to nil
newServer = NewServer()
try newServer.start()
XCTAssertNil(newServer.hostname)
XCTAssertNil(newServer.port)
XCTAssertNil(newServer.socketPath)
// start(hostname: ..., port: ...) should set the hostname and port appropriately
newServer = NewServer()
try newServer.start(hostname: "1.2.3.4", port: 123)
XCTAssertEqual(newServer.hostname, "1.2.3.4")
XCTAssertEqual(newServer.port, 123)
XCTAssertNil(newServer.socketPath)
// start(address: .hostname(..., port: ...)) should set the hostname and port appropriately
newServer = NewServer()
try newServer.start(address: .hostname("localhost", port: 8080))
XCTAssertEqual(newServer.hostname, "localhost")
XCTAssertEqual(newServer.port, 8080)
XCTAssertNil(newServer.socketPath)
// start(address: .unixDomainSocket(path: ...)) should throw
newServer = NewServer()
try newServer.start(address: .unixDomainSocket(path: "/path"))
XCTAssertNil(newServer.hostname)
XCTAssertNil(newServer.port)
XCTAssertEqual(newServer.socketPath, "/path")
}
func testConfigureHTTPDecompressionLimit() throws {
let app = Application(.testing)
defer { app.shutdown() }
let smallOrigString = "Hello, world!"
let smallBody = ByteBuffer(base64String: "H4sIAAAAAAAAE/NIzcnJ11Eozy/KSVEEAObG5usNAAA=")! // "Hello, world!"
let bigBody = ByteBuffer(base64String: "H4sIAAAAAAAAE/NIzcnJ11HILU3OgBBJmenpqUUK5flFOSkKJRmJeQpJqWn5RamKAICcGhUqAAAA")! // "Hello, much much bigger world than before!"
// Max out at the smaller payload (.size is of compressed data)
app.http.server.configuration.requestDecompression = .enabled(
limit: .size(smallBody.readableBytes)
)
app.post("gzip") { $0.body.string ?? "" }
try app.server.start()
defer { app.server.shutdown() }
// Small payload should just barely get through.
let res = try app.client.post("http://localhost:8080/gzip") { req in
req.headers.replaceOrAdd(name: .contentEncoding, value: "gzip")
req.body = smallBody
}.wait()
XCTAssertEqual(res.body?.string, smallOrigString)
// Big payload should be hard-rejected. We can't test for the raw NIOHTTPDecompression.DecompressionError.limit error here because
// protocol decoding errors are only ever logged and can't be directly caught.
do {
_ = try app.client.post("http://localhost:8080/gzip") { req in
req.headers.replaceOrAdd(name: .contentEncoding, value: "gzip")
req.body = bigBody
}.wait()
} catch let error as HTTPClientError {
XCTAssertEqual(error, HTTPClientError.remoteConnectionClosed)
} catch {
XCTFail("\(error)")
}
}
func testLiveServer() throws {
let app = Application(.testing)
defer { app.shutdown() }
app.routes.get("ping") { req -> String in
return "123"
}
try app.testable().test(.GET, "/ping") { res in
XCTAssertEqual(res.status, .ok)
XCTAssertEqual(res.body.string, "123")
}
}
func testCustomServer() throws {
let app = Application(.testing)
defer { app.shutdown() }
app.servers.use(.custom)
XCTAssertEqual(app.customServer.didStart, false)
XCTAssertEqual(app.customServer.didShutdown, false)
try app.server.start()
XCTAssertEqual(app.customServer.didStart, true)
XCTAssertEqual(app.customServer.didShutdown, false)
app.server.shutdown()
XCTAssertEqual(app.customServer.didStart, true)
XCTAssertEqual(app.customServer.didShutdown, true)
}
func testMultipleChunkBody() throws {
let app = Application(.testing)
defer { app.shutdown() }
let payload = [UInt8].random(count: 1 << 20)
app.on(.POST, "payload", body: .collect(maxSize: "1gb")) { req -> HTTPStatus in
guard let data = req.body.data else {
throw Abort(.internalServerError)
}
XCTAssertEqual(payload.count, data.readableBytes)
XCTAssertEqual([UInt8](data.readableBytesView), payload)
return .ok
}
var buffer = ByteBufferAllocator().buffer(capacity: payload.count)
buffer.writeBytes(payload)
try app.testable(method: .running).test(.POST, "payload", body: buffer) { res in
XCTAssertEqual(res.status, .ok)
}
}
func testCollectedResponseBodyEnd() throws {
let app = Application(.testing)
defer { app.shutdown() }
app.post("drain") { req -> EventLoopFuture<HTTPStatus> in
let promise = req.eventLoop.makePromise(of: HTTPStatus.self)
req.body.drain { result in
switch result {
case .buffer: break
case .error(let error):
promise.fail(error)
case .end:
promise.succeed(.ok)
}
return req.eventLoop.makeSucceededFuture(())
}
return promise.futureResult
}
try app.testable(method: .running).test(.POST, "drain", beforeRequest: { req in
try req.content.encode(["hello": "world"])
}, afterResponse: { res in
XCTAssertEqual(res.status, .ok)
})
}
// https://github.com/vapor/vapor/issues/1786
func testMissingBody() throws {
struct User: Content { }
let app = Application(.testing)
defer { app.shutdown() }
app.get("user") { req -> User in
return try req.content.decode(User.self)
}
try app.testable().test(.GET, "/user") { res in
XCTAssertEqual(res.status, .unsupportedMediaType)
}
}
// https://github.com/vapor/vapor/issues/2245
func testTooLargePort() throws {
let app = Application(.testing)
defer { app.shutdown() }
app.http.server.configuration.port = .max
XCTAssertThrowsError(try app.start())
}
func testEarlyExitStreamingRequest() throws {
let app = Application(.testing)
defer { app.shutdown() }
app.on(.POST, "upload", body: .stream) { req -> EventLoopFuture<Int> in
guard req.headers.first(name: "test") != nil else {
return req.eventLoop.makeFailedFuture(Abort(.badRequest))
}
var count = 0
let promise = req.eventLoop.makePromise(of: Int.self)
req.body.drain { part in
switch part {
case .buffer(let buffer):
count += buffer.readableBytes
case .error(let error):
promise.fail(error)
case .end:
promise.succeed(count)
}
return req.eventLoop.makeSucceededFuture(())
}
return promise.futureResult
}
var buffer = ByteBufferAllocator().buffer(capacity: 10_000_000)
buffer.writeString(String(repeating: "a", count: 10_000_000))
try app.testable(method: .running).test(.POST, "upload", beforeRequest: { req in
req.body = buffer
}, afterResponse: { res in
XCTAssertEqual(res.status, .badRequest)
}).test(.POST, "upload", beforeRequest: { req in
req.body = buffer
req.headers.replaceOrAdd(name: "test", value: "a")
}, afterResponse: { res in
XCTAssertEqual(res.status, .ok)
})
}
func testEchoServer() throws {
let app = Application(.testing)
defer { app.shutdown() }
final class Context {
var server: [String]
var client: [String]
init() {
self.server = []
self.client = []
}
}
let context = Context()
app.on(.POST, "echo", body: .stream) { request -> Response in
Response(body: .init(stream: { writer in
request.body.drain { body in
switch body {
case .buffer(let buffer):
context.server.append(buffer.string)
return writer.write(.buffer(buffer))
case .error(let error):
return writer.write(.error(error))
case .end:
return writer.write(.end)
}
}
}))
}
let port = 1337
app.http.server.configuration.port = port
try app.start()
let request = try HTTPClient.Request(
url: "http://localhost:\(port)/echo",
method: .POST,
headers: [
"transfer-encoding": "chunked"
],
body: .stream(length: nil, { stream in
stream.write(.byteBuffer(.init(string: "foo"))).flatMap {
stream.write(.byteBuffer(.init(string: "bar")))
}.flatMap {
stream.write(.byteBuffer(.init(string: "baz")))
}
})
)
final class ResponseDelegate: HTTPClientResponseDelegate {
typealias Response = HTTPClient.Response
let context: Context
init(context: Context) {
self.context = context
}
func didReceiveBodyPart(
task: HTTPClient.Task<HTTPClient.Response>,
_ buffer: ByteBuffer
) -> EventLoopFuture<Void> {
self.context.client.append(buffer.string)
return task.eventLoop.makeSucceededFuture(())
}
func didFinishRequest(task: HTTPClient.Task<HTTPClient.Response>) throws -> HTTPClient.Response {
.init(host: "", status: .ok, version: .init(major: 1, minor: 1), headers: [:], body: nil)
}
}
let response = ResponseDelegate(context: context)
_ = try app.http.client.shared.execute(
request: request,
delegate: response
).wait()
XCTAssertEqual(context.server, ["foo", "bar", "baz"])
XCTAssertEqual(context.client, ["foo", "bar", "baz"])
}
func testSkipStreaming() throws {
let app = Application(.testing)
defer { app.shutdown() }
app.on(.POST, "echo", body: .stream) { request in
"hello, world"
}
let port = 1337
app.http.server.configuration.port = port
try app.start()
let request = try HTTPClient.Request(
url: "http://localhost:\(port)/echo",
method: .POST,
headers: [
"transfer-encoding": "chunked"
],
body: .stream(length: nil, { stream in
stream.write(.byteBuffer(.init(string: "foo"))).flatMap {
stream.write(.byteBuffer(.init(string: "bar")))
}.flatMap {
stream.write(.byteBuffer(.init(string: "baz")))
}
})
)
let a = try app.http.client.shared.execute(request: request).wait()
XCTAssertEqual(a.status, .ok)
let b = try app.http.client.shared.execute(request: request).wait()
XCTAssertEqual(b.status, .ok)
}
func testStartWithValidSocketFile() throws {
let socketPath = "/tmp/\(UUID().uuidString).vapor.socket"
let app = Application(.testing)
app.http.server.configuration.address = .unixDomainSocket(path: socketPath)
defer {
app.shutdown()
}
XCTAssertNoThrow(try app.start())
}
func testStartWithUnsupportedSocketFile() throws {
let app = Application(.testing)
app.http.server.configuration.address = .unixDomainSocket(path: "/tmp")
defer { app.shutdown() }
XCTAssertThrowsError(try app.start())
}
func testStartWithInvalidSocketFilePath() throws {
let app = Application(.testing)
app.http.server.configuration.address = .unixDomainSocket(path: "/tmp/nonexistent/vapor.socket")
defer { app.shutdown() }
XCTAssertThrowsError(try app.start())
}
func testStartWithDefaultHostnameConfiguration() throws {
let app = Application(.testing)
app.http.server.configuration.address = .hostname(nil, port: nil)
defer { app.shutdown() }
XCTAssertNoThrow(try app.start())
}
func testStartWithDefaultHostname() throws {
let app = Application(.testing)
app.http.server.configuration.address = .hostname(nil, port: 8008)
defer { app.shutdown() }
XCTAssertNoThrow(try app.start())
}
func testStartWithDefaultPort() throws {
let app = Application(.testing)
app.http.server.configuration.address = .hostname("0.0.0.0", port: nil)
defer { app.shutdown() }
XCTAssertNoThrow(try app.start())
}
func testAddressConfigurations() throws {
var configuration = HTTPServer.Configuration()
XCTAssertEqual(configuration.address, .hostname(HTTPServer.Configuration.defaultHostname, port: HTTPServer.Configuration.defaultPort))
configuration = HTTPServer.Configuration(hostname: "1.2.3.4", port: 123)
XCTAssertEqual(configuration.address, .hostname("1.2.3.4", port: 123))
XCTAssertEqual(configuration.hostname, "1.2.3.4")
XCTAssertEqual(configuration.port, 123)
configuration = HTTPServer.Configuration(address: .hostname("1.2.3.4", port: 123))
XCTAssertEqual(configuration.address, .hostname("1.2.3.4", port: 123))
XCTAssertEqual(configuration.hostname, "1.2.3.4")
XCTAssertEqual(configuration.port, 123)
configuration = HTTPServer.Configuration(address: .hostname("1.2.3.4", port: nil))
XCTAssertEqual(configuration.address, .hostname("1.2.3.4", port: nil))
XCTAssertEqual(configuration.hostname, "1.2.3.4")
XCTAssertEqual(configuration.port, HTTPServer.Configuration.defaultPort)
configuration = HTTPServer.Configuration(address: .hostname(nil, port: 123))
XCTAssertEqual(configuration.address, .hostname(nil, port: 123))
XCTAssertEqual(configuration.hostname, HTTPServer.Configuration.defaultHostname)
XCTAssertEqual(configuration.port, 123)
configuration = HTTPServer.Configuration(address: .hostname(nil, port: nil))
XCTAssertEqual(configuration.address, .hostname(nil, port: nil))
XCTAssertEqual(configuration.hostname, HTTPServer.Configuration.defaultHostname)
XCTAssertEqual(configuration.port, HTTPServer.Configuration.defaultPort)
configuration = HTTPServer.Configuration(address: .unixDomainSocket(path: "/path"))
XCTAssertEqual(configuration.address, .unixDomainSocket(path: "/path"))
// Test mutating a config that was originally a socket path
configuration = HTTPServer.Configuration(address: .unixDomainSocket(path: "/path"))
XCTAssertEqual(configuration.address, .unixDomainSocket(path: "/path"))
configuration.hostname = "1.2.3.4"
XCTAssertEqual(configuration.hostname, "1.2.3.4")
XCTAssertEqual(configuration.port, HTTPServer.Configuration.defaultPort)
XCTAssertEqual(configuration.address, .hostname("1.2.3.4", port: nil))
configuration.address = .unixDomainSocket(path: "/path")
XCTAssertEqual(configuration.hostname, HTTPServer.Configuration.defaultHostname)
XCTAssertEqual(configuration.port, HTTPServer.Configuration.defaultPort)
XCTAssertEqual(configuration.address, .unixDomainSocket(path: "/path"))
configuration.port = 123
XCTAssertEqual(configuration.hostname, HTTPServer.Configuration.defaultHostname)
XCTAssertEqual(configuration.port, 123)
XCTAssertEqual(configuration.address, .hostname(nil, port: 123))
configuration.hostname = "1.2.3.4"
XCTAssertEqual(configuration.hostname, "1.2.3.4")
XCTAssertEqual(configuration.port, 123)
XCTAssertEqual(configuration.address, .hostname("1.2.3.4", port: 123))
configuration.address = .hostname(nil, port: nil)
XCTAssertEqual(configuration.hostname, HTTPServer.Configuration.defaultHostname)
XCTAssertEqual(configuration.port, HTTPServer.Configuration.defaultPort)
XCTAssertEqual(configuration.address, .hostname(nil, port: nil))
}
func testQuiesceKeepAliveConnections() throws {
let app = Application(.testing)
defer { app.shutdown() }
app.get("hello") { req in
"world"
}
let port = 1337
app.http.server.configuration.port = port
try app.start()
let request = try HTTPClient.Request(
url: "http://localhost:\(port)/hello",
method: .GET,
headers: ["connection": "keep-alive"]
)
let a = try app.http.client.shared.execute(request: request).wait()
XCTAssertEqual(a.headers.connection, .keepAlive)
}
override class func setUp() {
XCTAssertTrue(isLoggingConfigured)
}
}
extension Application.Servers.Provider {
static var custom: Self {
.init {
$0.servers.use { $0.customServer }
}
}
}
extension Application {
struct Key: StorageKey {
typealias Value = CustomServer
}
var customServer: CustomServer {
if let existing = self.storage[Key.self] {
return existing
} else {
let new = CustomServer()
self.storage[Key.self] = new
return new
}
}
}
final class CustomServer: Server {
var didStart: Bool
var didShutdown: Bool
var onShutdown: EventLoopFuture<Void> {
fatalError()
}
init() {
self.didStart = false
self.didShutdown = false
}
func start(hostname: String?, port: Int?) throws {
try self.start(address: .hostname(hostname, port: port))
}
func start(address: BindAddress?) throws {
self.didStart = true
}
func shutdown() {
self.didShutdown = true
}
}
private extension ByteBuffer {
init?(base64String: String) {
guard let decoded = Data(base64Encoded: base64String) else { return nil }
var buffer = ByteBufferAllocator().buffer(capacity: decoded.count)
buffer.writeBytes(decoded)
self = buffer
}
}