|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift OpenFeature open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 the Swift OpenFeature project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// |
| 10 | +// SPDX-License-Identifier: Apache-2.0 |
| 11 | +// |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | + |
| 14 | +import AsyncHTTPClient |
| 15 | +import Foundation |
| 16 | +import Logging |
| 17 | +import NIOCore |
| 18 | +import OFREP |
| 19 | +import ServiceLifecycle |
| 20 | +import Testing |
| 21 | + |
| 22 | +@testable import OpenAPIAsyncHTTPClient |
| 23 | + |
| 24 | +@Suite("HTTP Client Transport") |
| 25 | +struct OFREPHTTPClientTransportTests { |
| 26 | + @Test("Defaults to shared HTTP client") |
| 27 | + func sharedHTTPClient() async throws { |
| 28 | + let provider = OFREPProvider(serverURL: .stub) |
| 29 | + |
| 30 | + let serviceGroup = ServiceGroup( |
| 31 | + configuration: .init( |
| 32 | + services: [.init(service: provider), .shutdownTrigger], |
| 33 | + logger: Logger(label: "test") |
| 34 | + ) |
| 35 | + ) |
| 36 | + |
| 37 | + try await serviceGroup.run() |
| 38 | + } |
| 39 | + |
| 40 | + @Test("Shuts down internally created HTTP client") |
| 41 | + func internallyCreatedHTTPClient() async throws { |
| 42 | + let provider = OFREPProvider(serverURL: .stub, configuration: HTTPClient.Configuration()) |
| 43 | + |
| 44 | + let serviceGroup = ServiceGroup( |
| 45 | + configuration: .init( |
| 46 | + services: [.init(service: provider), .shutdownTrigger], |
| 47 | + logger: Logger(label: "test") |
| 48 | + ) |
| 49 | + ) |
| 50 | + |
| 51 | + try await serviceGroup.run() |
| 52 | + } |
| 53 | + |
| 54 | + @Test("Forwards request to AsyncHTTPClientTransport") |
| 55 | + func forwardsRequest() async throws { |
| 56 | + let requestSender = RecordingRequestSender() |
| 57 | + let transport = AsyncHTTPClientTransport(configuration: .init(), requestSender: requestSender) |
| 58 | + let provider = OFREPProvider(serverURL: .stub, transport: transport) |
| 59 | + |
| 60 | + _ = await provider.resolution(of: "flag", defaultValue: false, context: nil) |
| 61 | + |
| 62 | + await #expect(requestSender.requests.count == 1) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +private actor RecordingRequestSender: HTTPRequestSending { |
| 67 | + var requests = [Request]() |
| 68 | + |
| 69 | + func send( |
| 70 | + request: HTTPClientRequest, |
| 71 | + with client: HTTPClient, |
| 72 | + timeout: TimeAmount |
| 73 | + ) async throws -> AsyncHTTPClientTransport.Response { |
| 74 | + requests.append(Request(request: request, client: client, timeout: timeout)) |
| 75 | + return HTTPClientResponse() |
| 76 | + } |
| 77 | + |
| 78 | + struct Request { |
| 79 | + let request: HTTPClientRequest |
| 80 | + let client: HTTPClient |
| 81 | + let timeout: TimeAmount |
| 82 | + } |
| 83 | +} |
0 commit comments