@@ -20,6 +20,7 @@ import Network
20
20
import Logging
21
21
import NIOConcurrencyHelpers
22
22
import NIOCore
23
+ import NIOEmbedded
23
24
import NIOFoundationCompat
24
25
import NIOHTTP1
25
26
import NIOHTTPCompression
@@ -607,6 +608,35 @@ final class HTTPClientTests: XCTestCaseHTTPClientTestsBaseClass {
607
608
}
608
609
}
609
610
611
+ func testWriteTimeout( ) throws {
612
+ let localClient = HTTPClient ( eventLoopGroupProvider: . shared( self . clientGroup) ,
613
+ configuration: HTTPClient . Configuration ( timeout: HTTPClient . Configuration. Timeout ( write: . nanoseconds( 10 ) ) ) )
614
+
615
+ defer {
616
+ XCTAssertNoThrow ( try localClient. syncShutdown ( ) )
617
+ }
618
+
619
+ // Create a request that writes a chunk, then waits longer than the configured write timeout,
620
+ // and then writes again. This should trigger a write timeout error.
621
+ let request = try HTTPClient . Request ( url: self . defaultHTTPBinURLPrefix + " post " ,
622
+ method: . POST,
623
+ headers: [ " transfer-encoding " : " chunked " ] ,
624
+ body: . stream { streamWriter in
625
+ _ = streamWriter. write ( . byteBuffer( . init( ) ) )
626
+
627
+ let promise = self . clientGroup. next ( ) . makePromise ( of: Void . self)
628
+ self . clientGroup. next ( ) . scheduleTask ( in: . milliseconds( 3 ) ) {
629
+ streamWriter. write ( . byteBuffer( . init( ) ) ) . cascade ( to: promise)
630
+ }
631
+
632
+ return promise. futureResult
633
+ } )
634
+
635
+ XCTAssertThrowsError ( try localClient. execute ( request: request) . wait ( ) ) {
636
+ XCTAssertEqual ( $0 as? HTTPClientError , . writeTimeout)
637
+ }
638
+ }
639
+
610
640
func testConnectTimeout( ) throws {
611
641
#if os(Linux)
612
642
// 198.51.100.254 is reserved for documentation only and therefore should not accept any TCP connection
@@ -1230,8 +1260,8 @@ final class HTTPClientTests: XCTestCaseHTTPClientTestsBaseClass {
1230
1260
/// openssl req -x509 -newkey rsa:4096 -keyout self_signed_key.pem -out self_signed_cert.pem -sha256 -days 99999 -nodes -subj '/CN=localhost'
1231
1261
let certPath = Bundle . module. path ( forResource: " self_signed_cert " , ofType: " pem " ) !
1232
1262
let keyPath = Bundle . module. path ( forResource: " self_signed_key " , ofType: " pem " ) !
1233
- let configuration = TLSConfiguration . makeServerConfiguration (
1234
- certificateChain: try NIOSSLCertificate . fromPEMFile ( certPath) . map { . certificate( $0) } ,
1263
+ let configuration = try TLSConfiguration . makeServerConfiguration (
1264
+ certificateChain: NIOSSLCertificate . fromPEMFile ( certPath) . map { . certificate( $0) } ,
1235
1265
privateKey: . file( keyPath)
1236
1266
)
1237
1267
let sslContext = try NIOSSLContext ( configuration: configuration)
@@ -1270,8 +1300,8 @@ final class HTTPClientTests: XCTestCaseHTTPClientTestsBaseClass {
1270
1300
/// openssl req -x509 -newkey rsa:4096 -keyout self_signed_key.pem -out self_signed_cert.pem -sha256 -days 99999 -nodes -subj '/CN=localhost'
1271
1301
let certPath = Bundle . module. path ( forResource: " self_signed_cert " , ofType: " pem " ) !
1272
1302
let keyPath = Bundle . module. path ( forResource: " self_signed_key " , ofType: " pem " ) !
1273
- let configuration = TLSConfiguration . makeServerConfiguration (
1274
- certificateChain: try NIOSSLCertificate . fromPEMFile ( certPath) . map { . certificate( $0) } ,
1303
+ let configuration = try TLSConfiguration . makeServerConfiguration (
1304
+ certificateChain: NIOSSLCertificate . fromPEMFile ( certPath) . map { . certificate( $0) } ,
1275
1305
privateKey: . file( keyPath)
1276
1306
)
1277
1307
let sslContext = try NIOSSLContext ( configuration: configuration)
@@ -2728,7 +2758,7 @@ final class HTTPClientTests: XCTestCaseHTTPClientTestsBaseClass {
2728
2758
2729
2759
func uploader( _ streamWriter: HTTPClient . Body . StreamWriter ) -> EventLoopFuture < Void > {
2730
2760
let done = streamWriter. write ( . byteBuffer( ByteBuffer ( string: " X " ) ) )
2731
- done. recover { error -> Void in
2761
+ done. recover { error in
2732
2762
XCTFail ( " unexpected error \( error) " )
2733
2763
} . whenSuccess {
2734
2764
// This is executed when we have already sent the end of the request.
0 commit comments