Skip to content

Commit

Permalink
Merge branch 'feature/double_generic_results' into feature/response_g…
Browse files Browse the repository at this point in the history
…eneric_type

# Conflicts:
#	Source/ResponseSerialization.swift
  • Loading branch information
cnoon committed Sep 20, 2015
2 parents 4429f56 + 096beb8 commit e64ae65
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 28 deletions.
33 changes: 23 additions & 10 deletions Source/ResponseSerialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public protocol ResponseSerializerType {
typealias ErrorObject: ErrorType

/**
A closure used by response handlers that takes a request, response, and data and returns a result.
A closure used by response handlers that takes a request, response, data and error and returns a result.
*/
var serializeResponse: (NSURLRequest?, NSHTTPURLResponse?, NSData?) -> Result<SerializedObject, ErrorObject> { get }
var serializeResponse: (NSURLRequest?, NSHTTPURLResponse?, NSData?, NSError?) -> Result<SerializedObject, ErrorObject> { get }
}

// MARK: -
Expand All @@ -53,9 +53,9 @@ public struct ResponseSerializer<Value, Error: ErrorType>: ResponseSerializerTyp
public typealias ErrorObject = Error

/**
A closure used by response handlers that takes a request, response, and data and returns a result.
A closure used by response handlers that takes a request, response, data and error and returns a result.
*/
public var serializeResponse: (NSURLRequest?, NSHTTPURLResponse?, NSData?) -> Result<Value, Error>
public var serializeResponse: (NSURLRequest?, NSHTTPURLResponse?, NSData?, NSError?) -> Result<Value, Error>

/**
Initializes the `ResponseSerializer` instance with the given serialize response closure.
Expand All @@ -64,7 +64,7 @@ public struct ResponseSerializer<Value, Error: ErrorType>: ResponseSerializerTyp

- returns: The new generic response serializer instance.
*/
public init(serializeResponse: (NSURLRequest?, NSHTTPURLResponse?, NSData?) -> Result<Value, Error>) {
public init(serializeResponse: (NSURLRequest?, NSHTTPURLResponse?, NSData?, NSError?) -> Result<Value, Error>) {
self.serializeResponse = serializeResponse
}
}
Expand Down Expand Up @@ -112,7 +112,12 @@ extension Request {
-> Self
{
delegate.queue.addOperationWithBlock {
let result = responseSerializer.serializeResponse(self.request, self.response, self.delegate.data)
let result = responseSerializer.serializeResponse(
self.request,
self.response,
self.delegate.data,
self.delegate.error
)

dispatch_async(queue ?? dispatch_get_main_queue()) {
let response = Response<T.SerializedObject, T.ErrorObject>(
Expand Down Expand Up @@ -140,7 +145,9 @@ extension Request {
- returns: A data response serializer.
*/
public static func dataResponseSerializer() -> ResponseSerializer<NSData, NSError> {
return ResponseSerializer { _, _, data in
return ResponseSerializer { _, _, data, error in
guard error == nil else { return .Failure(error!) }

guard let validData = data where validData.length > 0 else {
let failureReason = "Data could not be serialized. Input data was nil or zero length."
let error = Error.errorWithCode(.DataSerializationFailed, failureReason: failureReason)
Expand Down Expand Up @@ -180,7 +187,9 @@ extension Request {
var encoding encoding: NSStringEncoding? = nil)
-> ResponseSerializer<String, NSError>
{
return ResponseSerializer { _, response, data in
return ResponseSerializer { _, response, data, error in
guard error == nil else { return .Failure(error!) }

guard let validData = data where validData.length > 0 else {
let failureReason = "String could not be serialized. Input data was nil or zero length."
let error = Error.errorWithCode(.StringSerializationFailed, failureReason: failureReason)
Expand Down Expand Up @@ -243,7 +252,9 @@ extension Request {
options options: NSJSONReadingOptions = .AllowFragments)
-> ResponseSerializer<AnyObject, NSError>
{
return ResponseSerializer { _, _, data in
return ResponseSerializer { _, _, data, error in
guard error == nil else { return .Failure(error!) }

guard let validData = data where validData.length > 0 else {
let failureReason = "JSON could not be serialized. Input data was nil or zero length."
let error = Error.errorWithCode(.JSONSerializationFailed, failureReason: failureReason)
Expand Down Expand Up @@ -295,7 +306,9 @@ extension Request {
options options: NSPropertyListReadOptions = NSPropertyListReadOptions())
-> ResponseSerializer<AnyObject, NSError>
{
return ResponseSerializer { _, _, data in
return ResponseSerializer { _, _, data, error in
guard error == nil else { return .Failure(error!) }

guard let validData = data where validData.length > 0 else {
let failureReason = "Property list could not be serialized. Input data was nil or zero length."
let error = Error.errorWithCode(.PropertyListSerializationFailed, failureReason: failureReason)
Expand Down
117 changes: 99 additions & 18 deletions Tests/ResponseSerializationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import Foundation
import XCTest

class ResponseSerializationTestCase: BaseTestCase {
let error = NSError(domain: Error.Domain, code: -10000, userInfo: nil)

// MARK: - Data Response Serializer Tests

Expand All @@ -34,7 +35,7 @@ class ResponseSerializationTestCase: BaseTestCase {
let data = "data".dataUsingEncoding(NSUTF8StringEncoding)!

// When
let result = serializer.serializeResponse(nil, nil, data)
let result = serializer.serializeResponse(nil, nil, data, nil)

// Then
XCTAssertTrue(result.isSuccess, "result is success should be true")
Expand All @@ -47,7 +48,7 @@ class ResponseSerializationTestCase: BaseTestCase {
let serializer = Request.dataResponseSerializer()

// When
let result = serializer.serializeResponse(nil, nil, nil)
let result = serializer.serializeResponse(nil, nil, nil, nil)

// Then
XCTAssertTrue(result.isFailure, "result is failure should be true")
Expand All @@ -62,14 +63,34 @@ class ResponseSerializationTestCase: BaseTestCase {
}
}

func testThatDataResponseSerializerFailsWhenErrorIsNotNil() {
// Given
let serializer = Request.dataResponseSerializer()

// When
let result = serializer.serializeResponse(nil, nil, nil, error)

// Then
XCTAssertTrue(result.isFailure, "result is failure should be true")
XCTAssertNil(result.value, "result value should be nil")
XCTAssertNotNil(result.error, "result error should not be nil")

if let error = result.error {
XCTAssertEqual(error.domain, Error.Domain, "error domain should match expected value")
XCTAssertEqual(error.code, self.error.code, "error code should match expected value")
} else {
XCTFail("error should not be nil")
}
}

// MARK: - String Response Serializer Tests

func testThatStringResponseSerializerFailsWhenDataIsNil() {
// Given
let serializer = Request.stringResponseSerializer()

// When
let result = serializer.serializeResponse(nil, nil, nil)
let result = serializer.serializeResponse(nil, nil, nil, nil)

// Then
XCTAssertTrue(result.isFailure, "result is failure should be true")
Expand All @@ -89,7 +110,7 @@ class ResponseSerializationTestCase: BaseTestCase {
let serializer = Request.stringResponseSerializer()

// When
let result = serializer.serializeResponse(nil, nil, NSData())
let result = serializer.serializeResponse(nil, nil, NSData(), nil)

// Then
XCTAssertTrue(result.isFailure, "result is failure should be true")
Expand All @@ -102,7 +123,7 @@ class ResponseSerializationTestCase: BaseTestCase {
let data = "data".dataUsingEncoding(NSUTF8StringEncoding)!

// When
let result = serializer.serializeResponse(nil, nil, data)
let result = serializer.serializeResponse(nil, nil, data, nil)

// Then
XCTAssertTrue(result.isSuccess, "result is success should be true")
Expand All @@ -115,7 +136,7 @@ class ResponseSerializationTestCase: BaseTestCase {
let data = "data".dataUsingEncoding(NSUTF8StringEncoding)!

// When
let result = serializer.serializeResponse(nil, nil, data)
let result = serializer.serializeResponse(nil, nil, data, nil)

// Then
XCTAssertTrue(result.isSuccess, "result is success should be true")
Expand All @@ -134,7 +155,7 @@ class ResponseSerializationTestCase: BaseTestCase {
)

// When
let result = serializer.serializeResponse(nil, response, data)
let result = serializer.serializeResponse(nil, response, data, nil)

// Then
XCTAssertTrue(result.isSuccess, "result is success should be true")
Expand All @@ -148,7 +169,7 @@ class ResponseSerializationTestCase: BaseTestCase {
let data = "random data".dataUsingEncoding(NSUTF32StringEncoding)!

// When
let result = serializer.serializeResponse(nil, nil, data)
let result = serializer.serializeResponse(nil, nil, data, nil)

// Then
XCTAssertTrue(result.isFailure, "result is failure should be true")
Expand All @@ -175,7 +196,7 @@ class ResponseSerializationTestCase: BaseTestCase {
)

// When
let result = serializer.serializeResponse(nil, response, data)
let result = serializer.serializeResponse(nil, response, data, nil)

// Then
XCTAssertTrue(result.isFailure, "result is failure should be true")
Expand All @@ -190,14 +211,34 @@ class ResponseSerializationTestCase: BaseTestCase {
}
}

func testThatStringResponseSerializerFailsWhenErrorIsNotNil() {
// Given
let serializer = Request.stringResponseSerializer()

// When
let result = serializer.serializeResponse(nil, nil, nil, error)

// Then
XCTAssertTrue(result.isFailure, "result is failure should be true")
XCTAssertNil(result.value, "result value should be nil")
XCTAssertNotNil(result.error, "result error should not be nil")

if let error = result.error {
XCTAssertEqual(error.domain, Error.Domain, "error domain should match expected value")
XCTAssertEqual(error.code, self.error.code, "error code should match expected value")
} else {
XCTFail("error should not be nil")
}
}

// MARK: - JSON Response Serializer Tests

func testThatJSONResponseSerializerFailsWhenDataIsNil() {
// Given
let serializer = Request.JSONResponseSerializer()

// When
let result = serializer.serializeResponse(nil, nil, nil)
let result = serializer.serializeResponse(nil, nil, nil, nil)

// Then
XCTAssertTrue(result.isFailure, "result is failure should be true")
Expand All @@ -217,7 +258,7 @@ class ResponseSerializationTestCase: BaseTestCase {
let serializer = Request.JSONResponseSerializer()

// When
let result = serializer.serializeResponse(nil, nil, NSData())
let result = serializer.serializeResponse(nil, nil, NSData(), nil)

// Then
XCTAssertTrue(result.isFailure, "result is failure should be true")
Expand All @@ -238,7 +279,7 @@ class ResponseSerializationTestCase: BaseTestCase {
let data = "{\"json\": true}".dataUsingEncoding(NSUTF8StringEncoding)!

// When
let result = serializer.serializeResponse(nil, nil, data)
let result = serializer.serializeResponse(nil, nil, data, nil)

// Then
XCTAssertTrue(result.isSuccess, "result is success should be true")
Expand All @@ -252,7 +293,7 @@ class ResponseSerializationTestCase: BaseTestCase {
let data = "definitely not valid json".dataUsingEncoding(NSUTF8StringEncoding)!

// When
let result = serializer.serializeResponse(nil, nil, data)
let result = serializer.serializeResponse(nil, nil, data, nil)

// Then
XCTAssertTrue(result.isFailure, "result is failure should be true")
Expand All @@ -267,14 +308,34 @@ class ResponseSerializationTestCase: BaseTestCase {
}
}

func testThatJSONResponseSerializerFailsWhenErrorIsNotNil() {
// Given
let serializer = Request.JSONResponseSerializer()

// When
let result = serializer.serializeResponse(nil, nil, nil, error)

// Then
XCTAssertTrue(result.isFailure, "result is failure should be true")
XCTAssertNil(result.value, "result value should be nil")
XCTAssertNotNil(result.error, "result error should not be nil")

if let error = result.error {
XCTAssertEqual(error.domain, Error.Domain, "error domain should match expected value")
XCTAssertEqual(error.code, self.error.code, "error code should match expected value")
} else {
XCTFail("error should not be nil")
}
}

// MARK: - Property List Response Serializer Tests

func testThatPropertyListResponseSerializerFailsWhenDataIsNil() {
// Given
let serializer = Request.propertyListResponseSerializer()

// When
let result = serializer.serializeResponse(nil, nil, nil)
let result = serializer.serializeResponse(nil, nil, nil, nil)

// Then
XCTAssertTrue(result.isFailure, "result is failure should be true")
Expand All @@ -294,7 +355,7 @@ class ResponseSerializationTestCase: BaseTestCase {
let serializer = Request.propertyListResponseSerializer()

// When
let result = serializer.serializeResponse(nil, nil, NSData())
let result = serializer.serializeResponse(nil, nil, NSData(), nil)

// Then
XCTAssertTrue(result.isFailure, "result is failure should be true")
Expand All @@ -315,7 +376,7 @@ class ResponseSerializationTestCase: BaseTestCase {
let data = NSKeyedArchiver.archivedDataWithRootObject(["foo": "bar"])

// When
let result = serializer.serializeResponse(nil, nil, data)
let result = serializer.serializeResponse(nil, nil, data, nil)

// Then
XCTAssertTrue(result.isSuccess, "result is success should be true")
Expand All @@ -325,11 +386,11 @@ class ResponseSerializationTestCase: BaseTestCase {

func testThatPropertyListResponseSerializerFailsWhenDataIsInvalidPropertyListData() {
// Given
let serializer = Request.JSONResponseSerializer()
let serializer = Request.propertyListResponseSerializer()
let data = "definitely not valid plist data".dataUsingEncoding(NSUTF8StringEncoding)!

// When
let result = serializer.serializeResponse(nil, nil, data)
let result = serializer.serializeResponse(nil, nil, data, nil)

// Then
XCTAssertTrue(result.isFailure, "result is failure should be true")
Expand All @@ -343,4 +404,24 @@ class ResponseSerializationTestCase: BaseTestCase {
XCTFail("error should not be nil")
}
}

func testThatPropertyListResponseSerializerFailsWhenErrorIsNotNil() {
// Given
let serializer = Request.propertyListResponseSerializer()

// When
let result = serializer.serializeResponse(nil, nil, nil, error)

// Then
XCTAssertTrue(result.isFailure, "result is failure should be true")
XCTAssertNil(result.value, "result value should be nil")
XCTAssertNotNil(result.error, "result error should not be nil")

if let error = result.error {
XCTAssertEqual(error.domain, Error.Domain, "error domain should match expected value")
XCTAssertEqual(error.code, self.error.code, "error code should match expected value")
} else {
XCTFail("error should not be nil")
}
}
}

0 comments on commit e64ae65

Please sign in to comment.