1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
class TestCachedURLResponse : XCTestCase {
func test_copy() throws {
let url = try XCTUnwrap(URL(string: "http://example.com/"))
let response = URLResponse(url: url, mimeType: nil, expectedContentLength: -1, textEncodingName: nil)
let bytes: [UInt8] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
let data = Data(bytes: bytes, count: bytes.count)
let userInfo: [AnyHashable: Any] = ["Key1": "Value1", "Key2": "Value2"]
let storagePolicy = URLCache.StoragePolicy.allowedInMemoryOnly
let cachedResponse = CachedURLResponse(response: response, data: data, userInfo: userInfo, storagePolicy: storagePolicy)
let copiedResponse = cachedResponse.copy() as! CachedURLResponse
XCTAssertEqual(cachedResponse.response, copiedResponse.response)
XCTAssertEqual(cachedResponse.data, copiedResponse.data)
XCTAssertEqual(cachedResponse.userInfo?.keys, copiedResponse.userInfo?.keys)
XCTAssertEqual(cachedResponse.storagePolicy, copiedResponse.storagePolicy)
}
func test_initDefaultUserInfoAndStoragePolicy() throws {
let url = try XCTUnwrap(URL(string: "http://example.com/"))
let response = URLResponse(url: url, mimeType: nil, expectedContentLength: -1, textEncodingName: nil)
let bytes: [UInt8] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
let data = Data(bytes: bytes, count: bytes.count)
let cachedResponse = CachedURLResponse(response: response, data: data)
XCTAssertEqual(response, cachedResponse.response)
XCTAssertEqual(data, cachedResponse.data)
XCTAssertNil(cachedResponse.userInfo)
XCTAssertEqual(.allowed, cachedResponse.storagePolicy)
}
func test_initDefaultUserInfo() throws {
let url = try XCTUnwrap(URL(string: "http://example.com/"))
let response = URLResponse(url: url, mimeType: nil, expectedContentLength: -1, textEncodingName: nil)
let bytes: [UInt8] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
let data = Data(bytes: bytes, count: bytes.count)
let storagePolicy = URLCache.StoragePolicy.allowedInMemoryOnly
let cachedResponse = CachedURLResponse(response: response, data: data, storagePolicy: storagePolicy)
XCTAssertEqual(response, cachedResponse.response)
XCTAssertEqual(data, cachedResponse.data)
XCTAssertNil(cachedResponse.userInfo)
XCTAssertEqual(storagePolicy, cachedResponse.storagePolicy)
}
func test_initWithoutDefaults() throws {
let url = try XCTUnwrap(URL(string: "http://example.com/"))
let response = URLResponse(url: url, mimeType: nil, expectedContentLength: -1, textEncodingName: nil)
let bytes: [UInt8] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
let data = Data(bytes: bytes, count: bytes.count)
let userInfo: [AnyHashable: Any] = ["Key1": "Value1", "Key2": "Value2"]
let storagePolicy = URLCache.StoragePolicy.allowedInMemoryOnly
let cachedResponse = CachedURLResponse(response: response, data: data, userInfo: userInfo, storagePolicy: storagePolicy)
XCTAssertEqual(response, cachedResponse.response)
XCTAssertEqual(data, cachedResponse.data)
XCTAssertEqual(userInfo.keys, cachedResponse.userInfo?.keys)
XCTAssertEqual(storagePolicy, cachedResponse.storagePolicy)
}
func test_equalWithTheSameInstance() throws {
let url = try XCTUnwrap(URL(string: "http://example.com/"))
let response = URLResponse(url: url, mimeType: nil, expectedContentLength: -1, textEncodingName: nil)
let bytes: [UInt8] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
let data = Data(bytes: bytes, count: bytes.count)
let userInfo: [AnyHashable: Any] = ["Key1": "Value1", "Key2": "Value2"]
let storagePolicy = URLCache.StoragePolicy.allowedInMemoryOnly
let cachedResponse = CachedURLResponse(response: response, data: data, userInfo: userInfo, storagePolicy: storagePolicy)
XCTAssertTrue(cachedResponse.isEqual(cachedResponse))
}
func test_equalWithUnrelatedObject() throws {
let url = try XCTUnwrap(URL(string: "http://example.com/"))
let response = URLResponse(url: url, mimeType: nil, expectedContentLength: -1, textEncodingName: nil)
let bytes: [UInt8] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
let data = Data(bytes: bytes, count: bytes.count)
let userInfo: [AnyHashable: Any] = ["Key1": "Value1", "Key2": "Value2"]
let storagePolicy = URLCache.StoragePolicy.allowedInMemoryOnly
let cachedResponse = CachedURLResponse(response: response, data: data, userInfo: userInfo, storagePolicy: storagePolicy)
XCTAssertFalse(cachedResponse.isEqual(NSObject()))
}
func test_equalCheckingResponse() throws {
let url1 = try XCTUnwrap(URL(string: "http://example.com/"))
let response1 = URLResponse(url: url1, mimeType: nil, expectedContentLength: -1, textEncodingName: nil)
let bytes: [UInt8] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
let data = Data(bytes: bytes, count: bytes.count)
let userInfo: [AnyHashable: Any] = ["Key1": "Value1", "Key2": "Value2"]
let storagePolicy = URLCache.StoragePolicy.allowedInMemoryOnly
let cachedResponse1 = CachedURLResponse(response: response1, data: data, userInfo: userInfo, storagePolicy: storagePolicy)
let url2 = try XCTUnwrap(URL(string: "http://example.com/second"))
let response2 = URLResponse(url: url2, mimeType: nil, expectedContentLength: -1, textEncodingName: nil)
let cachedResponse2 = CachedURLResponse(response: response2, data: data, userInfo: userInfo, storagePolicy: storagePolicy)
let url3 = try XCTUnwrap(URL(string: "http://example.com/"))
let response3 = URLResponse(url: url3, mimeType: nil, expectedContentLength: -1, textEncodingName: nil)
let cachedResponse3 = CachedURLResponse(response: response3, data: data, userInfo: userInfo, storagePolicy: storagePolicy)
XCTAssertFalse(cachedResponse1.isEqual(cachedResponse2))
XCTAssertFalse(cachedResponse2.isEqual(cachedResponse1))
XCTAssertTrue(cachedResponse1.isEqual(cachedResponse3))
XCTAssertTrue(cachedResponse3.isEqual(cachedResponse1))
}
func test_equalCheckingData() throws {
let url = try XCTUnwrap(URL(string: "http://example.com/"))
let response = URLResponse(url: url, mimeType: nil, expectedContentLength: -1, textEncodingName: nil)
let bytes1: [UInt8] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
let data1 = Data(bytes: bytes1, count: bytes1.count)
let userInfo: [AnyHashable: Any] = ["Key1": "Value1", "Key2": "Value2"]
let storagePolicy = URLCache.StoragePolicy.allowedInMemoryOnly
let cachedResponse1 = CachedURLResponse(response: response, data: data1, userInfo: userInfo, storagePolicy: storagePolicy)
let bytes2: [UInt8] = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
let data2 = Data(bytes: bytes2, count: bytes2.count)
let cachedResponse2 = CachedURLResponse(response: response, data: data2, userInfo: userInfo, storagePolicy: storagePolicy)
let bytes3: [UInt8] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
let data3 = Data(bytes: bytes3, count: bytes3.count)
let cachedResponse3 = CachedURLResponse(response: response, data: data3, userInfo: userInfo, storagePolicy: storagePolicy)
XCTAssertFalse(cachedResponse1.isEqual(cachedResponse2))
XCTAssertFalse(cachedResponse2.isEqual(cachedResponse1))
XCTAssertTrue(cachedResponse1.isEqual(cachedResponse3))
XCTAssertTrue(cachedResponse3.isEqual(cachedResponse1))
}
func test_equalCheckingStoragePolicy() throws {
let url = try XCTUnwrap(URL(string: "http://example.com/"))
let response = URLResponse(url: url, mimeType: nil, expectedContentLength: -1, textEncodingName: nil)
let bytes: [UInt8] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
let data = Data(bytes: bytes, count: bytes.count)
let userInfo: [AnyHashable: Any] = ["Key1": "Value1", "Key2": "Value2"]
let storagePolicy1 = URLCache.StoragePolicy.allowedInMemoryOnly
let cachedResponse1 = CachedURLResponse(response: response, data: data, userInfo: userInfo, storagePolicy: storagePolicy1)
let storagePolicy2 = URLCache.StoragePolicy.notAllowed
let cachedResponse2 = CachedURLResponse(response: response, data: data, userInfo: userInfo, storagePolicy: storagePolicy2)
let storagePolicy3 = URLCache.StoragePolicy.allowedInMemoryOnly
let cachedResponse3 = CachedURLResponse(response: response, data: data, userInfo: userInfo, storagePolicy: storagePolicy3)
XCTAssertFalse(cachedResponse1.isEqual(cachedResponse2))
XCTAssertFalse(cachedResponse2.isEqual(cachedResponse1))
XCTAssertTrue(cachedResponse1.isEqual(cachedResponse3))
XCTAssertTrue(cachedResponse3.isEqual(cachedResponse1))
}
func test_hash() throws {
let url1 = try XCTUnwrap(URL(string: "http://example.com/"))
let response1 = URLResponse(url: url1, mimeType: nil, expectedContentLength: -1, textEncodingName: nil)
let bytes1: [UInt8] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
let data1 = Data(bytes: bytes1, count: bytes1.count)
let userInfo1: [AnyHashable: Any] = ["Key1": "Value1", "Key2": "Value2"]
let storagePolicy1 = URLCache.StoragePolicy.allowedInMemoryOnly
let cachedResponse1 = CachedURLResponse(response: response1, data: data1, userInfo: userInfo1, storagePolicy: storagePolicy1)
let url2 = try XCTUnwrap(URL(string: "http://example.com/"))
let response2 = URLResponse(url: url2, mimeType: nil, expectedContentLength: -1, textEncodingName: nil)
let bytes2: [UInt8] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
let data2 = Data(bytes: bytes2, count: bytes2.count)
let userInfo2: [AnyHashable: Any] = ["Key1": "Value1", "Key2": "Value2"]
let storagePolicy2 = URLCache.StoragePolicy.allowedInMemoryOnly
let cachedResponse2 = CachedURLResponse(response: response2, data: data2, userInfo: userInfo2, storagePolicy: storagePolicy2)
// Ideally, this cached response should have a different hash.
let url3 = try XCTUnwrap(URL(string: "http://example.com/second"))
let response3 = URLResponse(url: url3, mimeType: nil, expectedContentLength: -1, textEncodingName: nil)
let bytes3: [UInt8] = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
let data3 = Data(bytes: bytes3, count: bytes3.count)
let userInfo3: [AnyHashable: Any] = ["Key3": "Value3", "Key2": "Value2"]
let storagePolicy3 = URLCache.StoragePolicy.notAllowed
let cachedResponse3 = CachedURLResponse(response: response3, data: data3, userInfo: userInfo3, storagePolicy: storagePolicy3)
XCTAssertEqual(cachedResponse1.hash, cachedResponse2.hash)
XCTAssertNotEqual(cachedResponse1.hash, cachedResponse3.hash)
XCTAssertNotEqual(cachedResponse2.hash, cachedResponse3.hash)
}
}
|