meow/Meow/Tests/MeowTests/MeowTests.swift

50 lines
1.6 KiB
Swift

//
// MeowTests.swift
// MeowTests
//
import Foundation
import Testing
import Mocker
@testable import Meow
@Suite("Meow Tests")
struct MeowTests {
private let baseURL = URL(string: "https://meow.meow")!
let urlSession: URLSession = {
let configuration = URLSessionConfiguration.default
configuration.protocolClasses = [MockingURLProtocol.self]
return URLSession(configuration: configuration)
}()
@Test
func constructor() async throws {
let instance = vendInstance()
#expect(instance.baseURL == baseURL)
}
@Test
func getFacts() async throws {
let instance = vendInstance()
let count = 5
mockPath("/", queryItems: ["count": count], data: [.get: Fixtures.facts])
let facts = try await instance.getFacts(count: count)
#expect(facts.count == count)
}
// MARK: - Private functionality
private func vendInstance() -> Meow {
return Meow(baseURL: baseURL, urlSession: urlSession)
}
private func mockPath(_ path: String, headers: [String: String] = [:], queryItems: [String: CustomStringConvertible] = [:], data: [Mock.HTTPMethod: Data] = [:]) {
let urlQueryItems = queryItems
.compactMap({ URLQueryItem(name: $0, value: $1.description) })
.sorted { a, b in a.name < b.name }
let base = baseURL.appendingPathComponent(path)
let url = urlQueryItems.isEmpty ? base : base.appending(queryItems: urlQueryItems)
Mock(url: url, ignoreQuery: false, contentType: .json, statusCode: 200, data: data, additionalHeaders: headers).register()
}
}