Skip to content

Commit 364b02b

Browse files
authored
Fix NIOTooManyBytesError that sometimes occurs during XCTests (#129)
1 parent 245d869 commit 364b02b

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

Sources/SwiftlyCore/HTTPClient.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public struct SwiftlyHTTPClient {
3838
self.executor = executor ?? HTTPRequestExecutorImpl()
3939
}
4040

41-
private func get(url: String, headers: [String: String]) async throws -> Response {
41+
private func get(url: String, headers: [String: String], maxBytes: Int) async throws -> Response {
4242
var request = makeRequest(url: url)
4343

4444
for (k, v) in headers {
@@ -47,9 +47,7 @@ public struct SwiftlyHTTPClient {
4747

4848
let response = try await self.executor.execute(request, timeout: .seconds(30))
4949

50-
// if defined, the content-length headers announces the size of the body
51-
let expectedBytes = response.headers.first(name: "content-length").flatMap(Int.init) ?? 1024 * 1024
52-
return Response(status: response.status, buffer: try await response.body.collect(upTo: expectedBytes))
50+
return Response(status: response.status, buffer: try await response.body.collect(upTo: maxBytes))
5351
}
5452

5553
/// Decode the provided type `T` from the JSON body of the response from a GET request
@@ -59,7 +57,8 @@ public struct SwiftlyHTTPClient {
5957
type: T.Type,
6058
headers: [String: String] = [:]
6159
) async throws -> T {
62-
let response = try await self.get(url: url, headers: headers)
60+
// Maximum expected size for a JSON payload for an API is 1MB
61+
let response = try await self.get(url: url, headers: headers, maxBytes: 1024 * 1024)
6362

6463
guard case .ok = response.status else {
6564
var message = "received status \"\(response.status)\" when reaching \(url)"

0 commit comments

Comments
 (0)