Skip to content
New issue

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

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

Already on GitHub? # to your account

added support for empty data response #58

Merged
merged 2 commits into from
Sep 17, 2022
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
12 changes: 11 additions & 1 deletion Sources/Get/DataLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ private final class DownloadTaskHandler: TaskHandler {

// MARK: - Helpers

protocol OptionalDecoding {}

struct DataLoaderError: Error {
let task: URLSessionTask
let error: Error
Expand All @@ -333,6 +335,8 @@ extension OperationQueue {
}
}

extension Optional: OptionalDecoding {}

func encode(_ value: Encodable, using encoder: JSONEncoder) async throws -> Data? {
if let data = value as? Data {
return data
Expand All @@ -346,7 +350,9 @@ func encode(_ value: Encodable, using encoder: JSONEncoder) async throws -> Data
}

func decode<T: Decodable>(_ data: Data, using decoder: JSONDecoder) async throws -> T {
if T.self == Data.self {
if data.isEmpty, T.self is OptionalDecoding.Type {
return Optional<Decodable>.none as! T
} else if T.self == Data.self {
return data as! T
} else if T.self == String.self {
guard let string = String(data: data, encoding: .utf8) else {
Expand All @@ -359,3 +365,7 @@ func decode<T: Decodable>(_ data: Data, using decoder: JSONDecoder) async throws
}.value
}
}




18 changes: 16 additions & 2 deletions Tests/GetTests/ClientSendingRequestsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ final class ClientSendingRequestsTests: XCTestCase {
// THEN returns decoded JSON
XCTAssertEqual(user?.login, "kean")
}

// func value(for:) -> Decodable
func testResponseEmpty() async throws {
// GIVEN
Expand All @@ -153,6 +153,20 @@ final class ClientSendingRequestsTests: XCTestCase {
// WHEN
try await client.send(Request(path: "/user")).value
}

func testResponseEmptyWithDecodableOptional() async throws {
// GIVEN
let url = URL(string: "https://api.github.com/user")!
Mock(url: url, dataType: .json, statusCode: 204, data: [
.get: Data()
]).register()

// WHEN
let user: User? = try await client.send(Request(path: "/user")).value

// THEN returns nil response
XCTAssertNil(user)
}

// func value(for:) -> Data
func testResponseData() async throws {
Expand All @@ -170,7 +184,7 @@ final class ClientSendingRequestsTests: XCTestCase {
}

// func value(for:) -> String
func testResponeString() async throws {
func testResponseString() async throws {
// GIVEN
let url = URL(string: "https://api.github.com/user")!
Mock(url: url, dataType: .json, statusCode: 200, data: [
Expand Down