[Swift] Handle String responses

This commit is contained in:
Jason Gavris 2016-08-23 10:15:47 -04:00
parent 3a457c6a7e
commit e89f914e8b
5 changed files with 63 additions and 36 deletions

View File

@ -89,6 +89,26 @@ class AlamofireRequestBuilder<T>: RequestBuilder<T> {
let validatedRequest = request.validate()
switch T.self {
case is String.Type:
validatedRequest.responseString(completionHandler: { (stringResponse) in
cleanupRequest()
if stringResponse.result.isFailure {
completion(
response: nil,
error: ErrorResponse.Error(stringResponse.response?.statusCode ?? 500, stringResponse.data, stringResponse.result.error!)
)
return
}
completion(
response: Response(
response: stringResponse.response!,
body: (stringResponse.result.value ?? "") as! T
),
error: nil
)
})
case is Void.Type:
validatedRequest.responseData(completionHandler: { (voidResponse) in
cleanupRequest()

View File

@ -89,6 +89,26 @@ class AlamofireRequestBuilder<T>: RequestBuilder<T> {
let validatedRequest = request.validate()
switch T.self {
case is String.Type:
validatedRequest.responseString(completionHandler: { (stringResponse) in
cleanupRequest()
if stringResponse.result.isFailure {
completion(
response: nil,
error: ErrorResponse.Error(stringResponse.response?.statusCode ?? 500, stringResponse.data, stringResponse.result.error!)
)
return
}
completion(
response: Response(
response: stringResponse.response!,
body: (stringResponse.result.value ?? "") as! T
),
error: nil
)
})
case is Void.Type:
validatedRequest.responseData(completionHandler: { (voidResponse) in
cleanupRequest()

View File

@ -28,19 +28,12 @@ class UserAPITests: XCTestCase {
let expectation = self.expectationWithDescription("testLogin")
UserAPI.loginUser(username: "swiftTester", password: "swift") { (_, error) in
// The server isn't returning JSON - and currently the alamofire implementation
// always parses responses as JSON, so making an exception for this here
guard let error = error else {
guard error == nil else {
XCTFail("error logging in")
return
}
switch error {
case ErrorResponse.Error(200, _, _):
expectation.fulfill()
default:
XCTFail("error logging in")
}
expectation.fulfill()
}
self.waitForExpectationsWithTimeout(testTimeout, handler: nil)

View File

@ -89,6 +89,26 @@ class AlamofireRequestBuilder<T>: RequestBuilder<T> {
let validatedRequest = request.validate()
switch T.self {
case is String.Type:
validatedRequest.responseString(completionHandler: { (stringResponse) in
cleanupRequest()
if stringResponse.result.isFailure {
completion(
response: nil,
error: ErrorResponse.Error(stringResponse.response?.statusCode ?? 500, stringResponse.data, stringResponse.result.error!)
)
return
}
completion(
response: Response(
response: stringResponse.response!,
body: (stringResponse.result.value ?? "") as! T
),
error: nil
)
})
case is Void.Type:
validatedRequest.responseData(completionHandler: { (voidResponse) in
cleanupRequest()

View File

@ -14,37 +14,11 @@ import XCTest
class UserAPITests: XCTestCase {
let testTimeout = 10.0
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testLogin() {
let expectation = self.expectationWithDescription("testLogin")
UserAPI.loginUser(username: "swiftTester", password: "swift").then { _ -> Void in
expectation.fulfill()
}.always {
// Noop for now
}.error { errorType -> Void in
// The server isn't returning JSON - and currently the alamofire implementation
// always parses responses as JSON, so making an exception for this here
guard let error = errorType as? ErrorResponse else {
XCTFail("error logging in")
return
}
switch error {
case ErrorResponse.Error(200, _, _):
expectation.fulfill()
default:
XCTFail("error logging in")
}
}
self.waitForExpectationsWithTimeout(testTimeout, handler: nil)
}