File tree Expand file tree Collapse file tree 2 files changed +39
-4
lines changed
Tests/AsyncHTTPClientTests Expand file tree Collapse file tree 2 files changed +39
-4
lines changed Original file line number Diff line number Diff line change @@ -27,11 +27,11 @@ extension HTTPHeaders {
27
27
}
28
28
29
29
self . remove ( name: " Transfer-Encoding " )
30
- self . remove ( name: " Content-Length " )
31
30
32
31
try self . validateFieldNames ( )
33
32
34
33
guard let body = body else {
34
+ self . remove ( name: " Content-Length " )
35
35
// if we don't have a body we might not need to send the Content-Length field
36
36
// https://tools.ietf.org/html/rfc7230#section-3.3.2
37
37
switch method {
@@ -60,11 +60,15 @@ extension HTTPHeaders {
60
60
}
61
61
62
62
if encodings. isEmpty {
63
- guard let length = body. length else {
64
- throw HTTPClientError . contentLengthMissing
63
+ if let length = body. length {
64
+ self . remove ( name: " Content-Length " )
65
+ contentLength = length
66
+ } else if !self . contains ( name: " Content-Length " ) {
67
+ transferEncoding = " chunked "
65
68
}
66
- contentLength = length
67
69
} else {
70
+ self . remove ( name: " Content-Length " )
71
+
68
72
transferEncoding = encodings. joined ( separator: " , " )
69
73
if !encodings. contains ( " chunked " ) {
70
74
guard let length = body. length else {
Original file line number Diff line number Diff line change @@ -2005,4 +2005,35 @@ class HTTPClientTests: XCTestCase {
2005
2005
2006
2006
self . defaultClient = nil // so it doesn't get shut down again.
2007
2007
}
2008
+
2009
+ func testUploadStreamingNoLength( ) throws {
2010
+ let server = NIOHTTP1TestServer ( group: self . serverGroup)
2011
+ let client = HTTPClient ( eventLoopGroupProvider: . shared( self . clientGroup) )
2012
+ defer {
2013
+ XCTAssertNoThrow ( try client. syncShutdown ( ) )
2014
+ XCTAssertNoThrow ( try server. stop ( ) )
2015
+ }
2016
+
2017
+ var request = try HTTPClient . Request ( url: " http://localhost: \( server. serverPort) / " )
2018
+ request. body = . stream( ) { writer in
2019
+ writer. write ( . byteBuffer( ByteBuffer . of ( string: " 1234 " ) ) )
2020
+ }
2021
+
2022
+ let future = client. execute ( request: request)
2023
+
2024
+ switch try server. readInbound ( ) {
2025
+ case . head( let head) :
2026
+ XCTAssertEqual ( head. headers [ " transfer-encoding " ] , [ " chunked " ] )
2027
+ default :
2028
+ XCTFail ( " Unexpected part " )
2029
+ }
2030
+
2031
+ XCTAssertNoThrow ( try server. readInbound ( ) ) // .body
2032
+ XCTAssertNoThrow ( try server. readInbound ( ) ) // .end
2033
+
2034
+ XCTAssertNoThrow ( try server. writeOutbound ( . head( . init( version: . init( major: 1 , minor: 1 ) , status: . ok) ) ) )
2035
+ XCTAssertNoThrow ( try server. writeOutbound ( . end( nil ) ) )
2036
+
2037
+ XCTAssertNoThrow ( try future. wait ( ) )
2038
+ }
2008
2039
}
You can’t perform that action at this time.
0 commit comments