DownloadTests.swift 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // DownloadTests.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 DownloadResponseTestCase: BaseTestCase {
  26. // MARK: - Properties
  27. let searchPathDirectory: NSSearchPathDirectory = .CachesDirectory
  28. let searchPathDomain: NSSearchPathDomainMask = .UserDomainMask
  29. // MARK: - Tests
  30. func testDownloadRequest() {
  31. // Given
  32. let numberOfLines = 100
  33. let URLString = "http://httpbin.org/stream/\(numberOfLines)"
  34. let destination = Alamofire.Request.suggestedDownloadDestination(directory: searchPathDirectory, domain: searchPathDomain)
  35. let expectation = expectationWithDescription("Download request should download data to file: \(URLString)")
  36. var request: NSURLRequest?
  37. var response: NSHTTPURLResponse?
  38. var error: NSError?
  39. // When
  40. Alamofire.download(.GET, URLString: URLString, destination: destination)
  41. .response { responseRequest, responseResponse, _, responseError in
  42. request = responseRequest
  43. response = responseResponse
  44. error = responseError
  45. expectation.fulfill()
  46. }
  47. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  48. // Then
  49. XCTAssertNotNil(request, "request should not be nil")
  50. XCTAssertNotNil(response, "response should not be nil")
  51. XCTAssertNil(error, "error should be nil")
  52. let fileManager = NSFileManager.defaultManager()
  53. let directory = fileManager.URLsForDirectory(self.searchPathDirectory, inDomains: self.searchPathDomain)[0]
  54. do {
  55. let contents = try fileManager.contentsOfDirectoryAtURL(directory, includingPropertiesForKeys: nil, options: .SkipsHiddenFiles)
  56. #if os(iOS)
  57. let suggestedFilename = "\(numberOfLines)"
  58. #elseif os(OSX)
  59. let suggestedFilename = "\(numberOfLines).json"
  60. #endif
  61. let predicate = NSPredicate(format: "lastPathComponent = '\(suggestedFilename)'")
  62. let filteredContents = (contents as NSArray).filteredArrayUsingPredicate(predicate)
  63. XCTAssertEqual(filteredContents.count, 1, "should have one file in Documents")
  64. if let file = filteredContents.first as? NSURL {
  65. XCTAssertEqual(file.lastPathComponent ?? "", "\(suggestedFilename)", "filename should be \(suggestedFilename)")
  66. if let data = NSData(contentsOfURL: file) {
  67. XCTAssertGreaterThan(data.length, 0, "data length should be non-zero")
  68. } else {
  69. XCTFail("data should exist for contents of URL")
  70. }
  71. do {
  72. try fileManager.removeItemAtURL(file)
  73. } catch {
  74. XCTFail("file manager should remove item at URL: \(file)")
  75. }
  76. } else {
  77. XCTFail("file should not be nil")
  78. }
  79. } catch {
  80. XCTFail("contents should not be nil")
  81. }
  82. }
  83. func testDownloadRequestWithProgress() {
  84. // Given
  85. let randomBytes = 4 * 1024 * 1024
  86. let URLString = "http://httpbin.org/bytes/\(randomBytes)"
  87. let fileManager = NSFileManager.defaultManager()
  88. let directory = fileManager.URLsForDirectory(self.searchPathDirectory, inDomains: self.searchPathDomain)[0]
  89. let filename = "test_download_data"
  90. let fileURL = directory.URLByAppendingPathComponent(filename)
  91. let expectation = expectationWithDescription("Bytes download progress should be reported: \(URLString)")
  92. var byteValues: [(bytes: Int64, totalBytes: Int64, totalBytesExpected: Int64)] = []
  93. var progressValues: [(completedUnitCount: Int64, totalUnitCount: Int64)] = []
  94. var responseRequest: NSURLRequest?
  95. var responseResponse: NSHTTPURLResponse?
  96. var responseData: AnyObject?
  97. var responseError: NSError?
  98. // When
  99. let download = Alamofire.download(.GET, URLString: URLString) { _, _ in
  100. return fileURL
  101. }
  102. download.progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
  103. let bytes = (bytes: bytesRead, totalBytes: totalBytesRead, totalBytesExpected: totalBytesExpectedToRead)
  104. byteValues.append(bytes)
  105. let progress = (completedUnitCount: download.progress.completedUnitCount, totalUnitCount: download.progress.totalUnitCount)
  106. progressValues.append(progress)
  107. }
  108. download.response { request, response, data, error in
  109. responseRequest = request
  110. responseResponse = response
  111. responseData = data
  112. responseError = error
  113. expectation.fulfill()
  114. }
  115. waitForExpectationsWithTimeout(self.defaultTimeout, handler: nil)
  116. // Then
  117. XCTAssertNotNil(responseRequest, "response request should not be nil")
  118. XCTAssertNotNil(responseResponse, "response response should not be nil")
  119. XCTAssertNil(responseData, "response data should be nil")
  120. XCTAssertNil(responseError, "response error should be nil")
  121. XCTAssertEqual(byteValues.count, progressValues.count, "byteValues count should equal progressValues count")
  122. if byteValues.count == progressValues.count {
  123. for index in 0..<byteValues.count {
  124. let byteValue = byteValues[index]
  125. let progressValue = progressValues[index]
  126. XCTAssertGreaterThan(byteValue.bytes, 0, "reported bytes should always be greater than 0")
  127. XCTAssertEqual(byteValue.totalBytes, progressValue.completedUnitCount, "total bytes should be equal to completed unit count")
  128. XCTAssertEqual(byteValue.totalBytesExpected, progressValue.totalUnitCount, "total bytes expected should be equal to total unit count")
  129. }
  130. }
  131. if let lastByteValue = byteValues.last,
  132. lastProgressValue = progressValues.last
  133. {
  134. let byteValueFractionalCompletion = Double(lastByteValue.totalBytes) / Double(lastByteValue.totalBytesExpected)
  135. let progressValueFractionalCompletion = Double(lastProgressValue.0) / Double(lastProgressValue.1)
  136. XCTAssertEqual(byteValueFractionalCompletion, 1.0, "byte value fractional completion should equal 1.0")
  137. XCTAssertEqual(progressValueFractionalCompletion, 1.0, "progress value fractional completion should equal 1.0")
  138. } else {
  139. XCTFail("last item in bytesValues and progressValues should not be nil")
  140. }
  141. do {
  142. try fileManager.removeItemAtURL(fileURL)
  143. } catch {
  144. XCTFail("file manager should remove item at URL: \(fileURL)")
  145. }
  146. }
  147. }