-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathPathConfigurationLoaderTests.swift
77 lines (60 loc) · 2.65 KB
/
PathConfigurationLoaderTests.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
import OHHTTPStubs
import OHHTTPStubsSwift
@testable import Turbo
import XCTest
class PathConfigurationLoaderTests: XCTestCase {
private let serverURL = URL(string: "http://turbo.test/configuration.json")!
private let fileURL = Bundle.module.url(forResource: "test-configuration", withExtension: "json", subdirectory: "Fixtures")!
func test_load_data_automaticallyLoadsFromPassedInDataAndCallsHandler() throws {
let data = try! Data(contentsOf: fileURL)
let loader = PathConfigurationLoader(sources: [.data(data)])
var loadedConfig: PathConfigurationDecoder? = nil
loader.load { loadedConfig = $0 }
let config = try XCTUnwrap(loadedConfig)
XCTAssertEqual(config.rules.count, 5)
}
func test_file_automaticallyLoadsFromTheLocalFileAndCallsTheHandler() throws {
let loader = PathConfigurationLoader(sources: [.file(fileURL)])
var loadedConfig: PathConfigurationDecoder? = nil
loader.load { loadedConfig = $0 }
let config = try XCTUnwrap(loadedConfig)
XCTAssertEqual(config.rules.count, 5)
}
func test_server_automaticallyDownloadsTheFileAndCallsTheHandler() throws {
let loader = PathConfigurationLoader(sources: [.server(serverURL)])
let expectation = stubRequest(for: loader)
var loadedConfig: PathConfigurationDecoder? = nil
loader.load { config in
loadedConfig = config
expectation.fulfill()
}
wait(for: [expectation])
let config = try XCTUnwrap(loadedConfig)
XCTAssertEqual(config.rules.count, 1)
}
func test_server_cachesTheFile() {
let loader = PathConfigurationLoader(sources: [.server(serverURL)])
let expectation = stubRequest(for: loader)
var handlerCalled = false
loader.load { _ in
handlerCalled = true
expectation.fulfill()
}
wait(for: [expectation])
XCTAssertTrue(handlerCalled)
XCTAssertTrue(FileManager.default.fileExists(atPath: loader.configurationCacheURL.path))
}
private func stubRequest(for loader: PathConfigurationLoader) -> XCTestExpectation {
stub(condition: { _ in true }) { _ in
let json = ["rules": [["patterns": ["/new"], "properties": ["presentation": "test"]] as [String: Any]]]
return HTTPStubsResponse(jsonObject: json, statusCode: 200, headers: [:])
}
clearCache(loader.configurationCacheURL)
return expectation(description: "Wait for configuration to load.")
}
private func clearCache(_ url: URL) {
do {
try FileManager.default.removeItem(at: url)
} catch {}
}
}