Skip to content

Commit

Permalink
Merge pull request #1743 from mkistler/mdk/8970-post-with-empty-body
Browse files Browse the repository at this point in the history
  • Loading branch information
swift-ci authored Nov 9, 2018
2 parents 753e621 + d00aea1 commit 3d07e9a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Foundation/URLSession/http/HTTPURLProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ internal class _HTTPURLProtocol: _NativeProtocol {
}
let customHeaders: [String]
let headersForRequest = curlHeaders(for: httpHeaders)
if ((request.httpMethod == "POST") && (request.value(forHTTPHeaderField: "Content-Type") == nil)) {
if ((request.httpMethod == "POST") && (request.httpBody?.count ?? 0 > 0)
&& (request.value(forHTTPHeaderField: "Content-Type") == nil)) {
customHeaders = headersForRequest + ["Content-Type:application/x-www-form-urlencoded"]
} else {
customHeaders = headersForRequest
Expand Down
7 changes: 7 additions & 0 deletions TestFoundation/HTTPServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,13 @@ public class TestURLSessionServer {
return _HTTPResponse(response: .OK, headers: "Content-Length: \(text.data(using: .utf8)!.count)", body: text)
}

if uri == "/emptyPost" {
if request.body.count == 0 && request.getHeader(for: "Content-Type") == nil {
return _HTTPResponse(response: .OK, body: "")
}
return _HTTPResponse(response: .NOTFOUND, body: "")
}

if uri == "/requestCookies" {
let text = request.getCommaSeparatedHeaders()
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)
Expand Down
20 changes: 20 additions & 0 deletions TestFoundation/TestURLSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class TestURLSession : LoopbackServerTest {
("test_setCookies", test_setCookies),
("test_dontSetCookies", test_dontSetCookies),
("test_redirectionWithSetCookies", test_redirectionWithSetCookies),
("test_postWithEmptyBody", test_postWithEmptyBody),
]
}

Expand Down Expand Up @@ -675,6 +676,25 @@ class TestURLSession : LoopbackServerTest {
XCTAssertEqual(config.urlCache, nil)
XCTAssertEqual(config.shouldUseExtendedBackgroundIdleMode, true)
}

/* Test for SR-8970 to verify that content-type header is not added to post with empty body */
func test_postWithEmptyBody() {
let config = URLSessionConfiguration.default
config.timeoutIntervalForRequest = 5
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/emptyPost"
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
var expect = expectation(description: "POST \(urlString): post with empty body")
var req = URLRequest(url: URL(string: urlString)!)
req.httpMethod = "POST"
var task = session.dataTask(with: req) { (_, response, error) -> Void in
defer { expect.fulfill() }
XCTAssertNil(error as? URLError, "error = \(error as! URLError)")
guard let httpresponse = response as? HTTPURLResponse else { fatalError() }
XCTAssertEqual(200, httpresponse.statusCode, "HTTP response code is not 200")
}
task.resume()
waitForExpectations(timeout: 30)
}
}

class SharedDelegate: NSObject {
Expand Down

0 comments on commit 3d07e9a

Please sign in to comment.