瀏覽代碼

Added test suite for request and session redirect handlers

Christian Noon 6 年之前
父節點
當前提交
f587f298a3
共有 2 個文件被更改,包括 232 次插入0 次删除
  1. 8 0
      Alamofire.xcodeproj/project.pbxproj
  2. 224 0
      Tests/RedirectHandlerTests.swift

+ 8 - 0
Alamofire.xcodeproj/project.pbxproj

@@ -298,6 +298,9 @@
 		4CFB030D1D7D2FA20056F249 /* utf8_string.txt in Resources */ = {isa = PBXBuildFile; fileRef = 4CFB02F41D7D2FA20056F249 /* utf8_string.txt */; };
 		4CFB030E1D7D2FA20056F249 /* utf8_string.txt in Resources */ = {isa = PBXBuildFile; fileRef = 4CFB02F41D7D2FA20056F249 /* utf8_string.txt */; };
 		4CFB030F1D7D2FA20056F249 /* utf8_string.txt in Resources */ = {isa = PBXBuildFile; fileRef = 4CFB02F41D7D2FA20056F249 /* utf8_string.txt */; };
+		4CFD6B102201145500FFB5E3 /* RedirectHandlerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CFD6B0F2201145500FFB5E3 /* RedirectHandlerTests.swift */; };
+		4CFD6B112201145500FFB5E3 /* RedirectHandlerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CFD6B0F2201145500FFB5E3 /* RedirectHandlerTests.swift */; };
+		4CFD6B122201145500FFB5E3 /* RedirectHandlerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CFD6B0F2201145500FFB5E3 /* RedirectHandlerTests.swift */; };
 		4DD67C241A5C58FB00ED2280 /* Alamofire.h in Headers */ = {isa = PBXBuildFile; fileRef = F8111E3819A95C8B0040E7D1 /* Alamofire.h */; settings = {ATTRIBUTES = (Public, ); }; };
 		4DD67C251A5C590000ED2280 /* Alamofire.swift in Sources */ = {isa = PBXBuildFile; fileRef = F897FF4019AA800700AB5182 /* Alamofire.swift */; };
 		8035DB621BAB492500466CB3 /* Alamofire.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F8111E3319A95C8B0040E7D1 /* Alamofire.framework */; };
@@ -428,6 +431,7 @@
 		4CFB02F21D7D2FA20056F249 /* empty_string.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = empty_string.txt; sourceTree = "<group>"; };
 		4CFB02F31D7D2FA20056F249 /* utf32_string.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = utf32_string.txt; sourceTree = "<group>"; };
 		4CFB02F41D7D2FA20056F249 /* utf8_string.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = utf8_string.txt; sourceTree = "<group>"; };
+		4CFD6B0F2201145500FFB5E3 /* RedirectHandlerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RedirectHandlerTests.swift; sourceTree = "<group>"; };
 		4DD67C0B1A5C55C900ED2280 /* Alamofire.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Alamofire.framework; sourceTree = BUILT_PRODUCTS_DIR; };
 		B39E2F831C1A72F8002DA1A9 /* certDER.cer */ = {isa = PBXFileReference; lastKnownFileType = file; name = certDER.cer; path = selfSignedAndMalformedCerts/certDER.cer; sourceTree = "<group>"; };
 		B39E2F841C1A72F8002DA1A9 /* certDER.crt */ = {isa = PBXFileReference; lastKnownFileType = file; name = certDER.crt; path = selfSignedAndMalformedCerts/certDER.crt; sourceTree = "<group>"; };
@@ -535,6 +539,7 @@
 				3113D46A21878227001CCD21 /* HTTPHeadersTests.swift */,
 				4C3238E61B3604DB00FE04AE /* MultipartFormDataTests.swift */,
 				4C3D00571C66A8B900D1F709 /* NetworkReachabilityManagerTests.swift */,
+				4CFD6B0F2201145500FFB5E3 /* RedirectHandlerTests.swift */,
 				4C0B58381B747A4400C0B99C /* ResponseSerializationTests.swift */,
 				4C33A1421B52089C00873DFF /* ServerTrustEvaluatorTests.swift */,
 				F86AEFE51AE6A282007D9C76 /* TLSEvaluationTests.swift */,
@@ -1334,6 +1339,7 @@
 				317A6A7820B2208000A9FEC5 /* DownloadTests.swift in Sources */,
 				31F9683E20BB70290009606F /* NSLoggingEventMonitor.swift in Sources */,
 				3113D46D21878227001CCD21 /* HTTPHeadersTests.swift in Sources */,
+				4CFD6B122201145500FFB5E3 /* RedirectHandlerTests.swift in Sources */,
 				31501E8A2196962A005829F2 /* ParameterEncoderTests.swift in Sources */,
 				3107EA4120A1267D00445260 /* SessionDelegateTests.swift in Sources */,
 				31C2B0EC20B271060089BA7C /* CacheTests.swift in Sources */,
@@ -1470,6 +1476,7 @@
 				317A6A7620B2207F00A9FEC5 /* DownloadTests.swift in Sources */,
 				31F9683C20BB70290009606F /* NSLoggingEventMonitor.swift in Sources */,
 				3113D46B21878227001CCD21 /* HTTPHeadersTests.swift in Sources */,
+				4CFD6B102201145500FFB5E3 /* RedirectHandlerTests.swift in Sources */,
 				31501E882196962A005829F2 /* ParameterEncoderTests.swift in Sources */,
 				3107EA3F20A1267C00445260 /* SessionDelegateTests.swift in Sources */,
 				31C2B0EA20B271040089BA7C /* CacheTests.swift in Sources */,
@@ -1501,6 +1508,7 @@
 				317A6A7720B2208000A9FEC5 /* DownloadTests.swift in Sources */,
 				31F9683D20BB70290009606F /* NSLoggingEventMonitor.swift in Sources */,
 				3113D46C21878227001CCD21 /* HTTPHeadersTests.swift in Sources */,
+				4CFD6B112201145500FFB5E3 /* RedirectHandlerTests.swift in Sources */,
 				31501E892196962A005829F2 /* ParameterEncoderTests.swift in Sources */,
 				3107EA4020A1267C00445260 /* SessionDelegateTests.swift in Sources */,
 				31C2B0EB20B271050089BA7C /* CacheTests.swift in Sources */,

+ 224 - 0
Tests/RedirectHandlerTests.swift

@@ -0,0 +1,224 @@
+//
+//  RedirectHandlerTests.swift
+//
+//  Copyright (c) 2019 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 RedirectHandlerTestCase: BaseTestCase {
+
+    // MARK: - Properties
+
+    private var redirectURLString: String { return "https://www.apple.com/" }
+    private var urlString: String { return "https://httpbin.org/redirect-to?url=\(redirectURLString)" }
+
+    // MARK: - Tests - Per Request
+
+    func testThatRequestRedirectHandlerCanFollowRedirects() {
+        // Given
+        let session = Session()
+
+        var response: DataResponse<Data?>?
+        let expectation = self.expectation(description: "Request should redirect to \(redirectURLString)")
+
+        // When
+        session.request(urlString).redirect(using: Redirector.follow).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)
+
+        XCTAssertEqual(response?.response?.url?.absoluteString, redirectURLString)
+        XCTAssertEqual(response?.response?.statusCode, 200)
+    }
+
+    func testThatRequestRedirectHandlerCanNotFollowRedirects() {
+        // Given
+        let session = Session()
+
+        var response: DataResponse<Data?>?
+        let expectation = self.expectation(description: "Request should NOT redirect to \(redirectURLString)")
+
+        // When
+        session.request(urlString).redirect(using: Redirector.doNotFollow).response { resp in
+            response = resp
+            expectation.fulfill()
+        }
+
+        waitForExpectations(timeout: timeout, handler: nil)
+
+        // Then
+        XCTAssertNotNil(response?.request)
+        XCTAssertNotNil(response?.response)
+        XCTAssertNil(response?.data)
+        XCTAssertNil(response?.error)
+
+        XCTAssertEqual(response?.response?.url?.absoluteString, urlString)
+        XCTAssertEqual(response?.response?.statusCode, 302)
+    }
+
+    func testThatRequestRedirectHandlerCanModifyRedirects() {
+        // Given
+        let session = Session()
+        let redirectURLString = "https://www.nike.com"
+        let redirectURLRequest = URLRequest(url: URL(string: redirectURLString)!)
+
+        var response: DataResponse<Data?>?
+        let expectation = self.expectation(description: "Request should redirect to \(redirectURLString)")
+
+        // When
+        let redirector = Redirector(behavior: .modify { _, _, _ in redirectURLRequest })
+
+        session.request(urlString).redirect(using: redirector).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)
+
+        XCTAssertEqual(response?.response?.url?.absoluteString, redirectURLString)
+        XCTAssertEqual(response?.response?.statusCode, 200)
+    }
+
+    // MARK: - Tests - Per Session
+
+    func testThatSessionRedirectHandlerCanFollowRedirects() {
+        // Given
+        let session = Session(redirectHandler: Redirector.follow)
+
+        var response: DataResponse<Data?>?
+        let expectation = self.expectation(description: "Request should redirect to \(redirectURLString)")
+
+        // When
+        session.request(urlString).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)
+
+        XCTAssertEqual(response?.response?.url?.absoluteString, redirectURLString)
+        XCTAssertEqual(response?.response?.statusCode, 200)
+    }
+
+    func testThatSessionRedirectHandlerCanNotFollowRedirects() {
+        // Given
+        let session = Session(redirectHandler: Redirector.doNotFollow)
+
+        var response: DataResponse<Data?>?
+        let expectation = self.expectation(description: "Request should NOT redirect to \(redirectURLString)")
+
+        // When
+        session.request(urlString).response { resp in
+            response = resp
+            expectation.fulfill()
+        }
+
+        waitForExpectations(timeout: timeout, handler: nil)
+
+        // Then
+        XCTAssertNotNil(response?.request)
+        XCTAssertNotNil(response?.response)
+        XCTAssertNil(response?.data)
+        XCTAssertNil(response?.error)
+
+        XCTAssertEqual(response?.response?.url?.absoluteString, urlString)
+        XCTAssertEqual(response?.response?.statusCode, 302)
+    }
+
+    func testThatSessionRedirectHandlerCanModifyRedirects() {
+        // Given
+        let redirectURLString = "https://www.nike.com"
+        let redirectURLRequest = URLRequest(url: URL(string: redirectURLString)!)
+
+        let redirector = Redirector(behavior: .modify { _, _, _ in redirectURLRequest })
+        let session = Session(redirectHandler: redirector)
+
+        var response: DataResponse<Data?>?
+        let expectation = self.expectation(description: "Request should redirect to \(redirectURLString)")
+
+        // When
+        session.request(urlString).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)
+
+        XCTAssertEqual(response?.response?.url?.absoluteString, redirectURLString)
+        XCTAssertEqual(response?.response?.statusCode, 200)
+    }
+
+    // MARK: - Tests - Per Request Prioritization
+
+    func testThatRequestRedirectHandlerIsPrioritizedOverSessionRedirectHandler() {
+        // Given
+        let session = Session(redirectHandler: Redirector.doNotFollow)
+
+        var response: DataResponse<Data?>?
+        let expectation = self.expectation(description: "Request should redirect to \(redirectURLString)")
+
+        // When
+        session.request(urlString).redirect(using: Redirector.follow).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)
+
+        XCTAssertEqual(response?.response?.url?.absoluteString, redirectURLString)
+        XCTAssertEqual(response?.response?.statusCode, 200)
+    }
+}