@@ -368,11 +368,49 @@ class HTTPClientInternalTests: XCTestCase {
368368 func didFinishRequest( task: HTTPClient . Task < Response > ) throws { }
369369 }
370370
371+ final class WriteAfterFutureSucceedsHandler : ChannelInboundHandler {
372+ typealias InboundIn = HTTPServerRequestPart
373+ typealias OutboundOut = HTTPServerResponsePart
374+
375+ let bodyFuture : EventLoopFuture < Void >
376+ let endFuture : EventLoopFuture < Void >
377+
378+ init ( bodyFuture: EventLoopFuture < Void > , endFuture: EventLoopFuture < Void > ) {
379+ self . bodyFuture = bodyFuture
380+ self . endFuture = endFuture
381+ }
382+
383+ func channelRead( context: ChannelHandlerContext , data: NIOAny ) {
384+ switch self . unwrapInboundIn ( data) {
385+ case . head:
386+ let head = HTTPResponseHead ( version: HTTPVersion ( major: 1 , minor: 1 ) , status: . ok)
387+ context. writeAndFlush ( wrapOutboundOut ( . head( head) ) , promise: nil )
388+ case . body:
389+ // ignore
390+ break
391+ case . end:
392+ self . bodyFuture. hop ( to: context. eventLoop) . whenSuccess {
393+ let buffer = context. channel. allocator. buffer ( string: " 1234 " )
394+ context. writeAndFlush ( self . wrapOutboundOut ( . body( . byteBuffer( buffer) ) ) , promise: nil )
395+ }
396+
397+ self . endFuture. hop ( to: context. eventLoop) . whenSuccess {
398+ context. writeAndFlush ( self . wrapOutboundOut ( . end( nil ) ) , promise: nil )
399+ }
400+ }
401+ }
402+ }
403+
371404 // cannot test with NIOTS as `maxMessagesPerRead` is not supported
372405 let eventLoopGroup = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
373406 let httpClient = HTTPClient ( eventLoopGroupProvider: . shared( eventLoopGroup) )
374- let promise = httpClient. eventLoopGroup. next ( ) . makePromise ( of: Channel . self)
375- let httpBin = HTTPBin ( channelPromise: promise)
407+ let delegate = BackpressureTestDelegate ( eventLoop: httpClient. eventLoopGroup. next ( ) )
408+ let httpBin = HTTPBin { _ in
409+ WriteAfterFutureSucceedsHandler (
410+ bodyFuture: delegate. optionsApplied. futureResult,
411+ endFuture: delegate. backpressurePromise. futureResult
412+ )
413+ }
376414
377415 defer {
378416 XCTAssertNoThrow ( try httpClient. syncShutdown ( requiresCleanClose: true ) )
@@ -381,27 +419,21 @@ class HTTPClientInternalTests: XCTestCase {
381419 }
382420
383421 let request = try Request ( url: " http://localhost: \( httpBin. port) /custom " )
384- let delegate = BackpressureTestDelegate ( eventLoop: httpClient. eventLoopGroup. next ( ) )
385- let future = httpClient. execute ( request: request, delegate: delegate) . futureResult
386422
387- let channel = try promise . futureResult . wait ( )
423+ let requestFuture = httpClient . execute ( request : request , delegate : delegate ) . futureResult
388424
389425 // We need to wait for channel options that limit NIO to sending only one byte at a time.
390426 try delegate. optionsApplied. futureResult. wait ( )
391427
392428 // Send 4 bytes, but only one should be received until the backpressure promise is succeeded.
393- let buffer = channel. allocator. buffer ( string: " 1234 " )
394- try channel. writeAndFlush ( HTTPServerResponsePart . body ( . byteBuffer( buffer) ) ) . wait ( )
395429
396430 // Now we wait until message is delivered to client channel pipeline
397431 try delegate. messageReceived. futureResult. wait ( )
398432 XCTAssertEqual ( delegate. reads, 1 )
399433
400434 // Succeed the backpressure promise.
401435 delegate. backpressurePromise. succeed ( ( ) )
402-
403- try channel. writeAndFlush ( HTTPServerResponsePart . end ( nil ) ) . wait ( )
404- try future. wait ( )
436+ try requestFuture. wait ( )
405437
406438 // At this point all other bytes should be delivered.
407439 XCTAssertEqual ( delegate. reads, 4 )
@@ -602,7 +634,7 @@ class HTTPClientInternalTests: XCTestCase {
602634 }
603635
604636 func testResponseConnectionCloseGet( ) throws {
605- let httpBin = HTTPBin ( ssl : false )
637+ let httpBin = HTTPBin ( . http1_1 ( ) )
606638 let httpClient = HTTPClient ( eventLoopGroupProvider: . shared( self . clientGroup) ,
607639 configuration: HTTPClient . Configuration ( certificateVerification: . none) )
608640 defer {
@@ -756,14 +788,14 @@ class HTTPClientInternalTests: XCTestCase {
756788 struct NoChannelError : Error { }
757789
758790 let client = HTTPClient ( eventLoopGroupProvider: . shared( self . clientGroup) )
759- var maybeServersAndChannels : [ ( HTTPBin , Channel ) ] ?
791+ var maybeServersAndChannels : [ ( HTTPBin < HTTPBinHandler > , Channel ) ] ?
760792 XCTAssertNoThrow ( maybeServersAndChannels = try ( 0 ..< 10 ) . map { _ in
761793 let web = HTTPBin ( )
762794 defer {
763795 XCTAssertNoThrow ( try web. shutdown ( ) )
764796 }
765797
766- let req = try ! HTTPClient . Request ( url: " http://localhost: \( web. serverChannel . localAddress! . port! ) /get " ,
798+ let req = try ! HTTPClient . Request ( url: " http://localhost: \( web. port) /get " ,
767799 method: . GET,
768800 body: nil )
769801 var maybeConnection : Connection ?
@@ -847,7 +879,7 @@ class HTTPClientInternalTests: XCTestCase {
847879 XCTAssertNoThrow ( try client. syncShutdown ( ) )
848880 }
849881
850- let req = try ! HTTPClient . Request ( url: " http://localhost: \( web. serverChannel . localAddress! . port! ) /get " ,
882+ let req = try ! HTTPClient . Request ( url: " http://localhost: \( web. port) /get " ,
851883 method: . GET,
852884 body: nil )
853885
@@ -1083,7 +1115,7 @@ class HTTPClientInternalTests: XCTestCase {
10831115 let el1 = elg. next ( )
10841116 let el2 = elg. next ( )
10851117
1086- let httpBin = HTTPBin ( refusesConnections : true )
1118+ let httpBin = HTTPBin ( . refuse )
10871119 let client = HTTPClient ( eventLoopGroupProvider: . shared( elg) )
10881120
10891121 defer {
0 commit comments