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 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
//
// HTTPServerPipelineHandlerTest.swift
// NIOHTTP1Tests
//
// Created by Cory Benfield on 02/03/2018.
//
import XCTest
import NIO
@testable import NIOHTTP1
private final class ReadRecorder: ChannelInboundHandler {
typealias InboundIn = HTTPServerRequestPart
enum Event: Equatable {
case channelRead(InboundIn)
case halfClose
static func ==(lhs: Event, rhs: Event) -> Bool {
switch (lhs, rhs) {
case (.channelRead(let b1), .channelRead(let b2)):
return b1 == b2
case (.halfClose, .halfClose):
return true
default:
return false
}
}
}
public var reads: [Event] = []
func channelRead(context: ChannelHandlerContext, data: NIOAny) {
self.reads.append(.channelRead(self.unwrapInboundIn(data)))
context.fireChannelRead(data)
}
func userInboundEventTriggered(context: ChannelHandlerContext, event: Any) {
switch event {
case let evt as ChannelEvent where evt == ChannelEvent.inputClosed:
self.reads.append(.halfClose)
default:
context.fireUserInboundEventTriggered(event)
}
}
}
private final class WriteRecorder: ChannelOutboundHandler {
typealias OutboundIn = HTTPServerResponsePart
public var writes: [HTTPServerResponsePart] = []
func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
self.writes.append(self.unwrapOutboundIn(data))
context.write(data, promise: promise)
}
}
private final class ReadCountingHandler: ChannelOutboundHandler {
typealias OutboundIn = Any
typealias OutboundOut = Any
public var readCount = 0
func read(context: ChannelHandlerContext) {
self.readCount += 1
context.read()
}
}
private final class QuiesceEventRecorder: ChannelInboundHandler {
typealias InboundIn = Any
typealias InboundOut = Any
public var quiesceCount = 0
func userInboundEventTriggered(context: ChannelHandlerContext, event: Any) {
if event is ChannelShouldQuiesceEvent {
quiesceCount += 1
}
context.fireUserInboundEventTriggered(event)
}
}
class HTTPServerPipelineHandlerTest: XCTestCase {
var channel: EmbeddedChannel! = nil
var requestHead: HTTPRequestHead! = nil
var responseHead: HTTPResponseHead! = nil
fileprivate var readRecorder: ReadRecorder! = nil
fileprivate var readCounter: ReadCountingHandler! = nil
fileprivate var writeRecorder: WriteRecorder! = nil
fileprivate var pipelineHandler: HTTPServerPipelineHandler! = nil
fileprivate var quiesceEventRecorder: QuiesceEventRecorder! = nil
override func setUp() {
self.channel = EmbeddedChannel()
self.readRecorder = ReadRecorder()
self.readCounter = ReadCountingHandler()
self.writeRecorder = WriteRecorder()
self.pipelineHandler = HTTPServerPipelineHandler()
self.quiesceEventRecorder = QuiesceEventRecorder()
XCTAssertNoThrow(try channel.pipeline.addHandler(self.readCounter).wait())
XCTAssertNoThrow(try channel.pipeline.addHandler(HTTPResponseEncoder()).wait())
XCTAssertNoThrow(try channel.pipeline.addHandler(self.writeRecorder).wait())
XCTAssertNoThrow(try channel.pipeline.addHandler(self.pipelineHandler).wait())
XCTAssertNoThrow(try channel.pipeline.addHandler(self.readRecorder).wait())
XCTAssertNoThrow(try channel.pipeline.addHandler(self.quiesceEventRecorder).wait())
self.requestHead = HTTPRequestHead(version: .http1_1, method: .GET, uri: "/path")
self.requestHead.headers.add(name: "Host", value: "example.com")
self.responseHead = HTTPResponseHead(version: .http1_1, status: .ok)
self.responseHead.headers.add(name: "Server", value: "SwiftNIO")
// this activates the channel
XCTAssertNoThrow(try self.channel.connect(to: SocketAddress(ipAddress: "127.0.0.1", port: 1)).wait())
}
override func tearDown() {
if let channel = self.channel {
XCTAssertNoThrow(try channel.finish(acceptAlreadyClosed: true))
self.channel = nil
}
self.requestHead = nil
self.responseHead = nil
self.readRecorder = nil
self.readCounter = nil
self.writeRecorder = nil
self.pipelineHandler = nil
self.quiesceEventRecorder = nil
}
func testBasicBufferingBehaviour() throws {
// Send in 3 requests at once.
for _ in 0..<3 {
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(self.requestHead)))
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.end(nil)))
}
// Only one request should have made it through.
XCTAssertEqual(self.readRecorder.reads,
[.channelRead(HTTPServerRequestPart.head(self.requestHead)),
.channelRead(HTTPServerRequestPart.end(nil))])
// Unblock by sending a response.
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.head(self.responseHead)).wait())
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.end(nil)).wait())
// Two requests should have made it through now.
XCTAssertEqual(self.readRecorder.reads,
[.channelRead(HTTPServerRequestPart.head(self.requestHead)),
.channelRead(HTTPServerRequestPart.end(nil)),
.channelRead(HTTPServerRequestPart.head(self.requestHead)),
.channelRead(HTTPServerRequestPart.end(nil))])
// Now send the last response.
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.head(self.responseHead)).wait())
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.end(nil)).wait())
// Now all three.
XCTAssertEqual(self.readRecorder.reads,
[.channelRead(HTTPServerRequestPart.head(self.requestHead)),
.channelRead(HTTPServerRequestPart.end(nil)),
.channelRead(HTTPServerRequestPart.head(self.requestHead)),
.channelRead(HTTPServerRequestPart.end(nil)),
.channelRead(HTTPServerRequestPart.head(self.requestHead)),
.channelRead(HTTPServerRequestPart.end(nil))])
}
func testReadCallsAreSuppressedWhenPipelining() throws {
// First, call read() and check it makes it through.
XCTAssertEqual(self.readCounter.readCount, 0)
self.channel.read()
XCTAssertEqual(self.readCounter.readCount, 1)
// Send in a request.
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(self.requestHead)))
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.end(nil)))
// Call read again, twice. This should not change the number.
self.channel.read()
self.channel.read()
XCTAssertEqual(self.readCounter.readCount, 1)
// Send a response.
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.head(self.responseHead)).wait())
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.end(nil)).wait())
// This should have automatically triggered a call to read(), but only one.
XCTAssertEqual(self.readCounter.readCount, 2)
}
func testReadCallsAreSuppressedWhenUnbufferingIfThereIsStillBufferedData() throws {
// First, call read() and check it makes it through.
XCTAssertEqual(self.readCounter.readCount, 0)
self.channel.read()
XCTAssertEqual(self.readCounter.readCount, 1)
// Send in two requests.
for _ in 0..<2 {
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(self.requestHead)))
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.end(nil)))
}
// Call read again, twice. This should not change the number.
self.channel.read()
self.channel.read()
XCTAssertEqual(self.readCounter.readCount, 1)
// Send a response.
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.head(self.responseHead)).wait())
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.end(nil)).wait())
// This should have not triggered a call to read.
XCTAssertEqual(self.readCounter.readCount, 1)
// Try calling read some more.
self.channel.read()
self.channel.read()
XCTAssertEqual(self.readCounter.readCount, 1)
// Now send in the last response, and see the read go through.
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.head(self.responseHead)).wait())
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.end(nil)).wait())
XCTAssertEqual(self.readCounter.readCount, 2)
}
func testServerCanRespondEarly() throws {
// Send in the first part of a request.
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(self.requestHead)))
// This is still moving forward: we can read.
XCTAssertEqual(self.readCounter.readCount, 0)
self.channel.read()
XCTAssertEqual(self.readCounter.readCount, 1)
// Now the server sends a response immediately.
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.head(self.responseHead)).wait())
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.end(nil)).wait())
// We're still moving forward and can read.
XCTAssertEqual(self.readCounter.readCount, 1)
self.channel.read()
XCTAssertEqual(self.readCounter.readCount, 2)
// The client response completes.
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.end(nil)))
// We can still read.
XCTAssertEqual(self.readCounter.readCount, 2)
self.channel.read()
XCTAssertEqual(self.readCounter.readCount, 3)
}
func testPipelineHandlerWillBufferHalfClose() throws {
// Send in 2 requests at once.
for _ in 0..<3 {
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(self.requestHead)))
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.end(nil)))
}
// Now half-close the connection.
self.channel.pipeline.fireUserInboundEventTriggered(ChannelEvent.inputClosed)
// Only one request should have made it through, no half-closure yet.
XCTAssertEqual(self.readRecorder.reads,
[.channelRead(HTTPServerRequestPart.head(self.requestHead)),
.channelRead(HTTPServerRequestPart.end(nil))])
// Unblock by sending a response.
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.head(self.responseHead)).wait())
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.end(nil)).wait())
// Two requests should have made it through now.
XCTAssertEqual(self.readRecorder.reads,
[.channelRead(HTTPServerRequestPart.head(self.requestHead)),
.channelRead(HTTPServerRequestPart.end(nil)),
.channelRead(HTTPServerRequestPart.head(self.requestHead)),
.channelRead(HTTPServerRequestPart.end(nil))])
// Now send the last response.
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.head(self.responseHead)).wait())
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.end(nil)).wait())
// Now the half-closure should be delivered.
XCTAssertEqual(self.readRecorder.reads,
[.channelRead(HTTPServerRequestPart.head(self.requestHead)),
.channelRead(HTTPServerRequestPart.end(nil)),
.channelRead(HTTPServerRequestPart.head(self.requestHead)),
.channelRead(HTTPServerRequestPart.end(nil)),
.channelRead(HTTPServerRequestPart.head(self.requestHead)),
.channelRead(HTTPServerRequestPart.end(nil)),
.halfClose])
}
func testPipelineHandlerWillDeliverHalfCloseEarly() throws {
// Send in a request.
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(self.requestHead)))
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.end(nil)))
// Now send a new request but half-close the connection before we get .end.
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(self.requestHead)))
self.channel.pipeline.fireUserInboundEventTriggered(ChannelEvent.inputClosed)
// Only one request should have made it through, no half-closure yet.
XCTAssertEqual(self.readRecorder.reads,
[.channelRead(HTTPServerRequestPart.head(self.requestHead)),
.channelRead(HTTPServerRequestPart.end(nil))])
// Unblock by sending a response.
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.head(self.responseHead)).wait())
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.end(nil)).wait())
// The second request head, followed by the half-close, should have made it through.
XCTAssertEqual(self.readRecorder.reads,
[.channelRead(HTTPServerRequestPart.head(self.requestHead)),
.channelRead(HTTPServerRequestPart.end(nil)),
.channelRead(HTTPServerRequestPart.head(self.requestHead)),
.halfClose])
}
func testAReadIsNotIssuedWhenUnbufferingAHalfCloseAfterRequestComplete() throws {
// First, call read() and check it makes it through.
XCTAssertEqual(self.readCounter.readCount, 0)
self.channel.read()
XCTAssertEqual(self.readCounter.readCount, 1)
// Send in two requests and then half-close.
for _ in 0..<2 {
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(self.requestHead)))
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.end(nil)))
}
self.channel.pipeline.fireUserInboundEventTriggered(ChannelEvent.inputClosed)
// Call read again, twice. This should not change the number.
self.channel.read()
self.channel.read()
XCTAssertEqual(self.readCounter.readCount, 1)
// Send a response.
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.head(self.responseHead)).wait())
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.end(nil)).wait())
// This should have not triggered a call to read.
XCTAssertEqual(self.readCounter.readCount, 1)
// Now send in the last response. This should also not issue a read.
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.head(self.responseHead)).wait())
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.end(nil)).wait())
XCTAssertEqual(self.readCounter.readCount, 1)
}
func testHalfCloseWhileWaitingForResponseIsPassedAlongIfNothingElseBuffered() throws {
// Send in 2 requests at once.
for _ in 0..<2 {
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(self.requestHead)))
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.end(nil)))
}
// Only one request should have made it through, no half-closure yet.
XCTAssertEqual(self.readRecorder.reads,
[.channelRead(HTTPServerRequestPart.head(self.requestHead)),
.channelRead(HTTPServerRequestPart.end(nil))])
// Unblock by sending a response.
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.head(self.responseHead)).wait())
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.end(nil)).wait())
// Two requests should have made it through now. Still no half-closure.
XCTAssertEqual(self.readRecorder.reads,
[.channelRead(HTTPServerRequestPart.head(self.requestHead)),
.channelRead(HTTPServerRequestPart.end(nil)),
.channelRead(HTTPServerRequestPart.head(self.requestHead)),
.channelRead(HTTPServerRequestPart.end(nil))])
// Now send the half-closure.
self.channel.pipeline.fireUserInboundEventTriggered(ChannelEvent.inputClosed)
// The half-closure should be delivered immediately.
XCTAssertEqual(self.readRecorder.reads,
[.channelRead(HTTPServerRequestPart.head(self.requestHead)),
.channelRead(HTTPServerRequestPart.end(nil)),
.channelRead(HTTPServerRequestPart.head(self.requestHead)),
.channelRead(HTTPServerRequestPart.end(nil)),
.halfClose])
}
func testRecursiveChannelReadInvocationsDoNotCauseIssues() throws {
func makeRequestHead(uri: String) -> HTTPRequestHead {
var requestHead = HTTPRequestHead(version: .http1_1, method: .GET, uri: uri)
requestHead.headers.add(name: "Host", value: "example.com")
return requestHead
}
class VerifyOrderHandler: ChannelInboundHandler {
typealias InboundIn = HTTPServerRequestPart
typealias OutboundOut = HTTPServerResponsePart
enum NextExpectedMessageType {
case head
case end
}
enum State {
case req1HeadExpected
case req1EndExpected
case req2HeadExpected
case req2EndExpected
case req3HeadExpected
case req3EndExpected
case reqBoomHeadExpected
case reqBoomEndExpected
case done
}
var state: State = .req1HeadExpected
func channelRead(context: ChannelHandlerContext, data: NIOAny) {
let req = self.unwrapInboundIn(data)
switch req {
case .head(let head):
// except for "req_1", we always send the .end straight away
var sendEnd = true
switch head.uri {
case "/req_1":
XCTAssertEqual(.req1HeadExpected, self.state)
self.state = .req1EndExpected
// for req_1, we don't send the end straight away to force the others to be buffered
sendEnd = false
case "/req_2":
XCTAssertEqual(.req2HeadExpected, self.state)
self.state = .req2EndExpected
case "/req_3":
XCTAssertEqual(.req3HeadExpected, self.state)
self.state = .req3EndExpected
case "/req_boom":
XCTAssertEqual(.reqBoomHeadExpected, self.state)
self.state = .reqBoomEndExpected
default:
XCTFail("didn't expect \(head)")
}
context.write(self.wrapOutboundOut(.head(HTTPResponseHead(version: .http1_1, status: .ok))), promise: nil)
if sendEnd {
context.write(self.wrapOutboundOut(.end(nil)), promise: nil)
}
context.flush()
case .end:
switch self.state {
case .req1EndExpected:
self.state = .req2HeadExpected
case .req2EndExpected:
self.state = .req3HeadExpected
// this will cause `channelRead` to be recursively called and we need to make sure everything then still works
try! (context.channel as! EmbeddedChannel).writeInbound(HTTPServerRequestPart.head(HTTPRequestHead(version: .http1_1, method: .GET, uri: "/req_boom")))
try! (context.channel as! EmbeddedChannel).writeInbound(HTTPServerRequestPart.end(nil))
case .req3EndExpected:
self.state = .reqBoomHeadExpected
case .reqBoomEndExpected:
self.state = .done
default:
XCTFail("illegal state for end: \(self.state)")
}
case .body:
XCTFail("we don't send any bodies")
}
}
}
let handler = VerifyOrderHandler()
XCTAssertNoThrow(try channel.pipeline.addHandler(handler).wait())
for f in 1...3 {
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(makeRequestHead(uri: "/req_\(f)"))))
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.end(nil)))
}
// now we should have delivered the first request, with the second and third buffered because req_1's .end
// doesn't get sent by the handler (instead we'll do that below)
XCTAssertEqual(.req2HeadExpected, handler.state)
// finish 1st request, that will send through the 2nd one which will then write the 'req_boom' request
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.end(nil)).wait())
XCTAssertEqual(.done, handler.state)
}
func testQuiescingEventWhenInitiallyIdle() throws {
XCTAssertTrue(self.channel.isActive)
self.channel.pipeline.fireUserInboundEventTriggered(ChannelShouldQuiesceEvent())
XCTAssertFalse(self.channel.isActive)
XCTAssertEqual(self.quiesceEventRecorder.quiesceCount, 0)
}
func testQuiescingEventWhenIdleAfterARequest() throws {
// Send through one request.
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(self.requestHead)))
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.end(nil)))
// The request should have made it through.
XCTAssertEqual(self.readRecorder.reads,
[.channelRead(HTTPServerRequestPart.head(self.requestHead)),
.channelRead(HTTPServerRequestPart.end(nil))])
// Now send a response.
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.head(self.responseHead)).wait())
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.end(nil)).wait())
// No further events should have happened.
XCTAssertEqual(self.readRecorder.reads,
[.channelRead(HTTPServerRequestPart.head(self.requestHead)),
.channelRead(HTTPServerRequestPart.end(nil))])
XCTAssertTrue(self.channel.isActive)
self.channel.pipeline.fireUserInboundEventTriggered(ChannelShouldQuiesceEvent())
XCTAssertFalse(self.channel.isActive)
XCTAssertEqual(self.quiesceEventRecorder.quiesceCount, 0)
}
func testQuiescingInTheMiddleOfARequestNoResponseBitsYet() throws {
// Send through only the head.
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(self.requestHead)))
XCTAssertEqual(self.readRecorder.reads,
[.channelRead(HTTPServerRequestPart.head(self.requestHead))])
XCTAssertTrue(self.channel.isActive)
self.channel.pipeline.fireUserInboundEventTriggered(ChannelShouldQuiesceEvent())
XCTAssertTrue(self.channel.isActive)
// Now send a response.
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.head(self.responseHead)).wait())
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.end(nil)).wait())
// still missing the request .end
XCTAssertTrue(self.channel.isActive)
var reqWithConnectionClose: HTTPResponseHead = self.responseHead
reqWithConnectionClose.headers.add(name: "connection", value: "close")
XCTAssertEqual([HTTPServerResponsePart.head(reqWithConnectionClose),
HTTPServerResponsePart.end(nil)],
self.writeRecorder.writes)
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.end(nil)))
XCTAssertFalse(self.channel.isActive)
XCTAssertEqual(self.quiesceEventRecorder.quiesceCount, 0)
}
func testQuiescingAfterHavingReceivedRequestButBeforeResponseWasSent() throws {
// Send through a full request.
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(self.requestHead)))
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.end(nil)))
XCTAssertEqual(self.readRecorder.reads,
[.channelRead(HTTPServerRequestPart.head(self.requestHead)),
.channelRead(HTTPServerRequestPart.end(nil))])
XCTAssertTrue(self.channel.isActive)
self.channel.pipeline.fireUserInboundEventTriggered(ChannelShouldQuiesceEvent())
XCTAssertTrue(self.channel.isActive)
// Now send a response.
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.head(self.responseHead)).wait())
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.end(nil)).wait())
XCTAssertFalse(self.channel.isActive)
var reqWithConnectionClose: HTTPResponseHead = self.responseHead
reqWithConnectionClose.headers.add(name: "connection", value: "close")
XCTAssertEqual([HTTPServerResponsePart.head(reqWithConnectionClose),
HTTPServerResponsePart.end(nil)],
self.writeRecorder.writes)
XCTAssertFalse(self.channel.isActive)
XCTAssertEqual(self.quiesceEventRecorder.quiesceCount, 0)
}
func testQuiescingAfterHavingReceivedRequestAndResponseHeadButNoResponseEndYet() throws {
// Send through a full request.
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(self.requestHead)))
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.end(nil)))
XCTAssertEqual(self.readRecorder.reads,
[.channelRead(HTTPServerRequestPart.head(self.requestHead)),
.channelRead(HTTPServerRequestPart.end(nil))])
// Now send the response .head.
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.head(self.responseHead)).wait())
XCTAssertTrue(self.channel.isActive)
self.channel.pipeline.fireUserInboundEventTriggered(ChannelShouldQuiesceEvent())
XCTAssertTrue(self.channel.isActive)
// Now send the response .end.
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.end(nil)).wait())
XCTAssertFalse(self.channel.isActive)
XCTAssertEqual([HTTPServerResponsePart.head(self.responseHead),
HTTPServerResponsePart.end(nil)],
self.writeRecorder.writes)
XCTAssertFalse(self.channel.isActive)
XCTAssertEqual(self.quiesceEventRecorder.quiesceCount, 0)
}
func testQuiescingAfterRequestAndResponseHeadsButBeforeAnyEndsThenRequestEndBeforeResponseEnd() throws {
// Send through a request .head.
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(self.requestHead)))
XCTAssertEqual(self.readRecorder.reads,
[.channelRead(HTTPServerRequestPart.head(self.requestHead))])
// Now send the response .head.
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.head(self.responseHead)).wait())
XCTAssertTrue(self.channel.isActive)
self.channel.pipeline.fireUserInboundEventTriggered(ChannelShouldQuiesceEvent())
XCTAssertTrue(self.channel.isActive)
// Request .end.
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.end(nil)))
XCTAssertEqual(self.readRecorder.reads,
[.channelRead(HTTPServerRequestPart.head(self.requestHead)),
.channelRead(HTTPServerRequestPart.end(nil))])
XCTAssertTrue(self.channel.isActive)
// Response .end.
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.end(nil)).wait())
XCTAssertFalse(self.channel.isActive)
XCTAssertEqual([HTTPServerResponsePart.head(self.responseHead),
HTTPServerResponsePart.end(nil)],
self.writeRecorder.writes)
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.end(nil)))
XCTAssertFalse(self.channel.isActive)
XCTAssertEqual(self.quiesceEventRecorder.quiesceCount, 0)
}
func testQuiescingAfterRequestAndResponseHeadsButBeforeAnyEndsThenRequestEndAfterResponseEnd() throws {
// Send through a request .head.
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(self.requestHead)))
XCTAssertEqual(self.readRecorder.reads,
[.channelRead(HTTPServerRequestPart.head(self.requestHead))])
// Now send the response .head.
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.head(self.responseHead)).wait())
XCTAssertTrue(self.channel.isActive)
self.channel.pipeline.fireUserInboundEventTriggered(ChannelShouldQuiesceEvent())
XCTAssertTrue(self.channel.isActive)
// Response .end.
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.end(nil)).wait())
XCTAssertTrue(self.channel.isActive)
// Request .end.
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.end(nil)))
XCTAssertEqual(self.readRecorder.reads,
[.channelRead(HTTPServerRequestPart.head(self.requestHead)),
.channelRead(HTTPServerRequestPart.end(nil))])
XCTAssertFalse(self.channel.isActive)
XCTAssertEqual([HTTPServerResponsePart.head(self.responseHead),
HTTPServerResponsePart.end(nil)],
self.writeRecorder.writes)
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.end(nil)))
XCTAssertFalse(self.channel.isActive)
XCTAssertEqual(self.quiesceEventRecorder.quiesceCount, 0)
}
func testQuiescingAfterHavingReceivedOneRequestButBeforeResponseWasSentWithMoreRequestsInTheBuffer() throws {
// Send through a full request and buffer a few more
for _ in 0..<3 {
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(self.requestHead)))
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.end(nil)))
}
// Check that only one request came through
XCTAssertEqual(self.readRecorder.reads,
[.channelRead(HTTPServerRequestPart.head(self.requestHead)),
.channelRead(HTTPServerRequestPart.end(nil))])
XCTAssertTrue(self.channel.isActive)
self.channel.pipeline.fireUserInboundEventTriggered(ChannelShouldQuiesceEvent())
XCTAssertTrue(self.channel.isActive)
// Now send a response.
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.head(self.responseHead)).wait())
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.end(nil)).wait())
XCTAssertFalse(self.channel.isActive)
var reqWithConnectionClose: HTTPResponseHead = self.responseHead
reqWithConnectionClose.headers.add(name: "connection", value: "close")
// check that only one response (with connection: close) came through
XCTAssertEqual([HTTPServerResponsePart.head(reqWithConnectionClose),
HTTPServerResponsePart.end(nil)],
self.writeRecorder.writes)
// Check that only one request came through
XCTAssertEqual(self.readRecorder.reads,
[.channelRead(HTTPServerRequestPart.head(self.requestHead)),
.channelRead(HTTPServerRequestPart.end(nil))])
XCTAssertFalse(self.channel.isActive)
XCTAssertEqual(self.quiesceEventRecorder.quiesceCount, 0)
}
func testParserErrorOnly() throws {
class VerifyOrderHandler: ChannelInboundHandler {
typealias InboundIn = HTTPServerRequestPart
typealias OutboundOut = HTTPServerResponsePart
enum State {
case errorExpected
case done
}
var state: State = .errorExpected
func errorCaught(context: ChannelHandlerContext, error: Error) {
XCTAssertEqual(HTTPParserError.headerOverflow, error as? HTTPParserError)
XCTAssertEqual(.errorExpected, self.state)
self.state = .done
}
func channelRead(context: ChannelHandlerContext, data: NIOAny) {
XCTFail("no requests expected")
}
}
let handler = VerifyOrderHandler()
XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTPServerProtocolErrorHandler()).wait())
XCTAssertNoThrow(try self.channel.pipeline.addHandler(handler).wait())
self.channel.pipeline.fireErrorCaught(HTTPParserError.headerOverflow)
XCTAssertEqual(.done, handler.state)
}
func testLegitRequestFollowedByParserErrorArrivingWhilstResponseOutstanding() throws {
func makeRequestHead(uri: String) -> HTTPRequestHead {
var requestHead = HTTPRequestHead(version: .http1_1, method: .GET, uri: uri)
requestHead.headers.add(name: "Host", value: "example.com")
return requestHead
}
class VerifyOrderHandler: ChannelInboundHandler {
typealias InboundIn = HTTPServerRequestPart
typealias OutboundOut = HTTPServerResponsePart
enum State {
case reqHeadExpected
case reqEndExpected
case errorExpected
case done
}
var state: State = .reqHeadExpected
func errorCaught(context: ChannelHandlerContext, error: Error) {
XCTAssertEqual(HTTPParserError.closedConnection, error as? HTTPParserError)
XCTAssertEqual(.errorExpected, self.state)
self.state = .done
}
func channelRead(context: ChannelHandlerContext, data: NIOAny) {
switch self.unwrapInboundIn(data) {
case .head:
// We dispatch this to the event loop so that it doesn't happen immediately but rather can be
// run from the driving test code whenever it wants by running the EmbeddedEventLoop.
context.eventLoop.execute {
context.writeAndFlush(self.wrapOutboundOut(.head(.init(version: .http1_1,
status: .ok))),
promise: nil)
}
XCTAssertEqual(.reqHeadExpected, self.state)
self.state = .reqEndExpected
case .body:
XCTFail("no body expected")
case .end:
// We dispatch this to the event loop so that it doesn't happen immediately but rather can be
// run from the driving test code whenever it wants by running the EmbeddedEventLoop.
context.eventLoop.execute {
context.writeAndFlush(self.wrapOutboundOut(.end(nil)), promise: nil)
}
XCTAssertEqual(.reqEndExpected, self.state)
self.state = .errorExpected
}
}
}
let handler = VerifyOrderHandler()
XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTPServerProtocolErrorHandler()).wait())
XCTAssertNoThrow(try self.channel.pipeline.addHandler(handler).wait())
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(makeRequestHead(uri: "/one"))))
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.end(nil)))
self.channel.pipeline.fireErrorCaught(HTTPParserError.closedConnection)
// let's now run the HTTP responses that we enqueued earlier on.
(self.channel.eventLoop as! EmbeddedEventLoop).run()
XCTAssertEqual(.done, handler.state)
}
func testRemovingWithResponseOutstandingTriggersRead() throws {
// First, call read() and check it makes it through.
XCTAssertEqual(self.readCounter.readCount, 0)
self.channel.read()
XCTAssertEqual(self.readCounter.readCount, 1)
// Send in a request.
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(self.requestHead)))
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.end(nil)))
// Call read again, twice. This should not change the number.
self.channel.read()
self.channel.read()
XCTAssertEqual(self.readCounter.readCount, 1)
// Remove the handler.
XCTAssertNoThrow(try channel.pipeline.removeHandler(self.pipelineHandler).wait())
// This should have automatically triggered a call to read(), but only one.
XCTAssertEqual(self.readCounter.readCount, 2)
// Incidentally we shouldn't have fired a quiesce event.
XCTAssertEqual(self.quiesceEventRecorder.quiesceCount, 0)
}
func testRemovingWithPartialResponseOutstandingTriggersRead() throws {
// First, call read() and check it makes it through.
XCTAssertEqual(self.readCounter.readCount, 0)
self.channel.read()
XCTAssertEqual(self.readCounter.readCount, 1)
// Send in a request.
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(self.requestHead)))
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.end(nil)))
// Call read again, twice. This should not change the number.
self.channel.read()
self.channel.read()
XCTAssertEqual(self.readCounter.readCount, 1)
// Send a partial response, which should not trigger a read.
XCTAssertNoThrow(try channel.writeAndFlush(HTTPServerResponsePart.head(self.responseHead)).wait())
XCTAssertEqual(self.readCounter.readCount, 1)
// Remove the handler.
XCTAssertNoThrow(try channel.pipeline.removeHandler(self.pipelineHandler).wait())
// This should have automatically triggered a call to read(), but only one.
XCTAssertEqual(self.readCounter.readCount, 2)
// Incidentally we shouldn't have fired a quiesce event.
XCTAssertEqual(self.quiesceEventRecorder.quiesceCount, 0)
}
func testRemovingWithBufferedRequestForwards() throws {
// Send in a request, and part of another.
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(self.requestHead)))
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.end(nil)))
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(self.requestHead)))
// Only one request should have made it through.
XCTAssertEqual(self.readRecorder.reads,
[.channelRead(HTTPServerRequestPart.head(self.requestHead)),
.channelRead(HTTPServerRequestPart.end(nil))])
// Remove the handler.
XCTAssertNoThrow(try channel.pipeline.removeHandler(self.pipelineHandler).wait())
// The extra data should have been forwarded.
XCTAssertEqual(self.readRecorder.reads,
[.channelRead(HTTPServerRequestPart.head(self.requestHead)),
.channelRead(HTTPServerRequestPart.end(nil)),
.channelRead(HTTPServerRequestPart.head(self.requestHead))])
}
func testQuiescingInAResponseThenRemovedFiresEventAndReads() throws {
// First, call read() and check it makes it through.
XCTAssertEqual(self.readCounter.readCount, 0)
self.channel.read()
XCTAssertEqual(self.readCounter.readCount, 1)
// Send through a request and part of another.
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(self.requestHead)))
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.end(nil)))
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(self.requestHead)))
// Only one request should have made it through.
XCTAssertEqual(self.readRecorder.reads,
[.channelRead(HTTPServerRequestPart.head(self.requestHead)),
.channelRead(HTTPServerRequestPart.end(nil))])
XCTAssertTrue(self.channel.isActive)
XCTAssertEqual(self.quiesceEventRecorder.quiesceCount, 0)
XCTAssertEqual(self.readCounter.readCount, 1)
// Now quiesce the channel.
self.channel.pipeline.fireUserInboundEventTriggered(ChannelShouldQuiesceEvent())
XCTAssertTrue(self.channel.isActive)
XCTAssertEqual(self.quiesceEventRecorder.quiesceCount, 0)
XCTAssertEqual(self.readCounter.readCount, 1)
// Call read again, twice. This should not lead to reads, as we're waiting for the end.
self.channel.read()
self.channel.read()
XCTAssertTrue(self.channel.isActive)
XCTAssertEqual(self.quiesceEventRecorder.quiesceCount, 0)
XCTAssertEqual(self.readCounter.readCount, 1)
// Now remove the handler.
XCTAssertNoThrow(try channel.pipeline.removeHandler(self.pipelineHandler).wait())
// Channel should be open, but the quiesce event should have fired, and read
// shouldn't have been called as we aren't expecting more data.
XCTAssertTrue(self.channel.isActive)
XCTAssertEqual(self.quiesceEventRecorder.quiesceCount, 1)
XCTAssertEqual(self.readCounter.readCount, 1)
}
func testQuiescingInAResponseThenRemovedFiresEventAndDoesntRead() throws {
// First, call read() and check it makes it through.
XCTAssertEqual(self.readCounter.readCount, 0)
self.channel.read()
XCTAssertEqual(self.readCounter.readCount, 1)
// Send through just the head.
XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(self.requestHead)))
XCTAssertEqual(self.readRecorder.reads,
[.channelRead(HTTPServerRequestPart.head(self.requestHead))])
XCTAssertTrue(self.channel.isActive)
XCTAssertEqual(self.quiesceEventRecorder.quiesceCount, 0)
XCTAssertEqual(self.readCounter.readCount, 1)
self.channel.pipeline.fireUserInboundEventTriggered(ChannelShouldQuiesceEvent())
XCTAssertTrue(self.channel.isActive)
XCTAssertEqual(self.quiesceEventRecorder.quiesceCount, 0)
XCTAssertEqual(self.readCounter.readCount, 1)
// Call read again, twice. This should pass through.
self.channel.read()
self.channel.read()
XCTAssertTrue(self.channel.isActive)
XCTAssertEqual(self.quiesceEventRecorder.quiesceCount, 0)
XCTAssertEqual(self.readCounter.readCount, 3)
// Now remove the handler.
XCTAssertNoThrow(try channel.pipeline.removeHandler(self.pipelineHandler).wait())
// Channel should be open, but the quiesce event should have fired, and read
// shouldn't be (as it passed through.
XCTAssertTrue(self.channel.isActive)
XCTAssertEqual(self.quiesceEventRecorder.quiesceCount, 1)
XCTAssertEqual(self.readCounter.readCount, 3)
}
}
|