Skip to content

SR-8970 Fix for handling of post with empty post body #1741

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Foundation/URLSession/http/HTTPURLProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,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 @@ -468,6 +468,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 @@ -45,6 +45,7 @@ class TestURLSession : LoopbackServerTest {
("test_initURLSessionConfiguration", test_initURLSessionConfiguration),
("test_basicAuthRequest", test_basicAuthRequest),
("test_redirectionWithSetCookies", test_redirectionWithSetCookies),
("test_postWithEmptyBody", test_postWithEmptyBody),
]
}

Expand Down Expand Up @@ -687,6 +688,25 @@ class TestURLSession : LoopbackServerTest {
d.run(with: url)
waitForExpectations(timeout: 60)
}

/* 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