RequestTests.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 XCTest
  24. extension Alamofire {
  25. struct RequestTests {
  26. class RequestInitializationTestCase: XCTestCase {
  27. func testRequestClassMethodWithMethodAndURL() {
  28. let URL = "http://httpbin.org/"
  29. let request = Alamofire.request(.GET, URL)
  30. XCTAssertNotNil(request.request, "request should not be nil")
  31. XCTAssertEqual(request.request.URL!, NSURL(string: URL), "request URL should be equal")
  32. XCTAssertNil(request.response, "response should be nil")
  33. }
  34. func testRequestClassMethodWithMethodAndURLAndParameters() {
  35. let URL = "http://httpbin.org/get"
  36. let request = Alamofire.request(.GET, URL, parameters: ["foo": "bar"])
  37. XCTAssertNotNil(request.request, "request should not be nil")
  38. XCTAssertNotEqual(request.request.URL, NSURL(string: URL), "request URL should be equal")
  39. XCTAssertEqual(request.request.URL.query!, "foo=bar", "query is incorrect")
  40. XCTAssertNil(request.response, "response should be nil")
  41. }
  42. }
  43. class RequestResponseTestCase: XCTestCase {
  44. func testRequestResponse() {
  45. let URL = "http://httpbin.org/get"
  46. let serializer = Alamofire.Request.stringResponseSerializer(encoding: NSUTF8StringEncoding)
  47. let expectation = expectationWithDescription("\(URL)")
  48. Alamofire.request(.GET, URL, parameters: ["foo": "bar"])
  49. .response(serializer: serializer){ (request, response, string, error) in
  50. expectation.fulfill()
  51. XCTAssertNotNil(request, "request should not be nil")
  52. XCTAssertNotNil(response, "response should not be nil")
  53. XCTAssertNotNil(string, "string should not be nil")
  54. XCTAssertNil(error, "error should be nil")
  55. }
  56. waitForExpectationsWithTimeout(10){ error in
  57. XCTAssertNil(error, "\(error)")
  58. }
  59. }
  60. }
  61. }
  62. }