ResponseTests.swift 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. // ResponseTests.swift
  2. //
  3. // Copyright (c) 2014–2016 Alamofire Software Foundation (http://alamofire.org/)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. import Alamofire
  23. import Foundation
  24. import XCTest
  25. class ResponseDataTestCase: BaseTestCase {
  26. func testThatResponseDataReturnsSuccessResultWithValidData() {
  27. // Given
  28. let URLString = "https://httpbin.org/get"
  29. let expectation = expectationWithDescription("request should succeed")
  30. var response: Response<NSData, NSError>?
  31. // When
  32. Alamofire.request(.GET, URLString, parameters: ["foo": "bar"])
  33. .responseData { closureResponse in
  34. response = closureResponse
  35. expectation.fulfill()
  36. }
  37. waitForExpectationsWithTimeout(timeout, handler: nil)
  38. // Then
  39. if let response = response {
  40. XCTAssertNotNil(response.request, "request should not be nil")
  41. XCTAssertNotNil(response.response, "response should not be nil")
  42. XCTAssertNotNil(response.data, "data should not be nil")
  43. XCTAssertTrue(response.result.isSuccess, "result should be success")
  44. } else {
  45. XCTFail("response should not be nil")
  46. }
  47. }
  48. func testThatResponseDataReturnsFailureResultWithOptionalDataAndError() {
  49. // Given
  50. let URLString = "https://invalid-url-here.org/this/does/not/exist"
  51. let expectation = expectationWithDescription("request should fail with 404")
  52. var response: Response<NSData, NSError>?
  53. // When
  54. Alamofire.request(.GET, URLString, parameters: ["foo": "bar"])
  55. .responseData { closureResponse in
  56. response = closureResponse
  57. expectation.fulfill()
  58. }
  59. waitForExpectationsWithTimeout(timeout, handler: nil)
  60. // Then
  61. if let response = response {
  62. XCTAssertNotNil(response.request, "request should not be nil")
  63. XCTAssertNil(response.response, "response should be nil")
  64. XCTAssertNotNil(response.data, "data should not be nil")
  65. XCTAssertTrue(response.result.isFailure, "result should be failure")
  66. } else {
  67. XCTFail("response should not be nil")
  68. }
  69. }
  70. }
  71. // MARK: -
  72. class ResponseStringTestCase: BaseTestCase {
  73. func testThatResponseStringReturnsSuccessResultWithValidString() {
  74. // Given
  75. let URLString = "https://httpbin.org/get"
  76. let expectation = expectationWithDescription("request should succeed")
  77. var response: Response<String, NSError>?
  78. // When
  79. Alamofire.request(.GET, URLString, parameters: ["foo": "bar"])
  80. .responseString { closureResponse in
  81. response = closureResponse
  82. expectation.fulfill()
  83. }
  84. waitForExpectationsWithTimeout(timeout, handler: nil)
  85. // Then
  86. if let response = response {
  87. XCTAssertNotNil(response.request, "request should not be nil")
  88. XCTAssertNotNil(response.response, "response should not be nil")
  89. XCTAssertNotNil(response.data, "data should not be nil")
  90. XCTAssertTrue(response.result.isSuccess, "result should be success")
  91. } else {
  92. XCTFail("response should not be nil")
  93. }
  94. }
  95. func testThatResponseStringReturnsFailureResultWithOptionalDataAndError() {
  96. // Given
  97. let URLString = "https://invalid-url-here.org/this/does/not/exist"
  98. let expectation = expectationWithDescription("request should fail with 404")
  99. var response: Response<String, NSError>?
  100. // When
  101. Alamofire.request(.GET, URLString, parameters: ["foo": "bar"])
  102. .responseString { closureResponse in
  103. response = closureResponse
  104. expectation.fulfill()
  105. }
  106. waitForExpectationsWithTimeout(timeout, handler: nil)
  107. // Then
  108. if let response = response {
  109. XCTAssertNotNil(response.request, "request should not be nil")
  110. XCTAssertNil(response.response, "response should be nil")
  111. XCTAssertNotNil(response.data, "data should not be nil")
  112. XCTAssertTrue(response.result.isFailure, "result should be failure")
  113. } else {
  114. XCTFail("response should not be nil")
  115. }
  116. }
  117. }
  118. // MARK: -
  119. class ResponseJSONTestCase: BaseTestCase {
  120. func testThatResponseJSONReturnsSuccessResultWithValidJSON() {
  121. // Given
  122. let URLString = "https://httpbin.org/get"
  123. let expectation = expectationWithDescription("request should succeed")
  124. var response: Response<AnyObject, NSError>?
  125. // When
  126. Alamofire.request(.GET, URLString, parameters: ["foo": "bar"])
  127. .responseJSON { closureResponse in
  128. response = closureResponse
  129. expectation.fulfill()
  130. }
  131. waitForExpectationsWithTimeout(timeout, handler: nil)
  132. // Then
  133. if let response = response {
  134. XCTAssertNotNil(response.request, "request should not be nil")
  135. XCTAssertNotNil(response.response, "response should not be nil")
  136. XCTAssertNotNil(response.data, "data should not be nil")
  137. XCTAssertTrue(response.result.isSuccess, "result should be success")
  138. } else {
  139. XCTFail("response should not be nil")
  140. }
  141. }
  142. func testThatResponseStringReturnsFailureResultWithOptionalDataAndError() {
  143. // Given
  144. let URLString = "https://invalid-url-here.org/this/does/not/exist"
  145. let expectation = expectationWithDescription("request should fail with 404")
  146. var response: Response<AnyObject, NSError>?
  147. // When
  148. Alamofire.request(.GET, URLString, parameters: ["foo": "bar"])
  149. .responseJSON { closureResponse in
  150. response = closureResponse
  151. expectation.fulfill()
  152. }
  153. waitForExpectationsWithTimeout(timeout, handler: nil)
  154. // Then
  155. if let response = response {
  156. XCTAssertNotNil(response.request, "request should not be nil")
  157. XCTAssertNil(response.response, "response should be nil")
  158. XCTAssertNotNil(response.data, "data should not be nil")
  159. XCTAssertTrue(response.result.isFailure, "result should be failure")
  160. } else {
  161. XCTFail("response should not be nil")
  162. }
  163. }
  164. func testThatResponseJSONReturnsSuccessResultForGETRequest() {
  165. // Given
  166. let URLString = "https://httpbin.org/get"
  167. let expectation = expectationWithDescription("request should succeed")
  168. var response: Response<AnyObject, NSError>?
  169. // When
  170. Alamofire.request(.GET, URLString, parameters: ["foo": "bar"])
  171. .responseJSON { closureResponse in
  172. response = closureResponse
  173. expectation.fulfill()
  174. }
  175. waitForExpectationsWithTimeout(timeout, handler: nil)
  176. // Then
  177. if let response = response {
  178. XCTAssertNotNil(response.request, "request should not be nil")
  179. XCTAssertNotNil(response.response, "response should not be nil")
  180. XCTAssertNotNil(response.data, "data should not be nil")
  181. XCTAssertTrue(response.result.isSuccess, "result should be success")
  182. // The `as NSString` cast is necessary due to a compiler bug. See the following rdar for more info.
  183. // - https://openradar.appspot.com/radar?id=5517037090635776
  184. if let args = response.result.value?["args" as NSString] as? [String: String] {
  185. XCTAssertEqual(args, ["foo": "bar"], "args should match parameters")
  186. } else {
  187. XCTFail("args should not be nil")
  188. }
  189. } else {
  190. XCTFail("response should not be nil")
  191. }
  192. }
  193. func testThatResponseJSONReturnsSuccessResultForPOSTRequest() {
  194. // Given
  195. let URLString = "https://httpbin.org/post"
  196. let expectation = expectationWithDescription("request should succeed")
  197. var response: Response<AnyObject, NSError>?
  198. // When
  199. Alamofire.request(.POST, URLString, parameters: ["foo": "bar"])
  200. .responseJSON { closureResponse in
  201. response = closureResponse
  202. expectation.fulfill()
  203. }
  204. waitForExpectationsWithTimeout(timeout, handler: nil)
  205. // Then
  206. if let response = response {
  207. XCTAssertNotNil(response.request, "request should not be nil")
  208. XCTAssertNotNil(response.response, "response should not be nil")
  209. XCTAssertNotNil(response.data, "data should not be nil")
  210. XCTAssertTrue(response.result.isSuccess, "result should be success")
  211. // The `as NSString` cast is necessary due to a compiler bug. See the following rdar for more info.
  212. // - https://openradar.appspot.com/radar?id=5517037090635776
  213. if let form = response.result.value?["form" as NSString] as? [String: String] {
  214. XCTAssertEqual(form, ["foo": "bar"], "form should match parameters")
  215. } else {
  216. XCTFail("form should not be nil")
  217. }
  218. } else {
  219. XCTFail("response should not be nil")
  220. }
  221. }
  222. }
  223. // MARK: -
  224. class RedirectResponseTestCase: BaseTestCase {
  225. var manager: Alamofire.Manager!
  226. override func setUp() {
  227. super.setUp()
  228. manager = Alamofire.Manager(configuration: NSURLSessionConfiguration.ephemeralSessionConfiguration())
  229. }
  230. override func tearDown() {
  231. super.tearDown()
  232. }
  233. // MARK: Tests
  234. func testThatRequestWillPerformHTTPRedirectionByDefault() {
  235. // Given
  236. let redirectURLString = "https://www.apple.com"
  237. let URLString = "https://httpbin.org/redirect-to?url=\(redirectURLString)"
  238. let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
  239. var request: NSURLRequest?
  240. var response: NSHTTPURLResponse?
  241. var data: NSData?
  242. var error: NSError?
  243. // When
  244. manager.request(.GET, URLString)
  245. .response { responseRequest, responseResponse, responseData, responseError in
  246. request = responseRequest
  247. response = responseResponse
  248. data = responseData
  249. error = responseError
  250. expectation.fulfill()
  251. }
  252. waitForExpectationsWithTimeout(timeout, handler: nil)
  253. // Then
  254. XCTAssertNotNil(request, "request should not be nil")
  255. XCTAssertNotNil(response, "response should not be nil")
  256. XCTAssertNotNil(data, "data should not be nil")
  257. XCTAssertNil(error, "error should be nil")
  258. XCTAssertEqual(response?.URL?.URLString ?? "", redirectURLString, "response URL should match the redirect URL")
  259. XCTAssertEqual(response?.statusCode ?? -1, 200, "response should have a 200 status code")
  260. }
  261. func testThatRequestWillPerformRedirectionMultipleTimesByDefault() {
  262. // Given
  263. let redirectURLString = "https://httpbin.org/get"
  264. let URLString = "https://httpbin.org/redirect/5"
  265. let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
  266. var request: NSURLRequest?
  267. var response: NSHTTPURLResponse?
  268. var data: NSData?
  269. var error: NSError?
  270. // When
  271. manager.request(.GET, URLString)
  272. .response { responseRequest, responseResponse, responseData, responseError in
  273. request = responseRequest
  274. response = responseResponse
  275. data = responseData
  276. error = responseError
  277. expectation.fulfill()
  278. }
  279. waitForExpectationsWithTimeout(timeout, handler: nil)
  280. // Then
  281. XCTAssertNotNil(request, "request should not be nil")
  282. XCTAssertNotNil(response, "response should not be nil")
  283. XCTAssertNotNil(data, "data should not be nil")
  284. XCTAssertNil(error, "error should be nil")
  285. XCTAssertEqual(response?.URL?.URLString ?? "", redirectURLString, "response URL should match the redirect URL")
  286. XCTAssertEqual(response?.statusCode ?? -1, 200, "response should have a 200 status code")
  287. }
  288. func testThatTaskOverrideClosureCanPerformHTTPRedirection() {
  289. // Given
  290. let redirectURLString = "https://www.apple.com"
  291. let URLString = "https://httpbin.org/redirect-to?url=\(redirectURLString)"
  292. let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
  293. let callbackExpectation = expectationWithDescription("Redirect callback should be made")
  294. let delegate: Alamofire.Manager.SessionDelegate = manager.delegate
  295. delegate.taskWillPerformHTTPRedirection = { _, _, _, request in
  296. callbackExpectation.fulfill()
  297. return request
  298. }
  299. var request: NSURLRequest?
  300. var response: NSHTTPURLResponse?
  301. var data: NSData?
  302. var error: NSError?
  303. // When
  304. manager.request(.GET, URLString)
  305. .response { responseRequest, responseResponse, responseData, responseError in
  306. request = responseRequest
  307. response = responseResponse
  308. data = responseData
  309. error = responseError
  310. expectation.fulfill()
  311. }
  312. waitForExpectationsWithTimeout(timeout, handler: nil)
  313. // Then
  314. XCTAssertNotNil(request, "request should not be nil")
  315. XCTAssertNotNil(response, "response should not be nil")
  316. XCTAssertNotNil(data, "data should not be nil")
  317. XCTAssertNil(error, "error should be nil")
  318. XCTAssertEqual(response?.URL?.URLString ?? "", redirectURLString, "response URL should match the redirect URL")
  319. XCTAssertEqual(response?.statusCode ?? -1, 200, "response should have a 200 status code")
  320. }
  321. func testThatTaskOverrideClosureWithCompletionCanPerformHTTPRedirection() {
  322. // Given
  323. let redirectURLString = "https://www.apple.com"
  324. let URLString = "https://httpbin.org/redirect-to?url=\(redirectURLString)"
  325. let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
  326. let callbackExpectation = expectationWithDescription("Redirect callback should be made")
  327. let delegate: Alamofire.Manager.SessionDelegate = manager.delegate
  328. delegate.taskWillPerformHTTPRedirectionWithCompletion = {_, _, _, request, completion in
  329. completion(request)
  330. callbackExpectation.fulfill()
  331. }
  332. var request: NSURLRequest?
  333. var response: NSHTTPURLResponse?
  334. var data: NSData?
  335. var error: NSError?
  336. // When
  337. manager.request(.GET, URLString)
  338. .response { responseRequest, responseResponse, responseData, responseError in
  339. request = responseRequest
  340. response = responseResponse
  341. data = responseData
  342. error = responseError
  343. expectation.fulfill()
  344. }
  345. waitForExpectationsWithTimeout(timeout, handler: nil)
  346. // Then
  347. XCTAssertNotNil(request, "request should not be nil")
  348. XCTAssertNotNil(response, "response should not be nil")
  349. XCTAssertNotNil(data, "data should not be nil")
  350. XCTAssertNil(error, "error should be nil")
  351. XCTAssertEqual(response?.URL?.URLString ?? "", redirectURLString, "response URL should match the redirect URL")
  352. XCTAssertEqual(response?.statusCode ?? -1, 200, "response should have a 200 status code")
  353. }
  354. func testThatTaskOverrideClosureCanCancelHTTPRedirection() {
  355. // Given
  356. let redirectURLString = "https://www.apple.com"
  357. let URLString = "https://httpbin.org/redirect-to?url=\(redirectURLString)"
  358. let expectation = expectationWithDescription("Request should not redirect to \(redirectURLString)")
  359. let callbackExpectation = expectationWithDescription("Redirect callback should be made")
  360. let delegate: Alamofire.Manager.SessionDelegate = manager.delegate
  361. delegate.taskWillPerformHTTPRedirectionWithCompletion = {_, _, _, _, completion in
  362. callbackExpectation.fulfill()
  363. completion(nil)
  364. }
  365. var request: NSURLRequest?
  366. var response: NSHTTPURLResponse?
  367. var data: NSData?
  368. var error: NSError?
  369. // When
  370. manager.request(.GET, URLString)
  371. .response { responseRequest, responseResponse, responseData, responseError in
  372. request = responseRequest
  373. response = responseResponse
  374. data = responseData
  375. error = responseError
  376. expectation.fulfill()
  377. }
  378. waitForExpectationsWithTimeout(timeout, handler: nil)
  379. // Then
  380. XCTAssertNotNil(request, "request should not be nil")
  381. XCTAssertNotNil(response, "response should not be nil")
  382. XCTAssertNotNil(data, "data should not be nil")
  383. XCTAssertNil(error, "error should be nil")
  384. XCTAssertEqual(response?.URL?.URLString ?? "", URLString, "response URL should match the origin URL")
  385. XCTAssertEqual(response?.statusCode ?? -1, 302, "response should have a 302 status code")
  386. }
  387. func testThatTaskOverrideClosureWithCompletionCanCancelHTTPRedirection() {
  388. // Given
  389. let redirectURLString = "https://www.apple.com"
  390. let URLString = "https://httpbin.org/redirect-to?url=\(redirectURLString)"
  391. let expectation = expectationWithDescription("Request should not redirect to \(redirectURLString)")
  392. let callbackExpectation = expectationWithDescription("Redirect callback should be made")
  393. let delegate: Alamofire.Manager.SessionDelegate = manager.delegate
  394. delegate.taskWillPerformHTTPRedirection = { _, _, _, _ in
  395. callbackExpectation.fulfill()
  396. return nil
  397. }
  398. var request: NSURLRequest?
  399. var response: NSHTTPURLResponse?
  400. var data: NSData?
  401. var error: NSError?
  402. // When
  403. manager.request(.GET, URLString)
  404. .response { responseRequest, responseResponse, responseData, responseError in
  405. request = responseRequest
  406. response = responseResponse
  407. data = responseData
  408. error = responseError
  409. expectation.fulfill()
  410. }
  411. waitForExpectationsWithTimeout(timeout, handler: nil)
  412. // Then
  413. XCTAssertNotNil(request, "request should not be nil")
  414. XCTAssertNotNil(response, "response should not be nil")
  415. XCTAssertNotNil(data, "data should not be nil")
  416. XCTAssertNil(error, "error should be nil")
  417. XCTAssertEqual(response?.URL?.URLString ?? "", URLString, "response URL should match the origin URL")
  418. XCTAssertEqual(response?.statusCode ?? -1, 302, "response should have a 302 status code")
  419. }
  420. func testThatTaskOverrideClosureIsCalledMultipleTimesForMultipleHTTPRedirects() {
  421. // Given
  422. let redirectURLString = "https://httpbin.org/get"
  423. let URLString = "https://httpbin.org/redirect/5"
  424. let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
  425. let delegate: Alamofire.Manager.SessionDelegate = manager.delegate
  426. var totalRedirectCount = 0
  427. delegate.taskWillPerformHTTPRedirection = { _, _, _, request in
  428. totalRedirectCount += 1
  429. return request
  430. }
  431. var request: NSURLRequest?
  432. var response: NSHTTPURLResponse?
  433. var data: NSData?
  434. var error: NSError?
  435. // When
  436. manager.request(.GET, URLString)
  437. .response { responseRequest, responseResponse, responseData, responseError in
  438. request = responseRequest
  439. response = responseResponse
  440. data = responseData
  441. error = responseError
  442. expectation.fulfill()
  443. }
  444. waitForExpectationsWithTimeout(timeout, handler: nil)
  445. // Then
  446. XCTAssertNotNil(request, "request should not be nil")
  447. XCTAssertNotNil(response, "response should not be nil")
  448. XCTAssertNotNil(data, "data should not be nil")
  449. XCTAssertNil(error, "error should be nil")
  450. XCTAssertEqual(response?.URL?.URLString ?? "", redirectURLString, "response URL should match the redirect URL")
  451. XCTAssertEqual(response?.statusCode ?? -1, 200, "response should have a 200 status code")
  452. XCTAssertEqual(totalRedirectCount, 5, "total redirect count should be 5")
  453. }
  454. func testThatTaskOverrideClosureWithCompletionIsCalledMultipleTimesForMultipleHTTPRedirects() {
  455. // Given
  456. let redirectURLString = "https://httpbin.org/get"
  457. let URLString = "https://httpbin.org/redirect/5"
  458. let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
  459. let delegate: Alamofire.Manager.SessionDelegate = manager.delegate
  460. var totalRedirectCount = 0
  461. delegate.taskWillPerformHTTPRedirectionWithCompletion = {_, _, _, request, completion in
  462. totalRedirectCount += 1
  463. completion(request)
  464. }
  465. var request: NSURLRequest?
  466. var response: NSHTTPURLResponse?
  467. var data: NSData?
  468. var error: NSError?
  469. // When
  470. manager.request(.GET, URLString)
  471. .response { responseRequest, responseResponse, responseData, responseError in
  472. request = responseRequest
  473. response = responseResponse
  474. data = responseData
  475. error = responseError
  476. expectation.fulfill()
  477. }
  478. waitForExpectationsWithTimeout(timeout, handler: nil)
  479. // Then
  480. XCTAssertNotNil(request, "request should not be nil")
  481. XCTAssertNotNil(response, "response should not be nil")
  482. XCTAssertNotNil(data, "data should not be nil")
  483. XCTAssertNil(error, "error should be nil")
  484. XCTAssertEqual(response?.URL?.URLString ?? "", redirectURLString, "response URL should match the redirect URL")
  485. XCTAssertEqual(response?.statusCode ?? -1, 200, "response should have a 200 status code")
  486. XCTAssertEqual(totalRedirectCount, 5, "total redirect count should be 5")
  487. }
  488. func testThatRedirectedRequestContainsAllHeadersFromOriginalRequest() {
  489. // Given
  490. let redirectURLString = "https://httpbin.org/get"
  491. let URLString = "https://httpbin.org/redirect-to?url=\(redirectURLString)"
  492. let headers = [
  493. "Authorization": "1234",
  494. "Custom-Header": "foobar",
  495. ]
  496. // NOTE: It appears that most headers are maintained during a redirect with the exception of the `Authorization`
  497. // header. It appears that Apple's strips the `Authorization` header from the redirected URL request. If you
  498. // need to maintain the `Authorization` header, you need to manually append it to the redirected request.
  499. manager.delegate.taskWillPerformHTTPRedirection = { session, task, response, request in
  500. var redirectedRequest = request
  501. if let
  502. originalRequest = task.originalRequest,
  503. headers = originalRequest.allHTTPHeaderFields,
  504. authorizationHeaderValue = headers["Authorization"]
  505. {
  506. let mutableRequest = request.mutableCopy() as! NSMutableURLRequest
  507. mutableRequest.setValue(authorizationHeaderValue, forHTTPHeaderField: "Authorization")
  508. redirectedRequest = mutableRequest
  509. }
  510. return redirectedRequest
  511. }
  512. let expectation = expectationWithDescription("Request should redirect to \(redirectURLString)")
  513. var response: Response<AnyObject, NSError>?
  514. // When
  515. manager.request(.GET, URLString, headers: headers)
  516. .responseJSON { closureResponse in
  517. response = closureResponse
  518. expectation.fulfill()
  519. }
  520. waitForExpectationsWithTimeout(timeout, handler: nil)
  521. // Then
  522. XCTAssertNotNil(response?.request, "request should not be nil")
  523. XCTAssertNotNil(response?.response, "response should not be nil")
  524. XCTAssertNotNil(response?.data, "data should not be nil")
  525. XCTAssertTrue(response?.result.isSuccess ?? false, "response result should be a success")
  526. if let
  527. JSON = response?.result.value as? [String: AnyObject],
  528. headers = JSON["headers"] as? [String: String]
  529. {
  530. XCTAssertEqual(headers["Custom-Header"], "foobar", "Custom-Header should be equal to foobar")
  531. XCTAssertEqual(headers["Authorization"], "1234", "Authorization header should be equal to 1234")
  532. }
  533. }
  534. }