From 1430998942ab59aaf8d6be1d45fad7254e1da6dc Mon Sep 17 00:00:00 2001 From: Mike Kistler Date: Mon, 29 Oct 2018 19:23:33 -0500 Subject: [PATCH 1/2] Test for handling of post with empty post body --- TestFoundation/HTTPServer.swift | 7 +++++++ TestFoundation/TestURLSession.swift | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/TestFoundation/HTTPServer.swift b/TestFoundation/HTTPServer.swift index 8bcc1e8554..63a6bab772 100644 --- a/TestFoundation/HTTPServer.swift +++ b/TestFoundation/HTTPServer.swift @@ -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) diff --git a/TestFoundation/TestURLSession.swift b/TestFoundation/TestURLSession.swift index 2c1c1a715c..f179ee7f05 100644 --- a/TestFoundation/TestURLSession.swift +++ b/TestFoundation/TestURLSession.swift @@ -43,6 +43,7 @@ class TestURLSession : LoopbackServerTest { ("test_setCookies", test_setCookies), ("test_dontSetCookies", test_dontSetCookies), ("test_redirectionWithSetCookies", test_redirectionWithSetCookies), + ("test_postWithEmptyBody", test_postWithEmptyBody), ] } @@ -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 { From d00aea147b28ca9da4a5f0e43ecd618d934d25f9 Mon Sep 17 00:00:00 2001 From: Mike Kistler Date: Tue, 30 Oct 2018 00:08:32 -0500 Subject: [PATCH 2/2] Fix for handling of post with empty post body --- Foundation/URLSession/http/HTTPURLProtocol.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Foundation/URLSession/http/HTTPURLProtocol.swift b/Foundation/URLSession/http/HTTPURLProtocol.swift index 7444a437cf..3e332d7f96 100644 --- a/Foundation/URLSession/http/HTTPURLProtocol.swift +++ b/Foundation/URLSession/http/HTTPURLProtocol.swift @@ -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