ResponseTests.swift 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // ResponseTests.swift
  2. //
  3. // Copyright (c) 2014–2015 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 JSONResponseTestCase: BaseTestCase {
  26. func testGETRequestJSONResponse() {
  27. // Given
  28. let URL = "http://httpbin.org/get"
  29. let expectation = expectationWithDescription("\(URL)")
  30. var request: NSURLRequest?
  31. var response: NSHTTPURLResponse?
  32. var JSON: AnyObject?
  33. var error: NSError?
  34. // When
  35. Alamofire.request(.GET, URL, parameters: ["foo": "bar"])
  36. .responseJSON { responseRequest, responseResponse, responseJSON, responseError in
  37. request = responseRequest
  38. response = responseResponse
  39. JSON = responseJSON
  40. error = responseError
  41. expectation.fulfill()
  42. }
  43. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  44. // Then
  45. XCTAssertNotNil(request, "request should not be nil")
  46. XCTAssertNotNil(response, "response should not be nil")
  47. XCTAssertNotNil(JSON, "JSON should not be nil")
  48. XCTAssertNil(error, "error should be nil")
  49. if let args = JSON?["args"] as? NSObject {
  50. XCTAssertEqual(args, ["foo": "bar"], "args should match parameters")
  51. } else {
  52. XCTFail("args should not be nil")
  53. }
  54. }
  55. func testPOSTRequestJSONResponse() {
  56. // Given
  57. let URL = "http://httpbin.org/post"
  58. let expectation = expectationWithDescription("\(URL)")
  59. var request: NSURLRequest?
  60. var response: NSHTTPURLResponse?
  61. var JSON: AnyObject?
  62. var error: NSError?
  63. // When
  64. Alamofire.request(.POST, URL, parameters: ["foo": "bar"])
  65. .responseJSON { responseRequest, responseResponse, responseJSON, responseError in
  66. request = responseRequest
  67. response = responseResponse
  68. JSON = responseJSON
  69. error = responseError
  70. expectation.fulfill()
  71. }
  72. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  73. // Then
  74. XCTAssertNotNil(request, "request should not be nil")
  75. XCTAssertNotNil(response, "response should not be nil")
  76. XCTAssertNotNil(JSON, "JSON should not be nil")
  77. XCTAssertNil(error, "error should be nil")
  78. if let form = JSON?["form"] as? NSObject {
  79. XCTAssertEqual(form, ["foo": "bar"], "form should match parameters")
  80. } else {
  81. XCTFail("form should not be nil")
  82. }
  83. }
  84. }
  85. // MARK: -
  86. class RedirectResponseTestCase: BaseTestCase {
  87. func testGETRequestRedirectResponse() {
  88. // Given
  89. let URLString = "http://google.com"
  90. let delegate: Alamofire.Manager.SessionDelegate = Alamofire.Manager.sharedInstance.delegate
  91. delegate.taskWillPerformHTTPRedirection = { session, task, response, request in
  92. // Accept the redirect by returning the updated request.
  93. return request
  94. }
  95. let expectation = expectationWithDescription("\(URLString)")
  96. var request: NSURLRequest?
  97. var response: NSHTTPURLResponse?
  98. var data: AnyObject?
  99. var error: NSError?
  100. // When
  101. Alamofire.request(.GET, URLString)
  102. .response { responseRequest, responseResponse, responseData, responseError in
  103. request = responseRequest
  104. response = responseResponse
  105. data = responseData
  106. error = responseError
  107. expectation.fulfill()
  108. }
  109. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  110. // Then
  111. XCTAssertNotNil(request, "request should not be nil")
  112. XCTAssertNotNil(response, "response should not be nil")
  113. XCTAssertNotNil(data, "data should not be nil")
  114. XCTAssertNil(error, "error should be nil")
  115. XCTAssertEqual(response?.URL ?? NSURL(), NSURL(string: "http://www.google.com/")!, "request should have followed a redirect")
  116. XCTAssertEqual(response?.statusCode ?? -1, 200, "response should have a 200 status code")
  117. }
  118. func testGETRequestDisallowRedirectResponse() {
  119. // Given
  120. let URLString = "http://google.com/"
  121. let delegate: Alamofire.Manager.SessionDelegate = Alamofire.Manager.sharedInstance.delegate
  122. delegate.taskWillPerformHTTPRedirection = { session, task, response, request in
  123. // Disallow redirects by returning nil.
  124. // NOTE: NSURLSessionDelegate's `URLSession:task:willPerformHTTPRedirection:newRequest:completionHandler:`
  125. // suggests that returning nil should refuse the redirect, but this causes a deadlock/timeout.
  126. return NSURLRequest(URL: NSURL(string: URLString)!)
  127. }
  128. let expectation = expectationWithDescription("\(URLString)")
  129. var request: NSURLRequest?
  130. var response: NSHTTPURLResponse?
  131. var data: AnyObject?
  132. var error: NSError?
  133. // When
  134. Alamofire.request(.GET, URLString)
  135. .response { responseRequest, responseResponse, responseData, responseError in
  136. request = responseRequest
  137. response = responseResponse
  138. data = responseData
  139. error = responseError
  140. expectation.fulfill()
  141. }
  142. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  143. // Then
  144. XCTAssertNotNil(request, "request should not be nil")
  145. XCTAssertNotNil(response, "response should not be nil")
  146. XCTAssertNotNil(data, "data should not be nil")
  147. XCTAssertNil(error, "error should be nil")
  148. XCTAssertEqual(response?.URL ?? NSURL(string: "")!, NSURL(string: URLString)!, "request should not have followed a redirect")
  149. XCTAssertEqual(response?.statusCode ?? -1, 301, "response should have a 301 status code")
  150. }
  151. }