-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #113 from guilhermearaujo/decodable
Add response decoding using Decodable
- Loading branch information
Showing
5 changed files
with
139 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
@testable import Malibu | ||
import When | ||
import Quick | ||
import Nimble | ||
|
||
final class ResponseDecodingSpec: QuickSpec, NetworkPromiseSpec { | ||
var networkPromise: NetworkPromise! | ||
var request: URLRequest! | ||
var data: Data! | ||
|
||
override func spec() { | ||
describe("ResponseDecoding") { | ||
let url = URL(string: "http://api.loc")! | ||
let response = HTTPURLResponse(url: url, statusCode: 200, httpVersion: "HTTP/2.0", headerFields: nil)! | ||
let decoder = JSONDecoder() | ||
|
||
// MARK: - Specs | ||
|
||
beforeEach { | ||
self.networkPromise = NetworkPromise() | ||
self.request = URLRequest(url: URL(string: "http://api.loc")!) | ||
self.data = try! JSONSerialization.data( | ||
withJSONObject: [["name": "Taylor"]], | ||
options: JSONSerialization.WritingOptions() | ||
) | ||
} | ||
|
||
describe("#decode") { | ||
var promise: Promise<[Int]>! | ||
|
||
beforeEach { | ||
promise = self.networkPromise.decode(using: [Int].self, decoder: decoder) | ||
} | ||
|
||
context("when response is rejected") { | ||
it("rejects promise with an error") { | ||
self.testFailedResponse(promise) | ||
} | ||
} | ||
|
||
context("when response is resolved") { | ||
context("when decoding fails") { | ||
it("rejects promise with an error") { | ||
self.data = "string".data(using: String.Encoding.utf32) | ||
let response = self.makeResponse(statusCode: 200, data: self.data) | ||
|
||
self.testFailedPromise( | ||
promise, | ||
error: NetworkError.responseDecodingFailed(type: [Int].self, response: response), | ||
response: response.httpUrlResponse) | ||
} | ||
} | ||
|
||
context("when there is no data in response") { | ||
it("rejects promise with an error") { | ||
self.data = Data() | ||
|
||
self.testFailedPromise( | ||
promise, | ||
error: NetworkError.noDataInResponse, | ||
response: response | ||
) | ||
} | ||
} | ||
|
||
context("when decoding succeeded") { | ||
it("resolves promise with the specified type") { | ||
let string = "[1,2,3]" | ||
self.data = string.data(using: String.Encoding.utf8) | ||
|
||
self.testSucceededPromise(promise, response: response) { result in | ||
expect(result == [1, 2, 3]).to(beTrue()) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import Foundation | ||
import When | ||
|
||
// MARK: - Decoding | ||
|
||
public extension Promise where T: Response { | ||
func decode<U: Decodable>(using type: U.Type, decoder: JSONDecoder) -> Promise<U> { | ||
return then { response -> U in | ||
guard !response.data.isEmpty else { throw NetworkError.noDataInResponse } | ||
|
||
do { | ||
return try decoder.decode(type, from: response.data) | ||
} catch { | ||
throw NetworkError.responseDecodingFailed(type: type, response: response) | ||
} | ||
} | ||
} | ||
} |