Feature: Testing mocks for HTTPFetcher + FormParameter parsing#32
Feature: Testing mocks for HTTPFetcher + FormParameter parsing#32ThisIsMissEm wants to merge 5 commits into
Conversation
🦋 Changeset detectedLatest commit: a26e610 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
New GermConvenienceTesting library target with: - FormParameters+Parsing: init(parsing:) extensions for Data, URLComponents, and [URLQueryItem] This helps with evaluating whether requests we send are the correct shape.
Introduces MockHTTPFetcher which conforms to HTTPFetcher allowing easier writing of test assertions: - MockHTTPFetcher: method-aware HTTP mock with per-URL handler queues, exact-to-any fallback, and request logging - HTTPResponseError: Equatable conformance to support error matching in tests
859bd8d to
a26e610
Compare
germ-mark
left a comment
There was a problem hiding this comment.
I'd suggest you adopt the three target structure of "library, mocks, tests", e.g. in
https://github.com/germ-network/AtprotoTypes
Where the mocks live in a separate target, can be adopted by downstream Mock and Test Targets. The tests can then import the library and the mock.
(I have been meaning to and this would be a good time to lift the Mockable target from AtprotoTypes to GermConvenience. don't need to do it as part of this PR, but now is a good time in this next release)
| let method: MethodMatcher | ||
| } | ||
|
|
||
| private let lock = NSLock() |
There was a problem hiding this comment.
what is the function of the lock here?
| return self | ||
| } | ||
|
|
||
| public func data(for request: BundledHTTPRequest) async throws -> HTTPDataResponse { |
There was a problem hiding this comment.
this doesn't have any async calls in the body, so this can be a synchronous call, which makes the use of a lock seemingly spurious
| public func requests(for url: URL, method: MethodMatcher) -> [BundledHTTPRequest] { | ||
| requestLog[url, default: []].filter { | ||
| switch method { | ||
| case .any: return true |
There was a problem hiding this comment.
can probably be an implicit return (llm's have not caught on to implicit returns)
| } | ||
|
|
||
| private func decodeString(_ input: Substring) -> String { | ||
| return input.removingPercentEncoding ?? String(input) |
There was a problem hiding this comment.
implicit return would be nice here
In order to start writing tests for oauth4swift, we needed some test helpers. This had been on my list for a while, and well, today I happened to be being annoyed by my cough so needed something to focus on.
Major changes:
.dataor.queryItems()value of FormParameters, intended for use with HTTP Testing.Minor changes, currently in GermConvenienceTesting:
As MockHTTPFetcher internally uses BundledHTTPRequest for storage, so we do actually capture the request body, so we can make an assertion about the request that we send: url, headers, body, everything.
This pull request was authored with the help of Claude, however, I largely wrote the code and claude reviewed and guided me when I got stuck with swift specifics, rather than it implementing the code (I only had it implement a few minor changes)