Skip to content

Commit

Permalink
[Issue 719] Added parameters and encoding parameters to download …
Browse files Browse the repository at this point in the history
…APIs.
  • Loading branch information
cnoon committed Sep 6, 2015
1 parent e5505ba commit 4e633d6
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 4 deletions.
13 changes: 12 additions & 1 deletion Source/Alamofire.swift
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ public func upload(

- parameter method: The HTTP method.
- parameter URLString: The URL string.
- parameter parameters: The parameters. `nil` by default.
- parameter encoding: The parameter encoding. `.URL` by default.
- parameter headers: The HTTP headers. `nil` by default.
- parameter destination: The closure used to determine the destination of the downloaded file.

Expand All @@ -320,11 +322,20 @@ public func upload(
public func download(
method: Method,
_ URLString: URLStringConvertible,
parameters: [String: AnyObject]? = nil,
encoding: ParameterEncoding = .URL,
headers: [String: String]? = nil,
destination: Request.DownloadFileDestination)
-> Request
{
return Manager.sharedInstance.download(method, URLString, headers: headers, destination: destination)
return Manager.sharedInstance.download(
method,
URLString,
parameters: parameters,
encoding: encoding,
headers: headers,
destination: destination
)
}

/**
Expand Down
11 changes: 9 additions & 2 deletions Source/Download.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,15 @@ extension Manager {
// MARK: Request

/**
Creates a download request for the specified method, URL string, headers and destination.
Creates a download request for the specified method, URL string, parameters, parameter encoding, headers
and destination.

If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.

- parameter method: The HTTP method.
- parameter URLString: The URL string.
- parameter parameters: The parameters. `nil` by default.
- parameter encoding: The parameter encoding. `.URL` by default.
- parameter headers: The HTTP headers. `nil` by default.
- parameter destination: The closure used to determine the destination of the downloaded file.

Expand All @@ -76,12 +79,16 @@ extension Manager {
public func download(
method: Method,
_ URLString: URLStringConvertible,
parameters: [String: AnyObject]? = nil,
encoding: ParameterEncoding = .URL,
headers: [String: String]? = nil,
destination: Request.DownloadFileDestination)
-> Request
{
let mutableURLRequest = URLRequest(method, URLString, headers: headers)
return download(mutableURLRequest, destination: destination)
let encodedURLRequest = encoding.encode(mutableURLRequest, parameters: parameters).0

return download(encodedURLRequest, destination: destination)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Source/Manager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public class Manager {
// MARK: - Request

/**
Creates a request for the specified method, URL string, parameters, and parameter encoding.
Creates a request for the specified method, URL string, parameters, parameter encoding and headers.

- parameter method: The HTTP method.
- parameter URLString: The URL string.
Expand Down
96 changes: 96 additions & 0 deletions Tests/DownloadTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,102 @@ class DownloadResponseTestCase: BaseTestCase {
XCTFail("file manager should remove item at URL: \(fileURL)")
}
}

func testDownloadRequestWithParameters() {
// Given
let fileURL: NSURL = {
let cachesDirectory = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true).first!
let cachesURL = NSURL(fileURLWithPath: cachesDirectory, isDirectory: true)
let fileURL = cachesURL.URLByAppendingPathComponent("\(NSUUID().UUIDString).json")

return fileURL
}()

let URLString = "https://httpbin.org/get"
let parameters = ["foo": "bar"]
let destination: Request.DownloadFileDestination = { _, _ in fileURL }
let expectation = expectationWithDescription("Download request should download data to file: \(fileURL)")

var request: NSURLRequest?
var response: NSHTTPURLResponse?
var error: ErrorType?

// When
Alamofire.download(.GET, URLString, parameters: parameters, destination: destination)
.response { responseRequest, responseResponse, _, responseError in
request = responseRequest
response = responseResponse
error = responseError

expectation.fulfill()
}

waitForExpectationsWithTimeout(defaultTimeout, handler: nil)

// Then
XCTAssertNotNil(request, "request should not be nil")
XCTAssertNotNil(response, "response should not be nil")
XCTAssertNil(error, "error should be nil")

if let
data = NSData(contentsOfURL: fileURL),
JSONObject = try? NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(rawValue: 0)),
JSON = JSONObject as? [String: AnyObject],
args = JSON["args"] as? [String: String]
{
XCTAssertEqual(args["foo"], "bar", "foo parameter should equal bar")
} else {
XCTFail("args parameter in JSON should not be nil")
}
}

func testDownloadRequestWithHeaders() {
// Given
let fileURL: NSURL = {
let cachesDirectory = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true).first!
let cachesURL = NSURL(fileURLWithPath: cachesDirectory, isDirectory: true)
let fileURL = cachesURL.URLByAppendingPathComponent("\(NSUUID().UUIDString).json")

return fileURL
}()

let URLString = "https://httpbin.org/get"
let headers = ["Authorization": "123456"]
let destination: Request.DownloadFileDestination = { _, _ in fileURL }
let expectation = expectationWithDescription("Download request should download data to file: \(fileURL)")

var request: NSURLRequest?
var response: NSHTTPURLResponse?
var error: ErrorType?

// When
Alamofire.download(.GET, URLString, headers: headers, destination: destination)
.response { responseRequest, responseResponse, _, responseError in
request = responseRequest
response = responseResponse
error = responseError

expectation.fulfill()
}

waitForExpectationsWithTimeout(defaultTimeout, handler: nil)

// Then
XCTAssertNotNil(request, "request should not be nil")
XCTAssertNotNil(response, "response should not be nil")
XCTAssertNil(error, "error should be nil")

if let
data = NSData(contentsOfURL: fileURL),
JSONObject = try? NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(rawValue: 0)),
JSON = JSONObject as? [String: AnyObject],
headers = JSON["headers"] as? [String: String]
{
XCTAssertEqual(headers["Authorization"], "123456", "Authorization parameter should equal 123456")
} else {
XCTFail("headers parameter in JSON should not be nil")
}
}
}

// MARK: -
Expand Down

0 comments on commit 4e633d6

Please sign in to comment.