@@ -557,6 +557,101 @@ class HTTPRequestStateMachineTests: XCTestCase {
557557 XCTAssertEqual ( state. errorHappened ( HTTPParserError . invalidEOFState) , . failRequest( HTTPParserError . invalidEOFState, . close) )
558558 XCTAssertEqual ( state. channelInactive ( ) , . wait)
559559 }
560+
561+ func testFailHTTPRequestWithContentLengthBecauseOfChannelInactiveWaitingForDemand( ) {
562+ var state = HTTPRequestStateMachine ( isChannelWritable: true , ignoreUncleanSSLShutdown: false )
563+ let requestHead = HTTPRequestHead ( version: . http1_1, method: . GET, uri: " / " )
564+ let metadata = RequestFramingMetadata ( connectionClose: false , body: . none)
565+ XCTAssertEqual ( state. startRequest ( head: requestHead, metadata: metadata) , . sendRequestHead( requestHead, startBody: false ) )
566+
567+ let responseHead = HTTPResponseHead ( version: . http1_1, status: . ok, headers: [ " Content-Length " : " 50 " ] )
568+ let body = ByteBuffer ( string: " foo bar " )
569+ XCTAssertEqual ( state. channelRead ( . head( responseHead) ) , . forwardResponseHead( responseHead, pauseRequestBodyStream: false ) )
570+ XCTAssertEqual ( state. demandMoreResponseBodyParts ( ) , . wait)
571+ XCTAssertEqual ( state. channelReadComplete ( ) , . wait)
572+ XCTAssertEqual ( state. read ( ) , . read)
573+ XCTAssertEqual ( state. channelRead ( . body( body) ) , . wait)
574+ XCTAssertEqual ( state. channelReadComplete ( ) , . forwardResponseBodyParts( [ body] ) )
575+ XCTAssertEqual ( state. read ( ) , . wait)
576+
577+ XCTAssertEqual ( state. channelRead ( . body( ByteBuffer ( string: " baz lightyear " ) ) ) , . wait)
578+ XCTAssertEqual ( state. channelReadComplete ( ) , . wait)
579+ XCTAssertEqual ( state. channelInactive ( ) , . failRequest( HTTPClientError . remoteConnectionClosed, . none) )
580+ }
581+
582+ func testFailHTTPRequestWithContentLengthBecauseOfChannelInactiveWaitingForRead( ) {
583+ var state = HTTPRequestStateMachine ( isChannelWritable: true , ignoreUncleanSSLShutdown: false )
584+ let requestHead = HTTPRequestHead ( version: . http1_1, method: . GET, uri: " / " )
585+ let metadata = RequestFramingMetadata ( connectionClose: false , body: . none)
586+ XCTAssertEqual ( state. startRequest ( head: requestHead, metadata: metadata) , . sendRequestHead( requestHead, startBody: false ) )
587+
588+ let responseHead = HTTPResponseHead ( version: . http1_1, status: . ok, headers: [ " Content-Length " : " 50 " ] )
589+ let body = ByteBuffer ( string: " foo bar " )
590+ XCTAssertEqual ( state. channelRead ( . head( responseHead) ) , . forwardResponseHead( responseHead, pauseRequestBodyStream: false ) )
591+ XCTAssertEqual ( state. demandMoreResponseBodyParts ( ) , . wait)
592+ XCTAssertEqual ( state. channelReadComplete ( ) , . wait)
593+ XCTAssertEqual ( state. read ( ) , . read)
594+ XCTAssertEqual ( state. channelRead ( . body( body) ) , . wait)
595+ XCTAssertEqual ( state. channelReadComplete ( ) , . forwardResponseBodyParts( [ body] ) )
596+ XCTAssertEqual ( state. demandMoreResponseBodyParts ( ) , . wait)
597+
598+ XCTAssertEqual ( state. channelRead ( . body( ByteBuffer ( string: " baz lightyear " ) ) ) , . wait)
599+ XCTAssertEqual ( state. channelReadComplete ( ) , . wait)
600+ XCTAssertEqual ( state. channelInactive ( ) , . failRequest( HTTPClientError . remoteConnectionClosed, . none) )
601+ }
602+
603+ func testFailHTTPRequestWithContentLengthBecauseOfChannelInactiveWaitingForReadAndDemand( ) {
604+ var state = HTTPRequestStateMachine ( isChannelWritable: true , ignoreUncleanSSLShutdown: false )
605+ let requestHead = HTTPRequestHead ( version: . http1_1, method: . GET, uri: " / " )
606+ let metadata = RequestFramingMetadata ( connectionClose: false , body: . none)
607+ XCTAssertEqual ( state. startRequest ( head: requestHead, metadata: metadata) , . sendRequestHead( requestHead, startBody: false ) )
608+
609+ let responseHead = HTTPResponseHead ( version: . http1_1, status: . ok, headers: [ " Content-Length " : " 50 " ] )
610+ let body = ByteBuffer ( string: " foo bar " )
611+ XCTAssertEqual ( state. channelRead ( . head( responseHead) ) , . forwardResponseHead( responseHead, pauseRequestBodyStream: false ) )
612+ XCTAssertEqual ( state. demandMoreResponseBodyParts ( ) , . wait)
613+ XCTAssertEqual ( state. channelReadComplete ( ) , . wait)
614+ XCTAssertEqual ( state. read ( ) , . read)
615+ XCTAssertEqual ( state. channelRead ( . body( body) ) , . wait)
616+ XCTAssertEqual ( state. channelReadComplete ( ) , . forwardResponseBodyParts( [ body] ) )
617+
618+ XCTAssertEqual ( state. channelRead ( . body( ByteBuffer ( string: " baz lightyear " ) ) ) , . wait)
619+ XCTAssertEqual ( state. channelReadComplete ( ) , . wait)
620+ XCTAssertEqual ( state. channelInactive ( ) , . failRequest( HTTPClientError . remoteConnectionClosed, . none) )
621+ }
622+
623+ func testFailHTTPRequestWithContentLengthBecauseOfChannelInactiveWaitingForReadAndDemandMultipleTimes( ) {
624+ var state = HTTPRequestStateMachine ( isChannelWritable: true , ignoreUncleanSSLShutdown: false )
625+ let requestHead = HTTPRequestHead ( version: . http1_1, method: . GET, uri: " / " )
626+ let metadata = RequestFramingMetadata ( connectionClose: false , body: . none)
627+ XCTAssertEqual ( state. startRequest ( head: requestHead, metadata: metadata) , . sendRequestHead( requestHead, startBody: false ) )
628+
629+ let responseHead = HTTPResponseHead ( version: . http1_1, status: . ok, headers: [ " Content-Length " : " 50 " ] )
630+ let body = ByteBuffer ( string: " foo bar " )
631+ XCTAssertEqual ( state. channelRead ( . head( responseHead) ) , . forwardResponseHead( responseHead, pauseRequestBodyStream: false ) )
632+ XCTAssertEqual ( state. demandMoreResponseBodyParts ( ) , . wait)
633+ XCTAssertEqual ( state. channelReadComplete ( ) , . wait)
634+ XCTAssertEqual ( state. read ( ) , . read)
635+ XCTAssertEqual ( state. channelRead ( . body( body) ) , . wait)
636+ XCTAssertEqual ( state. channelReadComplete ( ) , . forwardResponseBodyParts( [ body] ) )
637+
638+ let part1 = ByteBuffer ( string: " baz lightyear " )
639+ XCTAssertEqual ( state. channelRead ( . body( part1) ) , . wait)
640+ XCTAssertEqual ( state. channelReadComplete ( ) , . wait)
641+
642+ let part2 = ByteBuffer ( string: " nearly last " )
643+ XCTAssertEqual ( state. channelRead ( . body( part2) ) , . wait)
644+ XCTAssertEqual ( state. channelReadComplete ( ) , . wait)
645+
646+ let part3 = ByteBuffer ( string: " final message " )
647+ XCTAssertEqual ( state. channelRead ( . body( part3) ) , . wait)
648+ XCTAssertEqual ( state. channelReadComplete ( ) , . wait)
649+
650+ XCTAssertEqual ( state. channelRead ( . end( nil ) ) , . succeedRequest( . close, [ part1, part2, part3] ) )
651+ XCTAssertEqual ( state. channelReadComplete ( ) , . wait)
652+
653+ XCTAssertEqual ( state. channelInactive ( ) , . wait)
654+ }
560655}
561656
562657extension HTTPRequestStateMachine . Action : Equatable {
0 commit comments