-
Notifications
You must be signed in to change notification settings - Fork 1
/
ChuckNorrisHelpers.swift
53 lines (38 loc) · 1.65 KB
/
ChuckNorrisHelpers.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
//
// ChuckNorrisHelpers.swift
// APIRestDemoTests
//
// Created by Adrian Iraizos Mendoza on 24/1/24.
//
import Foundation
@testable import APIRestDemo
extension ChuckNorrisModel {
static let samples = (0..<5).map { ChuckNorrisModel(categories: ["Category \($0)"], value: "Joke \($0)") }
static let sample = ChuckNorrisModel(categories: ["Category 1"], value: "Joke1")
}
final class ChuckURLSessionMock: URLProtocol {
let jokesData = try? JSONEncoder().encode(ChuckNorrisModel.samples)
let jokeData = try? JSONEncoder().encode(ChuckNorrisModel.sample)
override class func canInit(with request: URLRequest) -> Bool {
true
}
override class func canonicalRequest(for request: URLRequest) -> URLRequest {
request
}
override func startLoading() {
if let url = request.url, url == .chuckNorrisURL, let jokeData {
client?.urlProtocol(self, didLoad: jokeData)
if let response = HTTPURLResponse(url: url, statusCode: 200, httpVersion: nil, headerFields: ["Content-Type":"application/json"]) {
client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed)
}
} else {
client?.urlProtocol(self, didFailWithError: PersistenceError.data)
}
client?.urlProtocolDidFinishLoading(self)
}
override func stopLoading() { }
}
struct URLTesting: URLLocator {
var chuckNorrisURL = URL(string: "http://localhost:8080/jokes/random")!
var chuckNorrisFavorites: URL = Bundle(for: APIRestDemoTests.self).url(forResource: "testJokes", withExtension: "json")!
}