Skip to content

Commit

Permalink
JSON and plist custom content types are now retained during parameter…
Browse files Browse the repository at this point in the history
… encoding.
  • Loading branch information
cnoon committed Feb 27, 2016
1 parent 70a1d9a commit 61120c5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
11 changes: 9 additions & 2 deletions Source/ParameterEncoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ public enum ParameterEncoding {
let options = NSJSONWritingOptions()
let data = try NSJSONSerialization.dataWithJSONObject(parameters, options: options)

mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
if mutableURLRequest.valueForHTTPHeaderField("Content-Type") == nil {
mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
}

mutableURLRequest.HTTPBody = data
} catch {
encodingError = error as NSError
Expand All @@ -154,7 +157,11 @@ public enum ParameterEncoding {
format: format,
options: options
)
mutableURLRequest.setValue("application/x-plist", forHTTPHeaderField: "Content-Type")

if mutableURLRequest.valueForHTTPHeaderField("Content-Type") == nil {
mutableURLRequest.setValue("application/x-plist", forHTTPHeaderField: "Content-Type")
}

mutableURLRequest.HTTPBody = data
} catch {
encodingError = error as NSError
Expand Down
32 changes: 32 additions & 0 deletions Tests/ParameterEncodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,22 @@ class JSONParameterEncodingTestCase: ParameterEncodingTestCase {
XCTFail("JSON should not be nil")
}
}

func testJSONParameterEncodeParametersRetainsCustomContentType() {
// Given
let mutableURLRequest = NSMutableURLRequest(URL: NSURL(string: "https://example.com/")!)
mutableURLRequest.setValue("application/custom-json-type+json", forHTTPHeaderField: "Content-Type")

let parameters = ["foo": "bar"]

// When
let (URLRequest, error) = encoding.encode(mutableURLRequest, parameters: parameters)

// Then
XCTAssertNil(error)
XCTAssertNil(URLRequest.URL?.query)
XCTAssertEqual(URLRequest.valueForHTTPHeaderField("Content-Type"), "application/custom-json-type+json")
}
}

// MARK: -
Expand Down Expand Up @@ -606,6 +622,22 @@ class PropertyListParameterEncodingTestCase: ParameterEncodingTestCase {
XCTFail("HTTPBody should not be nil")
}
}

func testPropertyListParameterEncodeParametersRetainsCustomContentType() {
// Given
let mutableURLRequest = NSMutableURLRequest(URL: NSURL(string: "https://example.com/")!)
mutableURLRequest.setValue("application/custom-plist-type+plist", forHTTPHeaderField: "Content-Type")

let parameters = ["foo": "bar"]

// When
let (URLRequest, error) = encoding.encode(mutableURLRequest, parameters: parameters)

// Then
XCTAssertNil(error)
XCTAssertNil(URLRequest.URL?.query)
XCTAssertEqual(URLRequest.valueForHTTPHeaderField("Content-Type"), "application/custom-plist-type+plist")
}
}

// MARK: -
Expand Down

0 comments on commit 61120c5

Please sign in to comment.