Skip to content

Commit

Permalink
Merge pull request Alamofire#742 from Alamofire/feature/url_encoded_i…
Browse files Browse the repository at this point in the history
…n_url_case

Added `URLEncodedInURL` case to Parameter Encoding
  • Loading branch information
cnoon committed Sep 9, 2015
2 parents 1108ae4 + 3636ab2 commit 092022f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 17 deletions.
46 changes: 31 additions & 15 deletions Source/ParameterEncoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,32 @@ public enum Method: String {
/**
Used to specify the way in which a set of parameters are applied to a URL request.

- URL: A query string to be set as or appended to any existing URL query for `GET`, `HEAD`, and `DELETE`
requests, or set as the body for requests with any other HTTP method. The `Content-Type` HTTP header
field of an encoded request with HTTP body is set to `application/x-www-form-urlencoded`. Since
there is no published specification for how to encode collection types, the convention of appending
`[]` to the key for array values (`foo[]=1&foo[]=2`), and appending the key surrounded by square
brackets for nested dictionary values (`foo[bar]=baz`).
- JSON: Uses `NSJSONSerialization` to create a JSON representation of the parameters object, which is set as
the body of the request. The `Content-Type` HTTP header field of an encoded request is set to
`application/json`.
- PropertyList: Uses `NSPropertyListSerialization` to create a plist representation of the parameters object,
according to the associated format and write options values, which is set as the body of the
request. The `Content-Type` HTTP header field of an encoded request is set to `application/x-plist`.
- Custom: Uses the associated closure value to construct a new request given an existing request and
parameters.
- `URL`: Creates a query string to be set as or appended to any existing URL query for `GET`, `HEAD`,
and `DELETE` requests, or set as the body for requests with any other HTTP method. The
`Content-Type` HTTP header field of an encoded request with HTTP body is set to
`application/x-www-form-urlencoded; charset=utf-8`. Since there is no published specification
for how to encode collection types, the convention of appending `[]` to the key for array
values (`foo[]=1&foo[]=2`), and appending the key surrounded by square brackets for nested
dictionary values (`foo[bar]=baz`).

- `URLEncodedInURL`: Creates query string to be set as or appended to any existing URL query. Uses the same
implementation as the `.URL` case, but always applies the encoded result to the URL.

- `JSON`: Uses `NSJSONSerialization` to create a JSON representation of the parameters object, which is
set as the body of the request. The `Content-Type` HTTP header field of an encoded request is
set to `application/json`.

- `PropertyList`: Uses `NSPropertyListSerialization` to create a plist representation of the parameters object,
according to the associated format and write options values, which is set as the body of the
request. The `Content-Type` HTTP header field of an encoded request is set to
`application/x-plist`.

- `Custom`: Uses the associated closure value to construct a new request given an existing request and
parameters.
*/
public enum ParameterEncoding {
case URL
case URLEncodedInURL
case JSON
case PropertyList(NSPropertyListFormat, NSPropertyListWriteOptions)
case Custom((URLRequestConvertible, [String: AnyObject]?) -> (NSMutableURLRequest, NSError?))
Expand Down Expand Up @@ -80,7 +89,7 @@ public enum ParameterEncoding {
var encodingError: NSError? = nil

switch self {
case .URL:
case .URL, .URLEncodedInURL:
func query(parameters: [String: AnyObject]) -> String {
var components: [(String, String)] = []
for key in Array(parameters.keys).sort(<) {
Expand All @@ -92,6 +101,13 @@ public enum ParameterEncoding {
}

func encodesParametersInURL(method: Method) -> Bool {
switch self {
case .URLEncodedInURL:
return true
default:
break
}

switch method {
case .GET, .HEAD, .DELETE:
return true
Expand Down
19 changes: 17 additions & 2 deletions Tests/ParameterEncodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ class URLParameterEncodingTestCase: ParameterEncodingTestCase {

// MARK: Tests - Varying HTTP Methods

func testURLParameterEncodeGETParametersInURL() {
func testThatURLParameterEncodingEncodesGETParametersInURL() {
// Given
let mutableURLRequest = self.URLRequest.URLRequest
mutableURLRequest.HTTPMethod = Method.GET.rawValue
Expand All @@ -367,7 +367,7 @@ class URLParameterEncodingTestCase: ParameterEncodingTestCase {
XCTAssertNil(URLRequest.HTTPBody, "HTTPBody should be nil")
}

func testURLParameterEncodePOSTParametersInHTTPBody() {
func testThatURLParameterEncodingEncodesPOSTParametersInHTTPBody() {
// Given
let mutableURLRequest = self.URLRequest.URLRequest
mutableURLRequest.HTTPMethod = Method.POST.rawValue
Expand All @@ -393,6 +393,21 @@ class URLParameterEncodingTestCase: ParameterEncodingTestCase {
XCTFail("decoded http body should not be nil")
}
}

func testThatURLEncodedInURLParameterEncodingEncodesPOSTParametersInURL() {
// Given
let mutableURLRequest = self.URLRequest.URLRequest
mutableURLRequest.HTTPMethod = Method.POST.rawValue
let parameters = ["foo": 1, "bar": 2]

// When
let (URLRequest, _) = ParameterEncoding.URLEncodedInURL.encode(mutableURLRequest, parameters: parameters)

// Then
XCTAssertEqual(URLRequest.URL?.query ?? "", "bar=2&foo=1", "query is incorrect")
XCTAssertNil(URLRequest.valueForHTTPHeaderField("Content-Type"), "Content-Type should be nil")
XCTAssertNil(URLRequest.HTTPBody, "HTTPBody should be nil")
}
}

// MARK: -
Expand Down

0 comments on commit 092022f

Please sign in to comment.