Skip to content

Commit 3d07e9a

Browse files
authored
Merge pull request #1743 from mkistler/mdk/8970-post-with-empty-body
2 parents 753e621 + d00aea1 commit 3d07e9a

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

Foundation/URLSession/http/HTTPURLProtocol.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ internal class _HTTPURLProtocol: _NativeProtocol {
133133
}
134134
let customHeaders: [String]
135135
let headersForRequest = curlHeaders(for: httpHeaders)
136-
if ((request.httpMethod == "POST") && (request.value(forHTTPHeaderField: "Content-Type") == nil)) {
136+
if ((request.httpMethod == "POST") && (request.httpBody?.count ?? 0 > 0)
137+
&& (request.value(forHTTPHeaderField: "Content-Type") == nil)) {
137138
customHeaders = headersForRequest + ["Content-Type:application/x-www-form-urlencoded"]
138139
} else {
139140
customHeaders = headersForRequest

TestFoundation/HTTPServer.swift

+7
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,13 @@ public class TestURLSessionServer {
426426
return _HTTPResponse(response: .OK, headers: "Content-Length: \(text.data(using: .utf8)!.count)", body: text)
427427
}
428428

429+
if uri == "/emptyPost" {
430+
if request.body.count == 0 && request.getHeader(for: "Content-Type") == nil {
431+
return _HTTPResponse(response: .OK, body: "")
432+
}
433+
return _HTTPResponse(response: .NOTFOUND, body: "")
434+
}
435+
429436
if uri == "/requestCookies" {
430437
let text = request.getCommaSeparatedHeaders()
431438
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

+20
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class TestURLSession : LoopbackServerTest {
4343
("test_setCookies", test_setCookies),
4444
("test_dontSetCookies", test_dontSetCookies),
4545
("test_redirectionWithSetCookies", test_redirectionWithSetCookies),
46+
("test_postWithEmptyBody", test_postWithEmptyBody),
4647
]
4748
}
4849

@@ -675,6 +676,25 @@ class TestURLSession : LoopbackServerTest {
675676
XCTAssertEqual(config.urlCache, nil)
676677
XCTAssertEqual(config.shouldUseExtendedBackgroundIdleMode, true)
677678
}
679+
680+
/* Test for SR-8970 to verify that content-type header is not added to post with empty body */
681+
func test_postWithEmptyBody() {
682+
let config = URLSessionConfiguration.default
683+
config.timeoutIntervalForRequest = 5
684+
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/emptyPost"
685+
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
686+
var expect = expectation(description: "POST \(urlString): post with empty body")
687+
var req = URLRequest(url: URL(string: urlString)!)
688+
req.httpMethod = "POST"
689+
var task = session.dataTask(with: req) { (_, response, error) -> Void in
690+
defer { expect.fulfill() }
691+
XCTAssertNil(error as? URLError, "error = \(error as! URLError)")
692+
guard let httpresponse = response as? HTTPURLResponse else { fatalError() }
693+
XCTAssertEqual(200, httpresponse.statusCode, "HTTP response code is not 200")
694+
}
695+
task.resume()
696+
waitForExpectations(timeout: 30)
697+
}
678698
}
679699

680700
class SharedDelegate: NSObject {

0 commit comments

Comments
 (0)