api-swift / Tests / IrdAPITests / CoverageTests.swift
181 words
 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
import Foundation
import Testing
@testable import IrdAPI

/// Executable full-coverage inventory: every generated method is invoked against a
/// recording transport answering the synthesised success body, and must issue exactly
/// one request with the exact method and URL. Decoding through the typed model proves
/// every response type reads what the spec describes.
@Suite struct CoverageTests {
    static let fixtures: [String: JSONValue] = {
        let url = Bundle.module.url(forResource: "synth", withExtension: "json", subdirectory: "Fixtures")!
        return try! JSONValue.parse(try! Data(contentsOf: url)).objectValue!
    }()

    @Test func theInventoryCoversEveryOperation() {
        #expect(GeneratedCoverage.all.count == 161)
        #expect(Set(GeneratedCoverage.all.map(\.key)).count == 161)
        #expect(Set(GeneratedCoverage.all.map(\.key)) == Spec.operations)
    }

    @Test(arguments: GeneratedCoverage.all.map(\.key))
    func everyOperationHitsTheExactMethodAndURL(key: String) async throws {
        let c = try #require(GeneratedCoverage.all.first { $0.key == key })
        let reply = Self.fixtures[key]?["response"]?["full"]
        let body = try reply.map { try $0.serialized() } ?? Data()
        let (client, http) = captureClient(script: [HTTPResponse(status: c.status, body: body)])
        try await c.call(client)
        #expect(http.count == 1, "exactly one request")
        let r = try #require(http.last)
        #expect(r.httpMethod == c.method)
        #expect(r.url?.absoluteString == c.url)
        #expect(r.value(forHTTPHeaderField: "Authorization") == "Bearer ird_k" || key.hasPrefix("POST /auth/device/"))
        #expect((r.httpBody != nil) == c.hasBody, "body presence")
        if c.hasBody { #expect(r.value(forHTTPHeaderField: "Content-Type") == "application/json") }
    }
}

static mirror of HEAD · about · clone: git clone https://ird.cod-sursa.ro/api-swift.git