Skip to content
Merged
Show file tree
Hide file tree
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
18 changes: 15 additions & 3 deletions Sources/SimpleNetworking/SimpleNetworking/createHTTPBody.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions Sources/SimpleNetworking/SimpleNetworking/logging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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"
Expand Down
41 changes: 41 additions & 0 deletions Tests/SimpleNetworkingTests/SimpleNetworkingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,47 @@
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() {
Expand Down Expand Up @@ -225,7 +266,7 @@
}

func testFileUploadError_InvalidURL() {
let httpURL = URL(string: "https://example.com/file.txt")!

Check failure on line 269 in Tests/SimpleNetworkingTests/SimpleNetworkingTests.swift

View workflow job for this annotation

GitHub Actions / swiftlint

SwiftLint rule 'force_unwrapping' did not trigger a violation in the disabled region; remove the disable command (superfluous_disable_command)
// swiftlint:disable:previous force_unwrapping

XCTAssertThrowsError(try SimpleNetworking.FileUpload(
Expand Down
Loading