Skip to content

[SR-6687] [Linux] Request body clears after modifying unrelated fields #3749

Open
@mbrandonw

Description

@mbrandonw
Previous ID SR-6687
Radar None
Original Reporter @mbrandonw
Type Bug
Environment

Swift 4, Linux

Additional Detail from JIRA
Votes 1
Component/s Foundation
Labels Bug, Linux
Assignee None
Priority Medium

md5: db509a16eb34a0d3bc2241e1ef7b6a0d

Issue Description:

This bug only affects Linux foundation. It works properly on macOS.

If you create a copy of a request that has an `httpBody` set, it will get cleared (set to `nil`) when modifying unrelated fields on the copy of the request:

var originalRequest = URLRequest(url: URL(string: "https://www.pointfree.co")!)
originalRequest.httpMethod = "post"
originalRequest.httpBody = Data("hello world".utf8)

var request = originalRequest
XCTAssertNotNil(request.httpBody) // <- Passes
request.allHTTPHeaderFields = ["test": "something"]
XCTAssertNotNil(request.httpBody) // <- Fails

request.httpMethod = request.httpMethod?.uppercased()
XCTAssertNotNil(request.httpBody) // <- Also fails

This does not seem to happen when you modify the request directly:

var request = URLRequest(url: URL(string: "https://www.pointfree.co")!)
request.httpMethod = "post"
request.httpBody = Data("hello world".utf8)

XCTAssertNotNil(request.httpBody) // <- Passes
request.allHTTPHeaderFields = ["test": "something"]
XCTAssertNotNil(request.httpBody) // <- Passes
request.httpMethod = request.httpMethod?.uppercased()
XCTAssertNotNil(request.httpBody) // <- Passes

This behavior also does not occur if you set the `httpBody` on the copy and then modify fields.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions