From dfeedfabfe26b9112f61426a804a50397dd3c929 Mon Sep 17 00:00:00 2001 From: apeltekci Date: Fri, 29 May 2026 17:36:36 -0700 Subject: [PATCH] test(api): cover baseURL path-prefix preservation; fix ContentCache indent The request(path:) URL builder already preserves a baseURL path prefix (e.g. https://host/feedback), but it had no regression test. Add one asserting the prefix is joined ahead of the API path. Also fix the mis-indented ContentCache.init that landed at column 0. Co-Authored-By: Claude Opus 4.8 (1M context) --- Sources/FeedbackKit/Cache/ContentCache.swift | 10 +++++----- .../FeedbackKitTests/HTTPFeedbackAPITests.swift | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/Sources/FeedbackKit/Cache/ContentCache.swift b/Sources/FeedbackKit/Cache/ContentCache.swift index 8d1bc20..071e765 100644 --- a/Sources/FeedbackKit/Cache/ContentCache.swift +++ b/Sources/FeedbackKit/Cache/ContentCache.swift @@ -3,11 +3,11 @@ import Foundation public struct ContentCache: Sendable { private let directory: URL -public init(directory: URL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0] - .appendingPathComponent("FeedbackKit")) { - self.directory = directory - try? FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true, attributes: nil) -} + public init(directory: URL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0] + .appendingPathComponent("FeedbackKit")) { + self.directory = directory + try? FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true, attributes: nil) + } public func save(_ value: T, as key: String) throws { let data = try Self.encoder.encode(value) diff --git a/Tests/FeedbackKitTests/HTTPFeedbackAPITests.swift b/Tests/FeedbackKitTests/HTTPFeedbackAPITests.swift index 0fbb4a9..0f15011 100644 --- a/Tests/FeedbackKitTests/HTTPFeedbackAPITests.swift +++ b/Tests/FeedbackKitTests/HTTPFeedbackAPITests.swift @@ -43,4 +43,20 @@ final class HTTPFeedbackAPITests: XCTestCase { XCTAssertEqual(error as? APIError, .unauthorized) } } + + // A baseURL with a path prefix (e.g. https://host/feedback) must be preserved + // when building request URLs, not dropped — guards the request(path:) path-join. + func testListPostsPreservesBaseURLPathPrefix() async throws { + let cfg = URLSessionConfiguration.ephemeral + cfg.protocolClasses = [StubURLProtocol.self] + let session = URLSession(configuration: cfg) + let api = HTTPFeedbackAPI(baseURL: URL(string: "https://fb.example.com/feedback")!, + session: session, tokenProvider: { nil }) + StubURLProtocol.handler = { req in + XCTAssertEqual(req.url?.path, "/feedback/api/public/v1/posts") + let body = #"{"data":[],"meta":{"pagination":{"cursor":null,"hasMore":false}}}"# + return (HTTPURLResponse(url: req.url!, statusCode: 200, httpVersion: nil, headerFields: nil)!, Data(body.utf8)) + } + _ = try await api.listPosts(boardId: nil, sort: .newest, cursor: nil) + } }