diff --git a/Sources/SimpleNetworking/SimpleNetworking/createHTTPBody.swift b/Sources/SimpleNetworking/SimpleNetworking/createHTTPBody.swift index f6a674d..1e4902b 100644 --- a/Sources/SimpleNetworking/SimpleNetworking/createHTTPBody.swift +++ b/Sources/SimpleNetworking/SimpleNetworking/createHTTPBody.swift @@ -18,6 +18,10 @@ extension SimpleNetworking { /// - Returns: Encoded data func createHTTPBody(with value: Any?, postType: POSTEncoding = .auto) -> Data? { // swiftlint:disable:previous cyclomatic_complexity function_body_length + if let data = value as? Data { + return data + } + // Determine the actual encoding to use let actualEncoding: POSTEncoding switch postType { @@ -30,11 +34,19 @@ extension SimpleNetworking { switch actualEncoding { case .json: if let contents = value as? [String: Codable] { - return try? JSONSerialization.data(withJSONObject: contents) + do { + return try JSONSerialization.data(withJSONObject: contents) + } catch { + log(error.localizedDescription, level: .error) + } } - if let contents = value as? Codable { - return try? JSONEncoder().encode(contents) + if let contents = value as? Encodable { + do { + return try JSONEncoder().encode(contents) + } catch { + log(error.localizedDescription, level: .error) + } } case .plain: diff --git a/Sources/SimpleNetworking/SimpleNetworking/logging.swift b/Sources/SimpleNetworking/SimpleNetworking/logging.swift index a571d43..615530b 100644 --- a/Sources/SimpleNetworking/SimpleNetworking/logging.swift +++ b/Sources/SimpleNetworking/SimpleNetworking/logging.swift @@ -29,7 +29,7 @@ extension SimpleNetworking { #endif /// Log helper that works on all platforms - private func log(_ message: String, level: LogLevel = .info) { + internal func log(_ message: String, level: LogLevel = .info) { #if canImport(OSLog) switch level { case .debug: @@ -46,7 +46,7 @@ extension SimpleNetworking { } /// Log levels - private enum LogLevel: String { + internal enum LogLevel: String { case debug = "DEBUG" case info = "INFO" case error = "ERROR" diff --git a/Tests/SimpleNetworkingTests/SimpleNetworkingTests.swift b/Tests/SimpleNetworkingTests/SimpleNetworkingTests.swift index 599fdfa..e4265a0 100644 --- a/Tests/SimpleNetworkingTests/SimpleNetworkingTests.swift +++ b/Tests/SimpleNetworkingTests/SimpleNetworkingTests.swift @@ -41,6 +41,47 @@ final class SimpleNetworkingTests: XCTestCase { XCTAssert(response.string?.contains("OVERRIDE") ?? false) } + func testPutRequestFillsHTTPBodyWithRawData() async { + let body = Data(#"{"name":"Updated"}"#.utf8) + networking.set(mockData: [ + "https://wesleydegroot.nl/api/update": .init( + data: "{}", + response: nil, + statusCode: 200, + error: nil + ) + ]) + + let response = await networking.request( + path: "/api/update", + method: .put(body) + ) + + XCTAssertEqual(response.request.httpMethod, "PUT") + XCTAssertEqual(response.request.httpBody, body) + XCTAssertEqual(response.request.value(forHTTPHeaderField: "Content-Type"), "application/json") + } + + func testPutRequestFillsHTTPBodyWithJSONDictionary() async throws { + networking.set(mockData: [ + "https://wesleydegroot.nl/api/update": .init( + data: "{}", + response: nil, + statusCode: 200, + error: nil + ) + ]) + + let response = await networking.request( + path: "/api/update", + method: .put(["id": "123", "name": "Updated"]) + ) + + let body = try XCTUnwrap(response.request.httpBody) + let json = try XCTUnwrap(JSONSerialization.jsonObject(with: body) as? [String: String]) + XCTAssertEqual(json, ["id": "123", "name": "Updated"]) + } + // MARK: - File Upload Tests func testFileUploadInitialization() {