From 916f8d98431928902597c1bee26c27dd39ed07d5 Mon Sep 17 00:00:00 2001 From: Diogo Guimaraes Date: Thu, 18 Feb 2016 18:46:51 +0000 Subject: [PATCH] Support empty response bodies --- Source/Client/UpholdRestAdapter.swift | 7 +++---- .../Client/UpholdRestAdapterTest.swift | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/Source/Client/UpholdRestAdapter.swift b/Source/Client/UpholdRestAdapter.swift index 47c8b03..1a5de14 100644 --- a/Source/Client/UpholdRestAdapter.swift +++ b/Source/Client/UpholdRestAdapter.swift @@ -45,13 +45,12 @@ public class UpholdRestAdapter { return } - guard let _ = response.text else { + if let body = response.text where !body.isEmpty { + reject(LogicError(code: nil, message: "Response body should be empty.")) + } else { fulfill(response) - - return } - reject(LogicError(code: nil, message: "Response body should be empty.")) }, onError: { (error: NSError) -> Void in reject(error) }) diff --git a/Tests/Integration/Client/UpholdRestAdapterTest.swift b/Tests/Integration/Client/UpholdRestAdapterTest.swift index d8207db..13b3451 100644 --- a/Tests/Integration/Client/UpholdRestAdapterTest.swift +++ b/Tests/Integration/Client/UpholdRestAdapterTest.swift @@ -56,6 +56,22 @@ class UpholdRestAdapterTest: UpholdTestCase { wait() } + func testBuildEmptyResponseWithBodyShouldReturnFulfilledPromise() { + let expectation = expectationWithDescription("Uphold REST adapter response test.") + let mockRequest = MockRequest(body: "", code: 200, errorHandler: {(error: NSError) -> Void in}, headers: nil, method: "foo") + let request = UpholdRestAdapter().buildRequest(mockRequest) + let promise: Promise = UpholdRestAdapter().buildEmptyResponse(request) + + promise.then { (response: Response) -> () in + XCTAssertEqual(response.basicStatus, Response.BasicResponseType.OK, "Failed: Wrong response basic status code.") + XCTAssertEqual(response.text, "", "Failed: Wrong response body.") + + expectation.fulfill() + } + + wait() + } + func testBuildEmptyResponseShouldReturnLogicError() { let expectation = expectationWithDescription("Uphold REST adapter response test.") let mockRequest = MockRequest(body: "foobar", code: 200, errorHandler: {(error: NSError) -> Void in}, headers: nil, method: "foo")