Skip to content

Commit 5dcd88c

Browse files
Mike Kistlerianpartridge
authored andcommitted
SR-8970 Fix for handling of post with empty post body (swiftlang#1741)
* Test for handling of post with empty post body * Fix for handling of post with empty post body
1 parent 598f805 commit 5dcd88c

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

Foundation/URLSession/http/HTTPURLProtocol.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ internal class _HTTPURLProtocol: _NativeProtocol {
139139
}
140140
let customHeaders: [String]
141141
let headersForRequest = curlHeaders(for: httpHeaders)
142-
if ((request.httpMethod == "POST") && (request.value(forHTTPHeaderField: "Content-Type") == nil)) {
142+
if ((request.httpMethod == "POST") && (request.httpBody?.count ?? 0 > 0)
143+
&& (request.value(forHTTPHeaderField: "Content-Type") == nil)) {
143144
customHeaders = headersForRequest + ["Content-Type:application/x-www-form-urlencoded"]
144145
} else {
145146
customHeaders = headersForRequest

TestFoundation/HTTPServer.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,13 @@ public class TestURLSessionServer {
468468
return _HTTPResponse(response: .OK, headers: "Content-Length: \(text.data(using: .utf8)!.count)", body: text)
469469
}
470470

471+
if uri == "/emptyPost" {
472+
if request.body.count == 0 && request.getHeader(for: "Content-Type") == nil {
473+
return _HTTPResponse(response: .OK, body: "")
474+
}
475+
return _HTTPResponse(response: .NOTFOUND, body: "")
476+
}
477+
471478
if uri == "/requestCookies" {
472479
let text = request.getCommaSeparatedHeaders()
473480
return _HTTPResponse(response: .OK, headers: "Content-Length: \(text.data(using: .utf8)!.count)\r\nSet-Cookie: fr=anjd&232; Max-Age=7776000; path=/\r\nSet-Cookie: nm=sddf&232; Max-Age=7776000; path=/; domain=.swift.org; secure; httponly\r\n", body: text)

TestFoundation/TestURLSession.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class TestURLSession : LoopbackServerTest {
4545
("test_initURLSessionConfiguration", test_initURLSessionConfiguration),
4646
("test_basicAuthRequest", test_basicAuthRequest),
4747
("test_redirectionWithSetCookies", test_redirectionWithSetCookies),
48+
("test_postWithEmptyBody", test_postWithEmptyBody),
4849
]
4950
}
5051

@@ -687,6 +688,25 @@ class TestURLSession : LoopbackServerTest {
687688
d.run(with: url)
688689
waitForExpectations(timeout: 60)
689690
}
691+
692+
/* Test for SR-8970 to verify that content-type header is not added to post with empty body */
693+
func test_postWithEmptyBody() {
694+
let config = URLSessionConfiguration.default
695+
config.timeoutIntervalForRequest = 5
696+
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/emptyPost"
697+
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
698+
var expect = expectation(description: "POST \(urlString): post with empty body")
699+
var req = URLRequest(url: URL(string: urlString)!)
700+
req.httpMethod = "POST"
701+
var task = session.dataTask(with: req) { (_, response, error) -> Void in
702+
defer { expect.fulfill() }
703+
XCTAssertNil(error as? URLError, "error = \(error as! URLError)")
704+
guard let httpresponse = response as? HTTPURLResponse else { fatalError() }
705+
XCTAssertEqual(200, httpresponse.statusCode, "HTTP response code is not 200")
706+
}
707+
task.resume()
708+
waitForExpectations(timeout: 30)
709+
}
690710
}
691711

692712
class SharedDelegate: NSObject {

0 commit comments

Comments
 (0)