From f5e311d07ac424d32f8e878ac905a7e245ac3de9 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 27 Feb 2026 07:31:52 +0000 Subject: [PATCH] Fix macOS test crash: read httpBodyStream when httpBody is nil On macOS, URLProtocol delivers POST bodies via httpBodyStream instead of httpBody. The bulk lookup test was force-unwrapping request.httpBody which is nil on macOS, causing a fatal error. https://claude.ai/code/session_01PFKz7y2ynoU3uEVaGxKHYP --- Tests/IPDataTests/IPDataClientTests.swift | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/Tests/IPDataTests/IPDataClientTests.swift b/Tests/IPDataTests/IPDataClientTests.swift index 303c722..c932d2a 100644 --- a/Tests/IPDataTests/IPDataClientTests.swift +++ b/Tests/IPDataTests/IPDataClientTests.swift @@ -141,7 +141,25 @@ final class IPDataClientTests: XCTestCase { XCTAssertEqual(request.value(forHTTPHeaderField: "Content-Type"), "application/json") XCTAssertEqual(request.value(forHTTPHeaderField: "api-key"), "test-api-key") - let body = try! JSONDecoder().decode([String].self, from: request.httpBody!) + // On macOS, URLProtocol delivers the body via httpBodyStream instead of httpBody. + let bodyData: Data + if let httpBody = request.httpBody { + bodyData = httpBody + } else if let stream = request.httpBodyStream { + stream.open() + var buffer = [UInt8](repeating: 0, count: 1024) + var collected = Data() + while stream.hasBytesAvailable { + let read = stream.read(&buffer, maxLength: buffer.count) + if read <= 0 { break } + collected.append(buffer, count: read) + } + stream.close() + bodyData = collected + } else { + XCTFail("Expected HTTP body"); return (HTTPURLResponse(), Data()) + } + let body = try! JSONDecoder().decode([String].self, from: bodyData) XCTAssertEqual(body, ["1.1.1.1", "8.8.8.8"]) let response = HTTPURLResponse(url: request.url!, statusCode: 200, httpVersion: nil, headerFields: nil)!