SessionDelegateTests.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // SessionDelegateTests.swift
  3. //
  4. // Copyright (c) 2014-2018 Alamofire Software Foundation (http://alamofire.org/)
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. @testable import Alamofire
  25. import Foundation
  26. import XCTest
  27. class SessionDelegateTestCase: BaseTestCase {
  28. var manager: Session!
  29. // MARK: - Setup and Teardown
  30. override func setUp() {
  31. super.setUp()
  32. manager = Session(configuration: .ephemeral)
  33. }
  34. // MARK: - Tests - Redirects
  35. func testThatRequestWillPerformHTTPRedirectionByDefault() {
  36. // Given
  37. let redirectURLString = "https://www.apple.com/"
  38. let urlString = "https://httpbin.org/redirect-to?url=\(redirectURLString)"
  39. let expectation = self.expectation(description: "Request should redirect to \(redirectURLString)")
  40. var response: DataResponse<Data?>?
  41. // When
  42. manager.request(urlString)
  43. .response { resp in
  44. response = resp
  45. expectation.fulfill()
  46. }
  47. waitForExpectations(timeout: timeout, handler: nil)
  48. // Then
  49. XCTAssertNotNil(response?.request)
  50. XCTAssertNotNil(response?.response)
  51. XCTAssertNotNil(response?.data)
  52. XCTAssertNil(response?.error)
  53. XCTAssertEqual(response?.response?.url?.absoluteString, redirectURLString)
  54. XCTAssertEqual(response?.response?.statusCode, 200)
  55. }
  56. func testThatRequestWillPerformRedirectionMultipleTimesByDefault() {
  57. // Given
  58. let redirectURLString = "https://httpbin.org/get"
  59. let urlString = "https://httpbin.org/redirect/5"
  60. let expectation = self.expectation(description: "Request should redirect to \(redirectURLString)")
  61. var response: DataResponse<Data?>?
  62. // When
  63. manager.request(urlString)
  64. .response { resp in
  65. response = resp
  66. expectation.fulfill()
  67. }
  68. waitForExpectations(timeout: timeout, handler: nil)
  69. // Then
  70. XCTAssertNotNil(response?.request)
  71. XCTAssertNotNil(response?.response)
  72. XCTAssertNotNil(response?.data)
  73. XCTAssertNil(response?.error)
  74. XCTAssertEqual(response?.response?.url?.absoluteString, redirectURLString)
  75. XCTAssertEqual(response?.response?.statusCode, 200)
  76. }
  77. func testThatAppropriateNotificationsAreCalledWithRequestForDataRequest() {
  78. // Given
  79. var request: Request?
  80. _ = expectation(forNotification: Request.didResume, object: nil, handler: nil)
  81. _ = expectation(forNotification: Request.didComplete, object: nil) { (notification) in
  82. request = notification.request
  83. return (request != nil)
  84. }
  85. // When
  86. manager.request("https://httpbin.org/get").response { _ in }
  87. waitForExpectations(timeout: timeout, handler: nil)
  88. // Then
  89. XCTAssertEqual(request?.response?.statusCode, 200)
  90. }
  91. func testThatDidCompleteNotificationIsCalledWithRequestForDownloadRequests() {
  92. // Given
  93. var request: Request?
  94. _ = expectation(forNotification: Request.didResume, object: nil, handler: nil)
  95. _ = expectation(forNotification: Request.didComplete, object: nil) { (notification) in
  96. request = notification.request
  97. return (request != nil)
  98. }
  99. // When
  100. manager.download("https://httpbin.org/get").response { _ in }
  101. waitForExpectations(timeout: timeout, handler: nil)
  102. // Then
  103. XCTAssertEqual(request?.response?.statusCode, 200)
  104. }
  105. }