Skip to content

Fix NIOTooManyBytesError that sometimes occurs during XCTests #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions Sources/SwiftlyCore/HTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public struct SwiftlyHTTPClient {
self.executor = executor ?? HTTPRequestExecutorImpl()
}

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

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

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

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

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

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