77//
88
99import XCTest
10+ import Dispatch
1011
1112@testable import HTTP
1213
@@ -481,6 +482,45 @@ class ServerTests: XCTestCase {
481482 }
482483
483484
485+ func testExplicitCloseConnections( ) {
486+ let expectation = self . expectation ( description: " 0 Open Connection " )
487+ let server = HTTPServer ( )
488+ do {
489+ try server. start ( port: 0 , handler: OkHandler ( ) . handle)
490+
491+ let session = URLSession ( configuration: URLSessionConfiguration . default)
492+ let url1 = URL ( string: " http://localhost: \( server. port) " ) !
493+ var request = URLRequest ( url: url1)
494+ request. httpMethod = " POST "
495+ request. setValue ( " close " , forHTTPHeaderField: " Connection " )
496+
497+ let dataTask1 = session. dataTask ( with: request) { ( responseBody, rawResponse, error) in
498+ XCTAssertNil ( error, " \( error!. localizedDescription) " )
499+ #if os(Linux)
500+ XCTAssertEqual ( server. connectionCount, 0 )
501+ expectation. fulfill ( )
502+
503+ // Darwin's URLSession replaces the `Connection: close` header with `Connection: keep-alive`, so allow it to expire
504+ #else
505+ DispatchQueue . main. asyncAfter ( deadline: . now( ) + 10 ) {
506+ XCTAssertEqual ( server. connectionCount, 0 )
507+ expectation. fulfill ( )
508+ }
509+ #endif
510+ }
511+ dataTask1. resume ( )
512+
513+ self . waitForExpectations ( timeout: 30 ) { ( error) in
514+ if let error = error {
515+ XCTFail ( " \( error) " )
516+ }
517+ }
518+ server. stop ( )
519+ } catch {
520+ XCTFail ( " Error listening on port \( 0 ) : \( error) . Use server.failed(callback:) to handle " )
521+ }
522+ }
523+
484524 static var allTests = [
485525 ( " testEcho " , testEcho) ,
486526 ( " testHello " , testHello) ,
@@ -492,6 +532,7 @@ class ServerTests: XCTestCase {
492532 ( " testRequestEchoEndToEnd " , testRequestEchoEndToEnd) ,
493533 ( " testRequestKeepAliveEchoEndToEnd " , testRequestKeepAliveEchoEndToEnd) ,
494534 ( " testRequestLargeEchoEndToEnd " , testRequestLargeEchoEndToEnd) ,
535+ ( " testExplicitCloseConnections " , testExplicitCloseConnections) ,
495536 ( " testRequestLargePostHelloWorld " , testRequestLargePostHelloWorld) ,
496537 ]
497538}
0 commit comments