RequestTests.swift 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // RequestTests.swift
  2. //
  3. // Copyright (c) 2014 Alamofire (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 Foundation
  23. import Alamofire
  24. import XCTest
  25. class AlamofireRequestInitializationTestCase: XCTestCase {
  26. func testRequestClassMethodWithMethodAndURL() {
  27. let URL = "http://httpbin.org/"
  28. let request = Alamofire.request(.GET, URL)
  29. XCTAssertNotNil(request.request, "request should not be nil")
  30. XCTAssertEqual(request.request.URL!, NSURL(string: URL)!, "request URL should be equal")
  31. XCTAssertNil(request.response, "response should be nil")
  32. }
  33. func testRequestClassMethodWithMethodAndURLAndParameters() {
  34. let URL = "http://httpbin.org/get"
  35. let request = Alamofire.request(.GET, URL, parameters: ["foo": "bar"])
  36. XCTAssertNotNil(request.request, "request should not be nil")
  37. XCTAssertNotEqual(request.request.URL!, NSURL(string: URL)!, "request URL should be equal")
  38. XCTAssertEqual(request.request.URL!.query!, "foo=bar", "query is incorrect")
  39. XCTAssertNil(request.response, "response should be nil")
  40. }
  41. }
  42. class AlamofireRequestResponseTestCase: XCTestCase {
  43. func testRequestResponse() {
  44. let URL = "http://httpbin.org/get"
  45. let serializer = Alamofire.Request.stringResponseSerializer(encoding: NSUTF8StringEncoding)
  46. let expectation = expectationWithDescription("\(URL)")
  47. Alamofire.request(.GET, URL, parameters: ["foo": "bar"])
  48. .response(serializer: serializer){ (request, response, string, error) in
  49. XCTAssertNotNil(request, "request should not be nil")
  50. XCTAssertNotNil(response, "response should not be nil")
  51. XCTAssertNotNil(string, "string should not be nil")
  52. XCTAssertNil(error, "error should be nil")
  53. expectation.fulfill()
  54. }
  55. waitForExpectationsWithTimeout(10) { (error) in
  56. XCTAssertNil(error, "\(error)")
  57. }
  58. }
  59. }
  60. class AlamofireRequestDescriptionTestCase: XCTestCase {
  61. func testRequestDescription() {
  62. let URL = "http://httpbin.org/get"
  63. let request = Alamofire.request(.GET, URL)
  64. XCTAssertEqual(request.description, "GET http://httpbin.org/get", "incorrect request description")
  65. let expectation = expectationWithDescription("\(URL)")
  66. request.response { (_, response,_,_) in
  67. XCTAssertEqual(request.description, "GET http://httpbin.org/get (\(response!.statusCode))", "incorrect request description")
  68. expectation.fulfill()
  69. }
  70. waitForExpectationsWithTimeout(10) { (error) in
  71. XCTAssertNil(error, "\(error)")
  72. }
  73. }
  74. }
  75. class AlamofireRequestDebugDescriptionTestCase: XCTestCase {
  76. let manager: Alamofire.Manager = {
  77. let manager = Alamofire.Manager(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
  78. manager.startRequestsImmediately = false
  79. return manager
  80. }()
  81. // MARK: -
  82. func testGETRequestDebugDescription() {
  83. let URL = "http://httpbin.org/get"
  84. let request = manager.request(.GET, URL)
  85. let components = cURLCommandComponents(request)
  86. XCTAssert(components[0..<3] == ["$", "curl", "-i"], "components should be equal")
  87. XCTAssert(!contains(components, "-X"), "command should not contain explicit -X flag")
  88. XCTAssert(components.last! == "\"\(URL)\"", "URL component should be equal")
  89. }
  90. func testPOSTRequestDebugDescription() {
  91. let URL = "http://httpbin.org/post"
  92. let request = manager.request(.POST, URL)
  93. let components = cURLCommandComponents(request)
  94. XCTAssert(components[0..<3] == ["$", "curl", "-i"], "components should be equal")
  95. XCTAssert(components[3..<5] == ["-X", "POST"], "command should contain explicit -X flag")
  96. XCTAssert(components.last! == "\"\(URL)\"", "URL component should be equal")
  97. }
  98. func testPOSTRequestWithJSONParametersDebugDescription() {
  99. let URL = "http://httpbin.org/post"
  100. let request = manager.request(.POST, URL, parameters: ["foo": "bar"], encoding: .JSON)
  101. let components = cURLCommandComponents(request)
  102. XCTAssert(components[0..<3] == ["$", "curl", "-i"], "components should be equal")
  103. XCTAssert(components[3..<5] == ["-X", "POST"], "command should contain explicit -X flag")
  104. XCTAssert(request.debugDescription.rangeOfString("-H \"Content-Type: application/json\"") != nil)
  105. XCTAssert(request.debugDescription.rangeOfString("-d \"{\\\"foo\\\":\\\"bar\\\"}\"") != nil)
  106. XCTAssert(components.last! == "\"\(URL)\"", "URL component should be equal")
  107. }
  108. // Temporarily disabled on OS X due to build failure for CocoaPods
  109. // See https://github.com/CocoaPods/swift/issues/24
  110. #if !os(OSX)
  111. func testPOSTRequestWithCookieDebugDescription() {
  112. let URL = "http://httpbin.org/post"
  113. let properties = [
  114. NSHTTPCookieDomain: "httpbin.org",
  115. NSHTTPCookiePath: "/post",
  116. NSHTTPCookieName: "foo",
  117. NSHTTPCookieValue: "bar",
  118. ]
  119. let cookie = NSHTTPCookie(properties: properties)!
  120. manager.session.configuration.HTTPCookieStorage?.setCookie(cookie)
  121. let request = manager.request(.POST, URL)
  122. let components = cURLCommandComponents(request)
  123. XCTAssert(components[0..<3] == ["$", "curl", "-i"], "components should be equal")
  124. XCTAssert(components[3..<5] == ["-X", "POST"], "command should contain explicit -X flag")
  125. XCTAssert(components[5..<6] == ["-b"], "command should contain -b flag")
  126. XCTAssert(components.last! == "\"\(URL)\"", "URL component should be equal")
  127. }
  128. #endif
  129. // MARK: -
  130. private func cURLCommandComponents(request: Request) -> [String] {
  131. return request.debugDescription.componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).filter { $0 != "" && $0 != "\\" }
  132. }
  133. }