UploadTests.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // UploadTests.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 UploadResponseTestCase: BaseTestCase {
  26. func testUploadRequest() {
  27. // Given
  28. let URLString = "http://httpbin.org/post"
  29. let data = "Lorem ipsum dolor sit amet".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
  30. let expectation = expectationWithDescription("Upload request should succeed: \(URLString)")
  31. var request: NSURLRequest?
  32. var response: NSHTTPURLResponse?
  33. var error: NSError?
  34. // When
  35. Alamofire.upload(.POST, URLString: URLString, data: data)
  36. .response { responseRequest, responseResponse, _, responseError in
  37. request = responseRequest
  38. response = responseResponse
  39. error = responseError
  40. expectation.fulfill()
  41. }
  42. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  43. // Then
  44. XCTAssertNotNil(request, "request should not be nil")
  45. XCTAssertNotNil(response, "response should not be nil")
  46. XCTAssertNil(error, "error should be nil")
  47. }
  48. func testUploadRequestWithProgress() {
  49. // Given
  50. let URLString = "http://httpbin.org/post"
  51. let data: NSData = {
  52. var text = ""
  53. for _ in 1...3_000 {
  54. text += "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
  55. }
  56. return text.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
  57. }()
  58. let expectation = expectationWithDescription("Bytes upload progress should be reported: \(URLString)")
  59. var byteValues: [(bytes: Int64, totalBytes: Int64, totalBytesExpected: Int64)] = []
  60. var progressValues: [(completedUnitCount: Int64, totalUnitCount: Int64)] = []
  61. var responseRequest: NSURLRequest?
  62. var responseResponse: NSHTTPURLResponse?
  63. var responseData: AnyObject?
  64. var responseError: NSError?
  65. // When
  66. let upload = Alamofire.upload(.POST, URLString: URLString, data: data)
  67. upload.progress { bytesWritten, totalBytesWritten, totalBytesExpectedToWrite in
  68. let bytes = (bytes: bytesWritten, totalBytes: totalBytesWritten, totalBytesExpected: totalBytesExpectedToWrite)
  69. byteValues.append(bytes)
  70. let progress = (completedUnitCount: upload.progress.completedUnitCount, totalUnitCount: upload.progress.totalUnitCount)
  71. progressValues.append(progress)
  72. }
  73. upload.response { request, response, data, error in
  74. responseRequest = request
  75. responseResponse = response
  76. responseData = data
  77. responseError = error
  78. expectation.fulfill()
  79. }
  80. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  81. // Then
  82. XCTAssertNotNil(responseRequest, "response request should not be nil")
  83. XCTAssertNotNil(responseResponse, "response response should not be nil")
  84. XCTAssertNotNil(responseData, "response data should not be nil")
  85. XCTAssertNil(responseError, "response error should be nil")
  86. XCTAssertEqual(byteValues.count, progressValues.count, "byteValues count should equal progressValues count")
  87. if byteValues.count == progressValues.count {
  88. for index in 0..<byteValues.count {
  89. let byteValue = byteValues[index]
  90. let progressValue = progressValues[index]
  91. XCTAssertGreaterThan(byteValue.bytes, 0, "reported bytes should always be greater than 0")
  92. XCTAssertEqual(byteValue.totalBytes, progressValue.completedUnitCount, "total bytes should be equal to completed unit count")
  93. XCTAssertEqual(byteValue.totalBytesExpected, progressValue.totalUnitCount, "total bytes expected should be equal to total unit count")
  94. }
  95. }
  96. if let lastByteValue = byteValues.last,
  97. lastProgressValue = progressValues.last
  98. {
  99. let byteValueFractionalCompletion = Double(lastByteValue.totalBytes) / Double(lastByteValue.totalBytesExpected)
  100. let progressValueFractionalCompletion = Double(lastProgressValue.0) / Double(lastProgressValue.1)
  101. XCTAssertEqual(byteValueFractionalCompletion, 1.0, "byte value fractional completion should equal 1.0")
  102. XCTAssertEqual(progressValueFractionalCompletion, 1.0, "progress value fractional completion should equal 1.0")
  103. } else {
  104. XCTFail("last item in bytesValues and progressValues should not be nil")
  105. }
  106. }
  107. }