// RequestTests.swift // // Copyright (c) 2014 Alamofire (http://alamofire.org) // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. import Alamofire import Foundation import XCTest class RequestInitializationTestCase: BaseTestCase { func testRequestClassMethodWithMethodAndURL() { // Given let URLString = "http://httpbin.org/" // When let request = Alamofire.request(.GET, URLString) // Then XCTAssertNotNil(request.request, "request should not be nil") XCTAssertEqual(request.request.HTTPMethod ?? "", "GET", "request HTTP method should match expected value") XCTAssertEqual(request.request.URL!, NSURL(string: URLString)!, "request URL should be equal") XCTAssertNil(request.response, "response should be nil") } func testRequestClassMethodWithMethodAndURLAndParameters() { // Given let URLString = "http://httpbin.org/get" // When let request = Alamofire.request(.GET, URLString, parameters: ["foo": "bar"]) // Then XCTAssertNotNil(request.request, "request should not be nil") XCTAssertEqual(request.request.HTTPMethod ?? "", "GET", "request HTTP method should match expected value") XCTAssertNotEqual(request.request.URL!, NSURL(string: URLString)!, "request URL should be equal") XCTAssertEqual(request.request.URL?.query ?? "", "foo=bar", "query is incorrect") XCTAssertNil(request.response, "response should be nil") } func testRequestClassMethodWithMethodURLParametersAndHeaders() { // Given let URLString = "http://httpbin.org/get" // When let request = Alamofire.request(.GET, URLString, parameters: ["foo": "bar"], headers: ["Authorization": "123456"]) // Then XCTAssertNotNil(request.request, "request should not be nil") XCTAssertEqual(request.request.HTTPMethod ?? "", "GET", "request HTTP method should match expected value") XCTAssertNotEqual(request.request.URL!, NSURL(string: URLString)!, "request URL should be equal") XCTAssertEqual(request.request.URL?.query ?? "", "foo=bar", "query is incorrect") let authorizationHeader = request.request.valueForHTTPHeaderField("Authorization") ?? "" XCTAssertEqual(authorizationHeader, "123456", "Authorization header is incorrect") XCTAssertNil(request.response, "response should be nil") } } // MARK: - class RequestResponseTestCase: BaseTestCase { func testRequestResponse() { // Given let URLString = "http://httpbin.org/get" let serializer = Request.stringResponseSerializer(encoding: NSUTF8StringEncoding) let expectation = expectationWithDescription("GET request should succeed: \(URLString)") var request: NSURLRequest? var response: NSHTTPURLResponse? var string: String? var error: NSError? // When Alamofire.request(.GET, URLString, parameters: ["foo": "bar"]) .response(responseSerializer: serializer) { responseRequest, responseResponse, responseString, responseError in request = responseRequest response = responseResponse string = responseString error = responseError expectation.fulfill() } waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil) // Then XCTAssertNotNil(request, "request should not be nil") XCTAssertNotNil(response, "response should not be nil") XCTAssertNotNil(string, "string should not be nil") XCTAssertNil(error, "error should be nil") } func testRequestResponseWithProgress() { // Given let randomBytes = 4 * 1024 * 1024 let URLString = "http://httpbin.org/bytes/\(randomBytes)" let expectation = expectationWithDescription("Bytes download progress should be reported: \(URLString)") var byteValues: [(bytes: Int64, totalBytes: Int64, totalBytesExpected: Int64)] = [] var progressValues: [(completedUnitCount: Int64, totalUnitCount: Int64)] = [] var responseRequest: NSURLRequest? var responseResponse: NSHTTPURLResponse? var responseData: NSData? var responseError: NSError? // When let request = Alamofire.request(.GET, URLString) request.progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in let bytes = (bytes: bytesRead, totalBytes: totalBytesRead, totalBytesExpected: totalBytesExpectedToRead) byteValues.append(bytes) let progress = (completedUnitCount: request.progress.completedUnitCount, totalUnitCount: request.progress.totalUnitCount) progressValues.append(progress) } request.response { request, response, data, error in responseRequest = request responseResponse = response responseData = data responseError = error expectation.fulfill() } waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil) // Then XCTAssertNotNil(responseRequest, "response request should not be nil") XCTAssertNotNil(responseResponse, "response response should not be nil") XCTAssertNotNil(responseData, "response data should not be nil") XCTAssertNil(responseError, "response error should be nil") XCTAssertEqual(byteValues.count, progressValues.count, "byteValues count should equal progressValues count") if byteValues.count == progressValues.count { for index in 0.. [String] { return request.debugDescription.componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).filter { $0 != "" && $0 != "\\" } } }