| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105 |
- //
- // RequestTests.swift
- //
- // Copyright (c) 2014-2018 Alamofire Software Foundation (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
- final class RequestResponseTestCase: BaseTestCase {
- func testRequestResponse() {
- // Given
- let urlString = "https://httpbin.org/get"
- let expectation = self.expectation(description: "GET request should succeed: \(urlString)")
- var response: DataResponse<Data?, AFError>?
- // When
- AF.request(urlString, parameters: ["foo": "bar"])
- .response { resp in
- response = resp
- expectation.fulfill()
- }
- waitForExpectations(timeout: timeout, handler: nil)
- // Then
- XCTAssertNotNil(response?.request)
- XCTAssertNotNil(response?.response)
- XCTAssertNotNil(response?.data)
- XCTAssertNil(response?.error)
- }
- func testRequestResponseWithProgress() {
- // Given
- let randomBytes = 1 * 1024 * 1024
- let urlString = "https://httpbin.org/bytes/\(randomBytes)"
- let expectation = self.expectation(description: "Bytes download progress should be reported: \(urlString)")
- var progressValues: [Double] = []
- var response: DataResponse<Data?, AFError>?
- // When
- AF.request(urlString)
- .downloadProgress { progress in
- progressValues.append(progress.fractionCompleted)
- }
- .response { resp in
- response = resp
- expectation.fulfill()
- }
- waitForExpectations(timeout: timeout, handler: nil)
- // Then
- XCTAssertNotNil(response?.request)
- XCTAssertNotNil(response?.response)
- XCTAssertNotNil(response?.data)
- XCTAssertNil(response?.error)
- var previousProgress: Double = progressValues.first ?? 0.0
- for progress in progressValues {
- XCTAssertGreaterThanOrEqual(progress, previousProgress)
- previousProgress = progress
- }
- if let lastProgressValue = progressValues.last {
- XCTAssertEqual(lastProgressValue, 1.0)
- } else {
- XCTFail("last item in progressValues should not be nil")
- }
- }
- func testPOSTRequestWithUnicodeParameters() {
- // Given
- let urlString = "https://httpbin.org/post"
- let parameters = ["french": "français",
- "japanese": "日本語",
- "arabic": "العربية",
- "emoji": "😃"]
- let expectation = self.expectation(description: "request should succeed")
- var response: DataResponse<Any, AFError>?
- // When
- AF.request(urlString, method: .post, parameters: parameters)
- .responseJSON { closureResponse in
- response = closureResponse
- expectation.fulfill()
- }
- waitForExpectations(timeout: timeout, handler: nil)
- // Then
- XCTAssertNotNil(response?.request)
- XCTAssertNotNil(response?.response)
- XCTAssertNotNil(response?.data)
- if let json = response?.result.success as? [String: Any], let form = json["form"] as? [String: String] {
- XCTAssertEqual(form["french"], parameters["french"])
- XCTAssertEqual(form["japanese"], parameters["japanese"])
- XCTAssertEqual(form["arabic"], parameters["arabic"])
- XCTAssertEqual(form["emoji"], parameters["emoji"])
- } else {
- XCTFail("form parameter in JSON should not be nil")
- }
- }
- func testPOSTRequestWithBase64EncodedImages() {
- // Given
- let urlString = "https://httpbin.org/post"
- let pngBase64EncodedString: String = {
- let URL = url(forResource: "unicorn", withExtension: "png")
- let data = try! Data(contentsOf: URL)
- return data.base64EncodedString(options: .lineLength64Characters)
- }()
- let jpegBase64EncodedString: String = {
- let URL = url(forResource: "rainbow", withExtension: "jpg")
- let data = try! Data(contentsOf: URL)
- return data.base64EncodedString(options: .lineLength64Characters)
- }()
- let parameters = ["email": "user@alamofire.org",
- "png_image": pngBase64EncodedString,
- "jpeg_image": jpegBase64EncodedString]
- let expectation = self.expectation(description: "request should succeed")
- var response: DataResponse<Any, AFError>?
- // When
- AF.request(urlString, method: .post, parameters: parameters)
- .responseJSON { closureResponse in
- response = closureResponse
- expectation.fulfill()
- }
- waitForExpectations(timeout: timeout, handler: nil)
- // Then
- XCTAssertNotNil(response?.request)
- XCTAssertNotNil(response?.response)
- XCTAssertNotNil(response?.data)
- XCTAssertEqual(response?.result.isSuccess, true)
- if let json = response?.result.success as? [String: Any], let form = json["form"] as? [String: String] {
- XCTAssertEqual(form["email"], parameters["email"])
- XCTAssertEqual(form["png_image"], parameters["png_image"])
- XCTAssertEqual(form["jpeg_image"], parameters["jpeg_image"])
- } else {
- XCTFail("form parameter in JSON should not be nil")
- }
- }
- // MARK: Queues
- func testThatResponseSerializationWorksWithSerializationQueue() {
- // Given
- let queue = DispatchQueue(label: "org.alamofire.testSerializationQueue")
- let manager = Session(serializationQueue: queue)
- let expectation = self.expectation(description: "request should complete")
- var response: DataResponse<Any, AFError>?
- // When
- manager.request("https://httpbin.org/get").responseJSON { resp in
- response = resp
- expectation.fulfill()
- }
- waitForExpectations(timeout: timeout, handler: nil)
- // Then
- XCTAssertEqual(response?.result.isSuccess, true)
- }
- func testThatRequestsWorksWithRequestAndSerializationQueue() {
- // Given
- let requestQueue = DispatchQueue(label: "org.alamofire.testRequestQueue")
- let serializationQueue = DispatchQueue(label: "org.alamofire.testSerializationQueue")
- let manager = Session(requestQueue: requestQueue, serializationQueue: serializationQueue)
- let expectation = self.expectation(description: "request should complete")
- var response: DataResponse<Any, AFError>?
- // When
- manager.request("https://httpbin.org/get").responseJSON { resp in
- response = resp
- expectation.fulfill()
- }
- waitForExpectations(timeout: timeout, handler: nil)
- // Then
- XCTAssertEqual(response?.result.isSuccess, true)
- }
- // MARK: Encodable Parameters
- func testThatRequestsCanPassEncodableParametersAsJSONBodyData() {
- // Given
- let parameters = HTTPBinParameters(property: "one")
- let expect = expectation(description: "request should complete")
- var receivedResponse: DataResponse<HTTPBinResponse, AFError>?
- // When
- AF.request("https://httpbin.org/post", method: .post, parameters: parameters, encoder: JSONParameterEncoder.default)
- .responseDecodable(of: HTTPBinResponse.self) { response in
- receivedResponse = response
- expect.fulfill()
- }
- waitForExpectations(timeout: timeout, handler: nil)
- // Then
- XCTAssertEqual(receivedResponse?.result.success?.data, "{\"property\":\"one\"}")
- }
- func testThatRequestsCanPassEncodableParametersAsAURLQuery() {
- // Given
- let parameters = HTTPBinParameters(property: "one")
- let expect = expectation(description: "request should complete")
- var receivedResponse: DataResponse<HTTPBinResponse, AFError>?
- // When
- AF.request("https://httpbin.org/get", method: .get, parameters: parameters)
- .responseDecodable(of: HTTPBinResponse.self) { response in
- receivedResponse = response
- expect.fulfill()
- }
- waitForExpectations(timeout: timeout, handler: nil)
- // Then
- XCTAssertEqual(receivedResponse?.result.success?.args, ["property": "one"])
- }
- func testThatRequestsCanPassEncodableParametersAsURLEncodedBodyData() {
- // Given
- let parameters = HTTPBinParameters(property: "one")
- let expect = expectation(description: "request should complete")
- var receivedResponse: DataResponse<HTTPBinResponse, AFError>?
- // When
- AF.request("https://httpbin.org/post", method: .post, parameters: parameters)
- .responseDecodable(of: HTTPBinResponse.self) { response in
- receivedResponse = response
- expect.fulfill()
- }
- waitForExpectations(timeout: timeout, handler: nil)
- // Then
- XCTAssertEqual(receivedResponse?.result.success?.form, ["property": "one"])
- }
- // MARK: Lifetime Events
- func testThatAutomaticallyResumedRequestReceivesAppropriateLifetimeEvents() {
- // Given
- let eventMonitor = ClosureEventMonitor()
- let session = Session(eventMonitors: [eventMonitor])
- let expect = expectation(description: "request should receive appropriate lifetime events")
- expect.expectedFulfillmentCount = 4
- eventMonitor.requestDidResumeTask = { _, _ in expect.fulfill() }
- eventMonitor.requestDidResume = { _ in expect.fulfill() }
- eventMonitor.requestDidFinish = { _ in expect.fulfill() }
- // Fulfill other events that would exceed the expected count. Inverted expectations require the full timeout.
- eventMonitor.requestDidSuspend = { _ in expect.fulfill() }
- eventMonitor.requestDidSuspendTask = { _, _ in expect.fulfill() }
- eventMonitor.requestDidCancel = { _ in expect.fulfill() }
- eventMonitor.requestDidCancelTask = { _, _ in expect.fulfill() }
- // When
- let request = session.request(URLRequest.makeHTTPBinRequest()).response { _ in expect.fulfill() }
- waitForExpectations(timeout: timeout, handler: nil)
- // Then
- XCTAssertEqual(request.state, .finished)
- }
- func testThatAutomaticallyAndManuallyResumedRequestReceivesAppropriateLifetimeEvents() {
- // Given
- let eventMonitor = ClosureEventMonitor()
- let session = Session(eventMonitors: [eventMonitor])
- let expect = expectation(description: "request should receive appropriate lifetime events")
- expect.expectedFulfillmentCount = 3
- eventMonitor.requestDidResumeTask = { _, _ in expect.fulfill() }
- eventMonitor.requestDidResume = { _ in expect.fulfill() }
- eventMonitor.requestDidFinish = { _ in expect.fulfill() }
- // Fulfill other events that would exceed the expected count. Inverted expectations require the full timeout.
- eventMonitor.requestDidSuspend = { _ in expect.fulfill() }
- eventMonitor.requestDidSuspendTask = { _, _ in expect.fulfill() }
- eventMonitor.requestDidCancel = { _ in expect.fulfill() }
- eventMonitor.requestDidCancelTask = { _, _ in expect.fulfill() }
- // When
- let request = session.request(URLRequest.makeHTTPBinRequest())
- for _ in 0..<100 {
- request.resume()
- }
- waitForExpectations(timeout: timeout, handler: nil)
- // Then
- XCTAssertEqual(request.state, .finished)
- }
- func testThatManuallyResumedRequestReceivesAppropriateLifetimeEvents() {
- // Given
- let eventMonitor = ClosureEventMonitor()
- let session = Session(startRequestsImmediately: false, eventMonitors: [eventMonitor])
- let expect = expectation(description: "request should receive appropriate lifetime events")
- expect.expectedFulfillmentCount = 3
- eventMonitor.requestDidResumeTask = { _, _ in expect.fulfill() }
- eventMonitor.requestDidResume = { _ in expect.fulfill() }
- eventMonitor.requestDidFinish = { _ in expect.fulfill() }
- // Fulfill other events that would exceed the expected count. Inverted expectations require the full timeout.
- eventMonitor.requestDidSuspend = { _ in expect.fulfill() }
- eventMonitor.requestDidSuspendTask = { _, _ in expect.fulfill() }
- eventMonitor.requestDidCancel = { _ in expect.fulfill() }
- eventMonitor.requestDidCancelTask = { _, _ in expect.fulfill() }
- // When
- let request = session.request(URLRequest.makeHTTPBinRequest())
- for _ in 0..<100 {
- request.resume()
- }
- waitForExpectations(timeout: timeout, handler: nil)
- // Then
- XCTAssertEqual(request.state, .finished)
- }
- func testThatRequestManuallyResumedManyTimesOnlyReceivesAppropriateLifetimeEvents() {
- // Given
- let eventMonitor = ClosureEventMonitor()
- let session = Session(startRequestsImmediately: false, eventMonitors: [eventMonitor])
- let expect = expectation(description: "request should receive appropriate lifetime events")
- expect.expectedFulfillmentCount = 3
- eventMonitor.requestDidResumeTask = { _, _ in expect.fulfill() }
- eventMonitor.requestDidResume = { _ in expect.fulfill() }
- eventMonitor.requestDidFinish = { _ in expect.fulfill() }
- // Fulfill other events that would exceed the expected count. Inverted expectations require the full timeout.
- eventMonitor.requestDidSuspend = { _ in expect.fulfill() }
- eventMonitor.requestDidSuspendTask = { _, _ in expect.fulfill() }
- eventMonitor.requestDidCancel = { _ in expect.fulfill() }
- eventMonitor.requestDidCancelTask = { _, _ in expect.fulfill() }
- // When
- let request = session.request(URLRequest.makeHTTPBinRequest())
- for _ in 0..<100 {
- request.resume()
- }
- waitForExpectations(timeout: timeout, handler: nil)
- // Then
- XCTAssertEqual(request.state, .finished)
- }
- func testThatRequestManuallySuspendedManyTimesAfterAutomaticResumeOnlyReceivesAppropriateLifetimeEvents() {
- // Given
- let eventMonitor = ClosureEventMonitor()
- let session = Session(startRequestsImmediately: false, eventMonitors: [eventMonitor])
- let expect = expectation(description: "request should receive appropriate lifetime events")
- expect.expectedFulfillmentCount = 2
- eventMonitor.requestDidSuspendTask = { _, _ in expect.fulfill() }
- eventMonitor.requestDidSuspend = { _ in expect.fulfill() }
- // Fulfill other events that would exceed the expected count. Inverted expectations require the full timeout.
- eventMonitor.requestDidCancel = { _ in expect.fulfill() }
- eventMonitor.requestDidCancelTask = { _, _ in expect.fulfill() }
- // When
- let request = session.request(URLRequest.makeHTTPBinRequest())
- for _ in 0..<100 {
- request.suspend()
- }
- waitForExpectations(timeout: timeout, handler: nil)
- // Then
- XCTAssertEqual(request.state, .suspended)
- }
- func testThatRequestManuallySuspendedManyTimesOnlyReceivesAppropriateLifetimeEvents() {
- // Given
- let eventMonitor = ClosureEventMonitor()
- let session = Session(startRequestsImmediately: false, eventMonitors: [eventMonitor])
- let expect = expectation(description: "request should receive appropriate lifetime events")
- expect.expectedFulfillmentCount = 2
- eventMonitor.requestDidSuspendTask = { _, _ in expect.fulfill() }
- eventMonitor.requestDidSuspend = { _ in expect.fulfill() }
- // Fulfill other events that would exceed the expected count. Inverted expectations require the full timeout.
- eventMonitor.requestDidResume = { _ in expect.fulfill() }
- eventMonitor.requestDidResumeTask = { _, _ in expect.fulfill() }
- eventMonitor.requestDidCancel = { _ in expect.fulfill() }
- eventMonitor.requestDidCancelTask = { _, _ in expect.fulfill() }
- // When
- let request = session.request(URLRequest.makeHTTPBinRequest())
- for _ in 0..<100 {
- request.suspend()
- }
- waitForExpectations(timeout: timeout, handler: nil)
- // Then
- XCTAssertEqual(request.state, .suspended)
- }
- func testThatRequestManuallyCancelledManyTimesAfterAutomaticResumeOnlyReceivesAppropriateLifetimeEvents() {
- // Given
- let eventMonitor = ClosureEventMonitor()
- let session = Session(eventMonitors: [eventMonitor])
- let expect = expectation(description: "request should receive appropriate lifetime events")
- expect.expectedFulfillmentCount = 2
- eventMonitor.requestDidCancelTask = { _, _ in expect.fulfill() }
- eventMonitor.requestDidCancel = { _ in expect.fulfill() }
- // Fulfill other events that would exceed the expected count. Inverted expectations require the full timeout.
- eventMonitor.requestDidSuspend = { _ in expect.fulfill() }
- eventMonitor.requestDidSuspendTask = { _, _ in expect.fulfill() }
- // When
- let request = session.request(URLRequest.makeHTTPBinRequest())
- // Cancellation stops task creation, so don't cancel the request until the task has been created.
- eventMonitor.requestDidCreateTask = { _, _ in
- for _ in 0..<100 {
- request.cancel()
- }
- }
- waitForExpectations(timeout: timeout, handler: nil)
- // Then
- XCTAssertEqual(request.state, .cancelled)
- }
- func testThatRequestManuallyCancelledManyTimesOnlyReceivesAppropriateLifetimeEvents() {
- // Given
- let eventMonitor = ClosureEventMonitor()
- let session = Session(startRequestsImmediately: false, eventMonitors: [eventMonitor])
- let expect = expectation(description: "request should receive appropriate lifetime events")
- expect.expectedFulfillmentCount = 2
- eventMonitor.requestDidCancelTask = { _, _ in expect.fulfill() }
- eventMonitor.requestDidCancel = { _ in expect.fulfill() }
- // Fulfill other events that would exceed the expected count. Inverted expectations require the full timeout.
- eventMonitor.requestDidResume = { _ in expect.fulfill() }
- eventMonitor.requestDidResumeTask = { _, _ in expect.fulfill() }
- eventMonitor.requestDidSuspend = { _ in expect.fulfill() }
- eventMonitor.requestDidSuspendTask = { _, _ in expect.fulfill() }
- // When
- let request = session.request(URLRequest.makeHTTPBinRequest())
- // Cancellation stops task creation, so don't cancel the request until the task has been created.
- eventMonitor.requestDidCreateTask = { _, _ in
- for _ in 0..<100 {
- request.cancel()
- }
- }
- waitForExpectations(timeout: timeout, handler: nil)
- // Then
- XCTAssertEqual(request.state, .cancelled)
- }
- func testThatRequestManuallyCancelledManyTimesOnManyQueuesOnlyReceivesAppropriateLifetimeEvents() {
- // Given
- let eventMonitor = ClosureEventMonitor()
- let session = Session(eventMonitors: [eventMonitor])
- let expect = expectation(description: "request should receive appropriate lifetime events")
- expect.expectedFulfillmentCount = 6
- eventMonitor.requestDidCancelTask = { _, _ in expect.fulfill() }
- eventMonitor.requestDidCancel = { _ in expect.fulfill() }
- eventMonitor.requestDidResume = { _ in expect.fulfill() }
- eventMonitor.requestDidResumeTask = { _, _ in expect.fulfill() }
- // Fulfill other events that would exceed the expected count. Inverted expectations require the full timeout.
- eventMonitor.requestDidSuspend = { _ in expect.fulfill() }
- eventMonitor.requestDidSuspendTask = { _, _ in expect.fulfill() }
- // When
- let request = session.request(URLRequest.makeHTTPBinRequest()).response { _ in expect.fulfill() }
- // Cancellation stops task creation, so don't cancel the request until the task has been created.
- eventMonitor.requestDidCreateTask = { _, _ in
- DispatchQueue.concurrentPerform(iterations: 100) { i in
- request.cancel()
- if i == 99 { expect.fulfill() }
- }
- }
- waitForExpectations(timeout: timeout, handler: nil)
- // Then
- XCTAssertEqual(request.state, .cancelled)
- }
- func testThatRequestTriggersAllAppropriateLifetimeEvents() {
- // Given
- let eventMonitor = ClosureEventMonitor()
- let session = Session(eventMonitors: [eventMonitor])
- let didReceiveChallenge = expectation(description: "didReceiveChallenge should fire")
- let taskDidFinishCollecting = expectation(description: "taskDidFinishCollecting should fire")
- let didReceiveData = expectation(description: "didReceiveData should fire")
- let willCacheResponse = expectation(description: "willCacheResponse should fire")
- let didCreateURLRequest = expectation(description: "didCreateInitialURLRequest should fire")
- let didCreateTask = expectation(description: "didCreateTask should fire")
- let didGatherMetrics = expectation(description: "didGatherMetrics should fire")
- let didComplete = expectation(description: "didComplete should fire")
- let didFinish = expectation(description: "didFinish should fire")
- let didResume = expectation(description: "didResume should fire")
- let didResumeTask = expectation(description: "didResumeTask should fire")
- let didParseResponse = expectation(description: "didParseResponse should fire")
- let responseHandler = expectation(description: "responseHandler should fire")
- var dataReceived = false
- eventMonitor.taskDidReceiveChallenge = { _, _, _ in didReceiveChallenge.fulfill() }
- eventMonitor.taskDidFinishCollectingMetrics = { _, _, _ in taskDidFinishCollecting.fulfill() }
- eventMonitor.dataTaskDidReceiveData = { _, _, _ in
- guard !dataReceived else { return }
- // Data may be received many times, fulfill only once.
- dataReceived = true
- didReceiveData.fulfill()
- }
- eventMonitor.dataTaskWillCacheResponse = { _, _, _ in willCacheResponse.fulfill() }
- eventMonitor.requestDidCreateInitialURLRequest = { _, _ in didCreateURLRequest.fulfill() }
- eventMonitor.requestDidCreateTask = { _, _ in didCreateTask.fulfill() }
- eventMonitor.requestDidGatherMetrics = { _, _ in didGatherMetrics.fulfill() }
- eventMonitor.requestDidCompleteTaskWithError = { _, _, _ in didComplete.fulfill() }
- eventMonitor.requestDidFinish = { _ in didFinish.fulfill() }
- eventMonitor.requestDidResume = { _ in didResume.fulfill() }
- eventMonitor.requestDidResumeTask = { _, _ in didResumeTask.fulfill() }
- eventMonitor.requestDidParseResponse = { _, _ in didParseResponse.fulfill() }
- // When
- let request = session.request(URLRequest.makeHTTPBinRequest()).response { _ in
- responseHandler.fulfill()
- }
- waitForExpectations(timeout: timeout, handler: nil)
- // Then
- XCTAssertEqual(request.state, .finished)
- }
- func testThatCancelledRequestTriggersAllAppropriateLifetimeEvents() {
- // Given
- let eventMonitor = ClosureEventMonitor()
- let session = Session(startRequestsImmediately: false, eventMonitors: [eventMonitor])
- let taskDidFinishCollecting = expectation(description: "taskDidFinishCollecting should fire")
- let didCreateURLRequest = expectation(description: "didCreateInitialURLRequest should fire")
- let didCreateTask = expectation(description: "didCreateTask should fire")
- let didGatherMetrics = expectation(description: "didGatherMetrics should fire")
- let didComplete = expectation(description: "didComplete should fire")
- let didFinish = expectation(description: "didFinish should fire")
- let didResume = expectation(description: "didResume should fire")
- let didResumeTask = expectation(description: "didResumeTask should fire")
- let didParseResponse = expectation(description: "didParseResponse should fire")
- let didCancel = expectation(description: "didCancel should fire")
- let didCancelTask = expectation(description: "didCancelTask should fire")
- let responseHandler = expectation(description: "responseHandler should fire")
- eventMonitor.taskDidFinishCollectingMetrics = { _, _, _ in taskDidFinishCollecting.fulfill() }
- eventMonitor.requestDidCreateInitialURLRequest = { _, _ in didCreateURLRequest.fulfill() }
- eventMonitor.requestDidCreateTask = { _, _ in didCreateTask.fulfill() }
- eventMonitor.requestDidGatherMetrics = { _, _ in didGatherMetrics.fulfill() }
- eventMonitor.requestDidCompleteTaskWithError = { _, _, _ in didComplete.fulfill() }
- eventMonitor.requestDidFinish = { _ in didFinish.fulfill() }
- eventMonitor.requestDidResume = { _ in didResume.fulfill() }
- eventMonitor.requestDidParseResponse = { _, _ in didParseResponse.fulfill() }
- eventMonitor.requestDidCancel = { _ in didCancel.fulfill() }
- eventMonitor.requestDidCancelTask = { _, _ in didCancelTask.fulfill() }
- // When
- let request = session.request(URLRequest.makeHTTPBinRequest()).response { _ in
- responseHandler.fulfill()
- }
- eventMonitor.requestDidResumeTask = { _, _ in
- request.cancel()
- didResumeTask.fulfill()
- }
- request.resume()
- waitForExpectations(timeout: timeout, handler: nil)
- // Then
- XCTAssertEqual(request.state, .cancelled)
- }
- func testThatAppendingResponseSerializerToCancelledRequestCallsCompletion() {
- // Given
- let session = Session()
- var response1: DataResponse<Any, AFError>?
- var response2: DataResponse<Any, AFError>?
- let expect = expectation(description: "both response serializer completions should be called")
- expect.expectedFulfillmentCount = 2
- // When
- let request = session.request(URLRequest.makeHTTPBinRequest())
- request.responseJSON { resp in
- response1 = resp
- expect.fulfill()
- request.responseJSON { resp in
- response2 = resp
- expect.fulfill()
- }
- }
- request.cancel()
- waitForExpectations(timeout: timeout, handler: nil)
- // Then
- XCTAssertEqual(response1?.error?.isExplicitlyCancelledError, true)
- XCTAssertEqual(response2?.error?.isExplicitlyCancelledError, true)
- }
- func testThatAppendingResponseSerializerToCompletedRequestInsideCompletionResumesRequest() {
- // Given
- let session = Session()
- var response1: DataResponse<Any, AFError>?
- var response2: DataResponse<Any, AFError>?
- var response3: DataResponse<Any, AFError>?
- let expect = expectation(description: "all response serializer completions should be called")
- expect.expectedFulfillmentCount = 3
- // When
- let request = session.request(URLRequest.makeHTTPBinRequest())
- request.responseJSON { resp in
- response1 = resp
- expect.fulfill()
- request.responseJSON { resp in
- response2 = resp
- expect.fulfill()
- request.responseJSON { resp in
- response3 = resp
- expect.fulfill()
- }
- }
- }
- waitForExpectations(timeout: timeout, handler: nil)
- // Then
- XCTAssertNotNil(response1?.value)
- XCTAssertNotNil(response2?.value)
- XCTAssertNotNil(response3?.value)
- }
- func testThatAppendingResponseSerializerToCompletedRequestOutsideCompletionResumesRequest() {
- // Given
- let session = Session()
- let request = session.request(URLRequest.makeHTTPBinRequest())
- var response1: DataResponse<Any, AFError>?
- var response2: DataResponse<Any, AFError>?
- var response3: DataResponse<Any, AFError>?
- // When
- let expect1 = expectation(description: "response serializer 1 completion should be called")
- request.responseJSON { response1 = $0; expect1.fulfill() }
- waitForExpectations(timeout: timeout, handler: nil)
- let expect2 = expectation(description: "response serializer 2 completion should be called")
- request.responseJSON { response2 = $0; expect2.fulfill() }
- waitForExpectations(timeout: timeout, handler: nil)
- let expect3 = expectation(description: "response serializer 3 completion should be called")
- request.responseJSON { response3 = $0; expect3.fulfill() }
- waitForExpectations(timeout: timeout, handler: nil)
- // Then
- XCTAssertNotNil(response1?.value)
- XCTAssertNotNil(response2?.value)
- XCTAssertNotNil(response3?.value)
- }
- }
- // MARK: -
- class RequestDescriptionTestCase: BaseTestCase {
- func testRequestDescription() {
- // Given
- let urlString = "https://httpbin.org/get"
- let manager = Session(startRequestsImmediately: false)
- let request = manager.request(urlString)
- let expectation = self.expectation(description: "Request description should update: \(urlString)")
- var response: HTTPURLResponse?
- // When
- request.response { resp in
- response = resp.response
- expectation.fulfill()
- }.resume()
- waitForExpectations(timeout: timeout, handler: nil)
- // Then
- XCTAssertEqual(request.description, "GET https://httpbin.org/get (\(response?.statusCode ?? -1))")
- }
- }
- // MARK: -
- final class RequestCURLDescriptionTestCase: BaseTestCase {
- // MARK: Properties
- let manager: Session = {
- let manager = Session()
- return manager
- }()
- let managerWithAcceptLanguageHeader: Session = {
- var headers = HTTPHeaders.default
- headers["Accept-Language"] = "en-US"
- let configuration = URLSessionConfiguration.af.default
- configuration.headers = headers
- let manager = Session(configuration: configuration)
- return manager
- }()
- let managerWithContentTypeHeader: Session = {
- var headers = HTTPHeaders.default
- headers["Content-Type"] = "application/json"
- let configuration = URLSessionConfiguration.af.default
- configuration.headers = headers
- let manager = Session(configuration: configuration)
- return manager
- }()
- func managerWithCookie(_ cookie: HTTPCookie) -> Session {
- let configuration = URLSessionConfiguration.af.default
- configuration.httpCookieStorage?.setCookie(cookie)
- return Session(configuration: configuration)
- }
- let managerDisallowingCookies: Session = {
- let configuration = URLSessionConfiguration.af.default
- configuration.httpShouldSetCookies = false
- let manager = Session(configuration: configuration)
- return manager
- }()
- // MARK: Tests
- func testGETRequestCURLDescription() {
- // Given
- let urlString = "https://httpbin.org/get"
- let expectation = self.expectation(description: "request should complete")
- var components: [String]?
- // When
- manager.request(urlString).cURLDescription {
- components = self.cURLCommandComponents(from: $0)
- expectation.fulfill()
- }
- waitForExpectations(timeout: timeout, handler: nil)
- // Then
- XCTAssertEqual(components?[0..<3], ["$", "curl", "-v"])
- XCTAssertTrue(components?.contains("-X") == true)
- XCTAssertEqual(components?.last, "\"\(urlString)\"")
- }
- func testGETRequestCURLDescriptionSynchronous() {
- // Given
- let urlString = "https://httpbin.org/get"
- let expectation = self.expectation(description: "request should complete")
- var components: [String]?
- var syncComponents: [String]?
- // When
- let request = manager.request(urlString)
- request.cURLDescription {
- components = self.cURLCommandComponents(from: $0)
- syncComponents = self.cURLCommandComponents(from: request.cURLDescription())
- expectation.fulfill()
- }
- waitForExpectations(timeout: timeout, handler: nil)
- // Then
- XCTAssertEqual(components?[0..<3], ["$", "curl", "-v"])
- XCTAssertTrue(components?.contains("-X") == true)
- XCTAssertEqual(components?.last, "\"\(urlString)\"")
- XCTAssertEqual(components?.sorted(), syncComponents?.sorted())
- }
- func testGETRequestCURLDescriptionCanBeRequestedManyTimes() {
- // Given
- let urlString = "https://httpbin.org/get"
- let expectation = self.expectation(description: "request should complete")
- var components: [String]?
- var secondComponents: [String]?
- // When
- let request = manager.request(urlString)
- request.cURLDescription {
- components = self.cURLCommandComponents(from: $0)
- request.cURLDescription {
- secondComponents = self.cURLCommandComponents(from: $0)
- expectation.fulfill()
- }
- }
- // Trigger the overwrite behavior.
- request.cURLDescription {
- components = self.cURLCommandComponents(from: $0)
- request.cURLDescription {
- secondComponents = self.cURLCommandComponents(from: $0)
- expectation.fulfill()
- }
- }
- waitForExpectations(timeout: timeout, handler: nil)
- // Then
- XCTAssertEqual(components?[0..<3], ["$", "curl", "-v"])
- XCTAssertTrue(components?.contains("-X") == true)
- XCTAssertEqual(components?.last, "\"\(urlString)\"")
- XCTAssertEqual(components?.sorted(), secondComponents?.sorted())
- }
- func testGETRequestWithCustomHeaderCURLDescription() {
- // Given
- let urlString = "https://httpbin.org/get"
- let expectation = self.expectation(description: "request should complete")
- var cURLDescription: String?
- // When
- let headers: HTTPHeaders = ["X-Custom-Header": "{\"key\": \"value\"}"]
- manager.request(urlString, headers: headers).cURLDescription {
- cURLDescription = $0
- expectation.fulfill()
- }
- waitForExpectations(timeout: timeout, handler: nil)
- // Then
- XCTAssertNotNil(cURLDescription?.range(of: "-H \"X-Custom-Header: {\\\"key\\\": \\\"value\\\"}\""))
- }
- func testGETRequestWithDuplicateHeadersDebugDescription() {
- // Given
- let urlString = "https://httpbin.org/get"
- let expectation = self.expectation(description: "request should complete")
- var cURLDescription: String?
- var components: [String]?
- // When
- let headers: HTTPHeaders = ["Accept-Language": "en-GB"]
- managerWithAcceptLanguageHeader.request(urlString, headers: headers).cURLDescription {
- components = self.cURLCommandComponents(from: $0)
- cURLDescription = $0
- expectation.fulfill()
- }
- waitForExpectations(timeout: timeout, handler: nil)
- // Then
- XCTAssertEqual(components?[0..<3], ["$", "curl", "-v"])
- XCTAssertTrue(components?.contains("-X") == true)
- XCTAssertEqual(components?.last, "\"\(urlString)\"")
- let acceptLanguageCount = components?.filter { $0.contains("Accept-Language") }.count
- XCTAssertEqual(acceptLanguageCount, 1, "command should contain a single Accept-Language header")
- XCTAssertNotNil(cURLDescription?.range(of: "-H \"Accept-Language: en-GB\""))
- }
- func testPOSTRequestCURLDescription() {
- // Given
- let urlString = "https://httpbin.org/post"
- let expectation = self.expectation(description: "request should complete")
- var components: [String]?
- // When
- manager.request(urlString, method: .post).cURLDescription {
- components = self.cURLCommandComponents(from: $0)
- expectation.fulfill()
- }
- waitForExpectations(timeout: timeout, handler: nil)
- // Then
- XCTAssertEqual(components?[0..<3], ["$", "curl", "-v"])
- XCTAssertEqual(components?[3..<5], ["-X", "POST"])
- XCTAssertEqual(components?.last, "\"\(urlString)\"")
- }
- func testPOSTRequestWithJSONParametersCURLDescription() {
- // Given
- let urlString = "https://httpbin.org/post"
- let expectation = self.expectation(description: "request should complete")
- var cURLDescription: String?
- var components: [String]?
- let parameters = ["foo": "bar",
- "fo\"o": "b\"ar",
- "f'oo": "ba'r"]
- // When
- manager.request(urlString, method: .post, parameters: parameters, encoding: JSONEncoding.default).cURLDescription {
- components = self.cURLCommandComponents(from: $0)
- cURLDescription = $0
- expectation.fulfill()
- }
- waitForExpectations(timeout: timeout, handler: nil)
- // Then
- XCTAssertEqual(components?[0..<3], ["$", "curl", "-v"])
- XCTAssertEqual(components?[3..<5], ["-X", "POST"])
- XCTAssertNotNil(cURLDescription?.range(of: "-H \"Content-Type: application/json\""))
- XCTAssertNotNil(cURLDescription?.range(of: "-d \"{"))
- XCTAssertNotNil(cURLDescription?.range(of: "\\\"f'oo\\\":\\\"ba'r\\\""))
- XCTAssertNotNil(cURLDescription?.range(of: "\\\"fo\\\\\\\"o\\\":\\\"b\\\\\\\"ar\\\""))
- XCTAssertNotNil(cURLDescription?.range(of: "\\\"foo\\\":\\\"bar\\"))
- XCTAssertEqual(components?.last, "\"\(urlString)\"")
- }
- func testPOSTRequestWithCookieCURLDescription() {
- // Given
- let urlString = "https://httpbin.org/post"
- let properties = [HTTPCookiePropertyKey.domain: "httpbin.org",
- HTTPCookiePropertyKey.path: "/post",
- HTTPCookiePropertyKey.name: "foo",
- HTTPCookiePropertyKey.value: "bar"]
- let cookie = HTTPCookie(properties: properties)!
- let cookieManager = managerWithCookie(cookie)
- let expectation = self.expectation(description: "request should complete")
- var components: [String]?
- // When
- cookieManager.request(urlString, method: .post).cURLDescription {
- components = self.cURLCommandComponents(from: $0)
- expectation.fulfill()
- }
- waitForExpectations(timeout: timeout, handler: nil)
- // Then
- XCTAssertEqual(components?[0..<3], ["$", "curl", "-v"])
- XCTAssertEqual(components?[3..<5], ["-X", "POST"])
- XCTAssertEqual(components?.last, "\"\(urlString)\"")
- XCTAssertEqual(components?[5..<6], ["-b"])
- }
- func testPOSTRequestWithCookiesDisabledCURLDescriptionHasNoCookies() {
- // Given
- let urlString = "https://httpbin.org/post"
- let properties = [HTTPCookiePropertyKey.domain: "httpbin.org",
- HTTPCookiePropertyKey.path: "/post",
- HTTPCookiePropertyKey.name: "foo",
- HTTPCookiePropertyKey.value: "bar"]
- let cookie = HTTPCookie(properties: properties)!
- managerDisallowingCookies.session.configuration.httpCookieStorage?.setCookie(cookie)
- let expectation = self.expectation(description: "request should complete")
- var components: [String]?
- // When
- managerDisallowingCookies.request(urlString, method: .post).cURLDescription {
- components = self.cURLCommandComponents(from: $0)
- expectation.fulfill()
- }
- waitForExpectations(timeout: timeout, handler: nil)
- // Then
- let cookieComponents = components?.filter { $0 == "-b" }
- XCTAssertTrue(cookieComponents?.isEmpty == true)
- }
- func testMultipartFormDataRequestWithDuplicateHeadersCURLDescriptionHasOneContentTypeHeader() {
- // Given
- let urlString = "https://httpbin.org/post"
- let japaneseData = Data("日本語".utf8)
- let expectation = self.expectation(description: "multipart form data encoding should succeed")
- var cURLDescription: String?
- var components: [String]?
- // When
- managerWithContentTypeHeader.upload(multipartFormData: { data in
- data.append(japaneseData, withName: "japanese")
- }, to: urlString).cURLDescription {
- components = self.cURLCommandComponents(from: $0)
- cURLDescription = $0
- expectation.fulfill()
- }
- waitForExpectations(timeout: timeout, handler: nil)
- // Then
- XCTAssertEqual(components?[0..<3], ["$", "curl", "-v"])
- XCTAssertTrue(components?.contains("-X") == true)
- XCTAssertEqual(components?.last, "\"\(urlString)\"")
- let contentTypeCount = components?.filter { $0.contains("Content-Type") }.count
- XCTAssertEqual(contentTypeCount, 1, "command should contain a single Content-Type header")
- XCTAssertNotNil(cURLDescription?.range(of: "-H \"Content-Type: multipart/form-data;"))
- }
- func testThatRequestWithInvalidURLDebugDescription() {
- // Given
- let urlString = "invalid_url"
- let expectation = self.expectation(description: "request should complete")
- var cURLDescription: String?
- // When
- manager.request(urlString).cURLDescription {
- cURLDescription = $0
- expectation.fulfill()
- }
- waitForExpectations(timeout: timeout, handler: nil)
- // Then
- XCTAssertNotNil(cURLDescription, "debugDescription should not crash")
- }
- // MARK: Test Helper Methods
- private func cURLCommandComponents(from cURLString: String) -> [String] {
- return cURLString.components(separatedBy: .whitespacesAndNewlines)
- .filter { $0 != "" && $0 != "\\" }
- }
- }
|