-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
GeneralizedImageFetcherTests.swift
128 lines (102 loc) · 3.6 KB
/
GeneralizedImageFetcherTests.swift
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/
import Foundation
import XCTest
@testable import Client
class GeneralizedImageFetcherTests: XCTestCase {
override func setUp() {
super.setUp()
clearState()
}
override func tearDown() {
super.tearDown()
clearState()
}
func testErrorResponse() {
loadStubResponse(response: nil, statusCode: 200, error: anError)
testGeneralizedImageFetcher { imageData in
XCTAssertNil(imageData)
}
}
func testNilData() {
loadStubResponse(response: nil, statusCode: 200, error: nil)
testGeneralizedImageFetcher { imageData in
XCTAssertNil(imageData)
}
}
func testBadStatusCode() {
loadStubResponse(response: nil, statusCode: 500, error: nil)
testGeneralizedImageFetcher { imageData in
XCTAssertNil(imageData)
}
}
func testFetchFailForUnsupportedImageType() {
loadStubResponse(response: sampleResponseUnsupportedImageType, statusCode: 200, error: nil)
testGeneralizedImageFetcher { imageData in
XCTAssertNil(imageData)
}
}
func testFetchSucceeds() {
_ = XCTSkip("Production is currently giving back an unsupported image type, SVG.")
}
}
// MARK: - Helpers
extension GeneralizedImageFetcherTests {
var testUrl: URL {
return URL(string: "https://profile.accounts.firefox.com/v1/avatar/a")!
}
var anError: NSError {
return NSError(domain: "test error", code: 0)
}
var sampleResponseUnsupportedImageType: String {
return """
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
<path d="M30,1h40l29,29v40l-29,29h-40l-29-29v-40z" stroke="#000" fill="none"/>
<path d="M31,3h38l28,28v38l-28,28h-38l-28-28v-38z" fill="#a23"/>
<text x="50" y="68" font-size="48" fill="#FFF" text-anchor="middle">410</text>
</svg>
"""
}
func clearState() {
URLProtocolStub.removeStub()
}
func loadStubResponse(
response: String?,
statusCode: Int,
error: Error?
) {
let mockData = response?.data(using: .utf8)
let response = HTTPURLResponse(
url: testUrl,
statusCode: statusCode,
httpVersion: nil,
headerFields: nil
)
URLProtocolStub.stub(
data: mockData,
response: response,
error: error
)
}
func testGeneralizedImageFetcher(completion: @escaping (UIImage?) -> Void) {
let imageFetcher = getGeneralizedImageFetcher()
let expectation = expectation(description: "Wait on completion.")
imageFetcher.getImageFor(url: testUrl) { imageData in
completion(imageData)
expectation.fulfill()
}
waitForExpectations(timeout: 1.0, handler: nil)
}
func getGeneralizedImageFetcher(file: StaticString = #filePath, line: UInt = #line) -> GeneralizedImageFetcher {
let configuration = URLSessionConfiguration.ephemeral
configuration.protocolClasses = [URLProtocolStub.self]
let session = URLSession(configuration: configuration)
let cache = URLCache(memoryCapacity: 100000, diskCapacity: 1000, directory: URL(string: "/dev/null"))
var fetcher = GeneralizedImageFetcher()
fetcher.urlSession = session
fetcher.urlCache = cache
trackForMemoryLeaks(cache, file: file, line: line)
return fetcher
}
}