Browse Source

[Issue 719] Added `parameters` and `encoding` parameters to download APIs.

Christian Noon 10 years ago
parent
commit
4e633d6d9a
4 changed files with 118 additions and 4 deletions
  1. 12 1
      Source/Alamofire.swift
  2. 9 2
      Source/Download.swift
  3. 1 1
      Source/Manager.swift
  4. 96 0
      Tests/DownloadTests.swift

+ 12 - 1
Source/Alamofire.swift

@@ -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.
 
@@ -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
+    )
 }
 
 /**

+ 9 - 2
Source/Download.swift

@@ -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.
 
@@ -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)
     }
 
     /**

+ 1 - 1
Source/Manager.swift

@@ -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.

+ 96 - 0
Tests/DownloadTests.swift

@@ -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: -