2020-09-07 04:56:18 +00:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
|
|
|
import CodableBloomFilter
|
|
|
|
import Combine
|
|
|
|
import CombineExpectations
|
|
|
|
@testable import ServiceLayer
|
|
|
|
@testable import ServiceLayerMocks
|
|
|
|
import Stubbing
|
|
|
|
import XCTest
|
|
|
|
|
2020-09-10 04:51:31 +00:00
|
|
|
class InstanceURLServiceTests: XCTestCase {
|
2020-09-07 04:56:18 +00:00
|
|
|
func testFiltering() throws {
|
2020-09-10 04:51:31 +00:00
|
|
|
let sut = InstanceURLService(environment: .mock())
|
2020-09-07 04:56:18 +00:00
|
|
|
|
2020-09-23 07:11:35 +00:00
|
|
|
if case .failure = sut.url(text: "unfiltered.instance") {
|
|
|
|
XCTFail("Expected success")
|
|
|
|
}
|
|
|
|
|
|
|
|
if case .success = sut.url(text: "filtered.instance") {
|
|
|
|
XCTFail("Expected failure")
|
|
|
|
}
|
|
|
|
|
|
|
|
if case .success = sut.url(text: "subdomain.filtered.instance") {
|
|
|
|
XCTFail("Expected failure")
|
|
|
|
}
|
2020-09-07 04:56:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testUpdating() throws {
|
|
|
|
let environment = AppEnvironment.mock()
|
2020-09-10 04:51:31 +00:00
|
|
|
var sut = InstanceURLService(environment: environment)
|
2020-09-07 04:56:18 +00:00
|
|
|
|
2020-09-23 07:11:35 +00:00
|
|
|
if case .success = sut.url(text: "filtered.instance") {
|
|
|
|
XCTFail("Expected failure")
|
|
|
|
}
|
|
|
|
|
|
|
|
if case .failure = sut.url(text: "instance.filtered") {
|
|
|
|
XCTFail("Expected success")
|
|
|
|
}
|
2020-09-07 04:56:18 +00:00
|
|
|
|
2020-09-08 06:49:58 +00:00
|
|
|
var updatedFilter = try BloomFilter<String>(hashes: [.djb232, .sdbm32], byteCount: 16)
|
2020-09-07 04:56:18 +00:00
|
|
|
|
|
|
|
updatedFilter.insert("instance.filtered")
|
|
|
|
|
|
|
|
let updatedFilterData = try JSONEncoder().encode(updatedFilter)
|
|
|
|
let stub: HTTPStub = .success((URLResponse(), updatedFilterData))
|
|
|
|
|
2020-09-13 00:02:56 +00:00
|
|
|
StubbingURLProtocol.setStub(stub, forURL: URL(string: "https://filter.metabolist.com/filter")!)
|
2020-09-07 04:56:18 +00:00
|
|
|
|
|
|
|
let updateRecorder = sut.updateFilter().collect().record()
|
|
|
|
|
|
|
|
_ = try wait(for: updateRecorder.next(), timeout: 1)
|
|
|
|
|
2020-09-23 07:11:35 +00:00
|
|
|
if case .failure = sut.url(text: "filtered.instance") {
|
|
|
|
XCTFail("Expected success")
|
|
|
|
}
|
|
|
|
|
|
|
|
if case .success = sut.url(text: "instance.filtered") {
|
|
|
|
XCTFail("Expected failure")
|
|
|
|
}
|
2020-09-07 04:56:18 +00:00
|
|
|
|
2020-09-10 04:51:31 +00:00
|
|
|
sut = InstanceURLService(environment: environment)
|
2020-09-07 04:56:18 +00:00
|
|
|
|
2020-09-23 07:11:35 +00:00
|
|
|
if case .failure = sut.url(text: "filtered.instance") {
|
|
|
|
XCTFail("Expected success")
|
|
|
|
}
|
|
|
|
|
|
|
|
if case .success = sut.url(text: "instance.filtered") {
|
|
|
|
XCTFail("Expected failure")
|
|
|
|
}
|
2020-09-07 04:56:18 +00:00
|
|
|
}
|
|
|
|
}
|