|
1 | 1 | //
|
2 |
| -// Copyright 2019, 2021, Optimizely, Inc. and contributors |
| 2 | +// Copyright 2019, 2021, 2023, Optimizely, Inc. and contributors |
3 | 3 | //
|
4 | 4 | // Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | // you may not use this file except in compliance with the License.
|
@@ -65,7 +65,65 @@ class DatafileHandlerTests: XCTestCase {
|
65 | 65 | // This is an example of a functional test case.
|
66 | 66 | // Use XCTAssert and related functions to verify your tests produce the correct results.
|
67 | 67 | }
|
| 68 | + |
| 69 | + func testDatafileDownloadWith200AndValidDatafile() { |
| 70 | + // Datafile and last updated should not be saved in this case |
| 71 | + let handler = MockDatafileHandler(statusCode: 200, localResponseData:"{}") |
| 72 | + let expectation = XCTestExpectation(description: "no-nil data") |
| 73 | + |
| 74 | + handler.downloadDatafile(sdkKey: sdkKey) { (result) in |
| 75 | + if case let .success(data) = result { |
| 76 | + XCTAssert(data != nil) |
| 77 | + expectation.fulfill() |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + wait(for: [expectation], timeout: 3) |
| 82 | + XCTAssertTrue(handler.isDatafileSaved(sdkKey: sdkKey)) |
| 83 | + XCTAssertNotNil(handler.sharedDataStore.getLastModified(sdkKey: sdkKey)) |
| 84 | + } |
| 85 | + |
68 | 86 |
|
| 87 | + func testDatafileDownloadWith200AndInvalidDatafile() { |
| 88 | + // Datafile and last updated should not be saved in this case |
| 89 | + let handler = MockDatafileHandler(statusCode: 200, localResponseData: "1231") |
| 90 | + let expectation = XCTestExpectation(description: "wait to get failure") |
| 91 | + |
| 92 | + handler.downloadDatafile(sdkKey: sdkKey) { (result) in |
| 93 | + if case .success(_) = result { |
| 94 | + XCTFail() |
| 95 | + } |
| 96 | + if case .failure(_) = result { |
| 97 | + expectation.fulfill() |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + wait(for: [expectation], timeout: 3) |
| 102 | + XCTAssertFalse(handler.isDatafileSaved(sdkKey: sdkKey)) |
| 103 | + XCTAssertNil(handler.sharedDataStore.getLastModified(sdkKey: sdkKey)) |
| 104 | + } |
| 105 | + |
| 106 | + func testStartWith200AndInvalidDatafile() { |
| 107 | + let handler = MockDatafileHandler(statusCode: 200, localResponseData: "1231") |
| 108 | + let expectation = XCTestExpectation(description: "wait to get failure") |
| 109 | + |
| 110 | + let optimizely = OptimizelyClient(sdkKey: sdkKey, |
| 111 | + datafileHandler: handler, |
| 112 | + periodicDownloadInterval: 1) |
| 113 | + |
| 114 | + optimizely.start(completion: { result in |
| 115 | + if case let .failure(error) = result { |
| 116 | + print(error) |
| 117 | + XCTAssert(true) |
| 118 | + expectation.fulfill() |
| 119 | + } |
| 120 | + }) |
| 121 | + |
| 122 | + wait(for: [expectation], timeout: 3) |
| 123 | + XCTAssertFalse(handler.isDatafileSaved(sdkKey: sdkKey)) |
| 124 | + XCTAssertNil(handler.sharedDataStore.getLastModified(sdkKey: sdkKey)) |
| 125 | + } |
| 126 | + |
69 | 127 | func testDatafileDownload500() {
|
70 | 128 | OTUtils.createDatafileCache(sdkKey: sdkKey)
|
71 | 129 |
|
|
0 commit comments