InternalRequestTests.swift 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //
  2. // InternalRequestTests.swift
  3. //
  4. // Copyright (c) 2020 Alamofire Software Foundation (http://alamofire.org/)
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. @testable import Alamofire
  25. import XCTest
  26. final class InternalRequestTests: BaseTestCase {
  27. @MainActor
  28. func testThatMultipleFinishInvocationsDoNotCallSerializersMoreThanOnce() {
  29. // Given
  30. let session = Session(rootQueue: .main, startRequestsImmediately: false)
  31. let expect = expectation(description: "request complete")
  32. var response: DataResponse<Data?, AFError>?
  33. // When
  34. let request = session.request(.get).response { resp in
  35. response = resp
  36. expect.fulfill()
  37. }
  38. for _ in 0..<100 {
  39. request.finish()
  40. }
  41. waitForExpectations(timeout: timeout)
  42. // Then
  43. XCTAssertNotNil(response)
  44. }
  45. #if canImport(zlib) && !os(Android) // Match RequestCompression support.
  46. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  47. func testThatRequestCompressorProperlyCalculatesAdler32() {
  48. // Given
  49. let compressor = DeflateRequestCompressor()
  50. // When
  51. let checksum = compressor.adler32Checksum(of: Data("Wikipedia".utf8))
  52. // Then
  53. // From https://en.wikipedia.org/wiki/Adler-32
  54. XCTAssertEqual(checksum, 300_286_872)
  55. }
  56. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  57. func testThatRequestCompressorDeflatesDataCorrectly() throws {
  58. // Given
  59. let compressor = DeflateRequestCompressor()
  60. // When
  61. let compressedData = try compressor.deflate(Data([0]))
  62. // Then
  63. XCTAssertEqual(compressedData, Data([0x78, 0x5E, 0x63, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01]))
  64. }
  65. #endif
  66. }