[api-upload-refactor] Add test target, relax decode constraints, add HTTPBody + MultipartForm
- Package: new LCEssentialsTests target (runs on iOS simulator) - JSONDecoder.decode: T: Codable -> T: Decodable & Sendable (4 overloads); remove try! in decode(fromURL:); honour encoding param in decode(_:using:) - Propagate & Sendable to Data.object, Dictionary.toObjetct, API.request return - LCEHTTPBody: HTTPBody protocol, JSONBody, FormURLEncodedBody, RawBody, factories - LCEMultipartForm: multipart builder, OutputStream-streamed serialize(), in-memory + on-disk file parts, ported mimeType(for:) - Tests: StubURLProtocol harness + 15 tests
This commit is contained in:
26
Tests/LCEssentialsTests/JSONDecoderDecodeTests.swift
Normal file
26
Tests/LCEssentialsTests/JSONDecoderDecodeTests.swift
Normal file
@@ -0,0 +1,26 @@
|
||||
import XCTest
|
||||
@testable import LCEssentials
|
||||
|
||||
private struct OnlyDecodable: Decodable, Sendable, Equatable {
|
||||
let id: Int
|
||||
let name: String
|
||||
}
|
||||
|
||||
final class JSONDecoderDecodeTests: XCTestCase {
|
||||
|
||||
func testDecodesTypeThatIsDecodableAndSendableButNotEncodable() throws {
|
||||
let data = Data(#"{"id":7,"name":"loverde"}"#.utf8)
|
||||
let value: OnlyDecodable = try JSONDecoder.decode(data: data)
|
||||
XCTAssertEqual(value, OnlyDecodable(id: 7, name: "loverde"))
|
||||
}
|
||||
|
||||
func testDecodeFromStringOverload() throws {
|
||||
let value: OnlyDecodable = try JSONDecoder.decode(#"{"id":1,"name":"a"}"#)
|
||||
XCTAssertEqual(value, OnlyDecodable(id: 1, name: "a"))
|
||||
}
|
||||
|
||||
func testDecodeFromURLThrowsInsteadOfCrashingOnMissingFile() {
|
||||
let missing = URL(fileURLWithPath: "/tmp/does-not-exist-\(UUID().uuidString).json")
|
||||
XCTAssertThrowsError(try JSONDecoder.decode(fromURL: missing) as OnlyDecodable)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user