diff --git a/Tests/Integration/Client/UpholdClientTest.swift b/Tests/Integration/Client/UpholdClientTest.swift new file mode 100644 index 0000000..9eecd8a --- /dev/null +++ b/Tests/Integration/Client/UpholdClientTest.swift @@ -0,0 +1,93 @@ +import XCTest +import ObjectMapper +import PromiseKit +@testable import UpholdSdk +@testable import SwiftClient + +/// UpholdClient integration tests. +class UpholdClientTest: UpholdTestCase { + + func testGetTickersShouldReturnTheArrayOfTickers() { + let expectation = expectationWithDescription("Uphold client test.") + + let json: String = "[" + + "{" + + "\"ask\": \"foo\"," + + "\"bid\": \"bar\"," + + "\"currency\": \"foobar\"," + + "\"pair\": \"foobiz\"" + + "}, {" + + "\"ask\": \"fiz\"," + + "\"bid\": \"biz\"," + + "\"currency\": \"foobiz\"," + + "\"pair\": \"bar\"" + + "}, {" + + "\"ask\": \"foobar\"," + + "\"bid\": \"foobaz\"," + + "\"currency\": \"bar\"," + + "\"pair\": \"foo\"" + + "}" + + "]" + + let client = UpholdClient() + client.token.adapter = MockRestAdapter(body: json) + + client.getTickers().then { (rates: [Rate]) -> () in + XCTAssertEqual(rates.count, 3, "Failed: Wrong response object size.") + XCTAssertEqual(rates[0].ask, "foo", "Failed: Wrong response object attribute.") + XCTAssertEqual(rates[0].bid, "bar", "Failed: Wrong response object attribute.") + XCTAssertEqual(rates[0].currency, "foobar", "Failed: Wrong response object attribute.") + XCTAssertEqual(rates[0].pair, "foobiz", "Failed: Wrong response object attribute.") + XCTAssertEqual(rates[1].ask, "fiz", "Failed: Wrong response object attribute.") + XCTAssertEqual(rates[1].bid, "biz", "Failed: Wrong response object attribute.") + XCTAssertEqual(rates[1].currency, "foobiz", "Failed: Wrong response object attribute.") + XCTAssertEqual(rates[1].pair, "bar", "Failed: Wrong response object attribute.") + XCTAssertEqual(rates[2].ask, "foobar", "Failed: Wrong response object attribute.") + XCTAssertEqual(rates[2].bid, "foobaz", "Failed: Wrong response object attribute.") + XCTAssertEqual(rates[2].currency, "bar", "Failed: Wrong response object attribute.") + XCTAssertEqual(rates[2].pair, "foo", "Failed: Wrong response object attribute.") + + expectation.fulfill() + } + + wait() + } + + func testGetTickersByCurrencyShouldReturnTheArrayOfTickers() { + let expectation = expectationWithDescription("Uphold client test.") + + let json: String = "[" + + "{" + + "\"ask\": \"foo\"," + + "\"bid\": \"bar\"," + + "\"currency\": \"foobar\"," + + "\"pair\": \"foobiz\"" + + "}, {" + + "\"ask\": \"fiz\"," + + "\"bid\": \"biz\"," + + "\"currency\": \"foobiz\"," + + "\"pair\": \"bar\"" + + "}, {" + + "\"ask\": \"foobar\"," + + "\"bid\": \"foobaz\"," + + "\"currency\": \"bar\"," + + "\"pair\": \"foo\"" + + "}" + + "]" + + let client = UpholdClient() + client.token.adapter = MockRestAdapter(body: json) + + client.getTickersByCurrency("USD").then { (rates: [Rate]) -> () in + XCTAssertEqual(rates.count, 3, "Failed: Wrong response object size.") + XCTAssertEqual(rates[0].ask, "foo", "Failed: Wrong response object attribute.") + XCTAssertEqual(rates[1].ask, "fiz", "Failed: Wrong response object attribute.") + XCTAssertEqual(rates[2].ask, "foobar", "Failed: Wrong response object attribute.") + + expectation.fulfill() + } + + wait() + } + +} diff --git a/Tests/Integration/Client/UpholdRestAdapterTest.swift b/Tests/Integration/Client/UpholdRestAdapterTest.swift index df9b6d2..502d281 100644 --- a/Tests/Integration/Client/UpholdRestAdapterTest.swift +++ b/Tests/Integration/Client/UpholdRestAdapterTest.swift @@ -7,8 +7,6 @@ import UpholdSdk /// UpholdRestAdapter integration tests. class UpholdRestAdapterTest: UpholdTestCase { - var expectation: XCTestExpectation! - func testBuildRequestShouldReturnRequest() { let mockRequest = MockRequest(body: nil, code: 200, errorHandler: {(error: NSError) -> Void in}, headers: nil, method: "foo") let request = UpholdRestAdapter().buildRequest(mockRequest) @@ -19,7 +17,7 @@ class UpholdRestAdapterTest: UpholdTestCase { } func testBuildEmptyResponseShouldReturnBadRequestError() { - self.expectation = expectationWithDescription("REST adapter response.") + let expectation = expectationWithDescription("Uphold REST adapter response test.") let mockRequest = MockRequest(body: nil, code: 400, errorHandler: {(error: NSError) -> Void in}, headers: nil, method: "foo") let request = UpholdRestAdapter().buildRequest(mockRequest) @@ -36,7 +34,7 @@ class UpholdRestAdapterTest: UpholdTestCase { XCTAssertEqual(badRequestError.code, 400, "Failed: Wrong response HTTP status code.") XCTAssertEqual(badRequestError.info["Bad request error"], "HTTP error 400 - Bad request.", "Failed: Wrong message.") - self.expectation.fulfill() + expectation.fulfill() return promise } @@ -45,7 +43,7 @@ class UpholdRestAdapterTest: UpholdTestCase { } func testBuildEmptyResponseShouldReturnFulfilledPromise() { - self.expectation = expectationWithDescription("REST adapter response.") + let expectation = expectationWithDescription("Uphold REST adapter response test.") let mockRequest = MockRequest(body: nil, code: 200, errorHandler: {(error: NSError) -> Void in}, headers: nil, method: "foo") let request = UpholdRestAdapter().buildRequest(mockRequest) @@ -55,14 +53,14 @@ class UpholdRestAdapterTest: UpholdTestCase { XCTAssertEqual(response.basicStatus, Response.BasicResponseType.OK, "Failed: Wrong response basic status code.") XCTAssertNil(response.text, "Failed: Wrong response body.") - self.expectation.fulfill() + expectation.fulfill() } wait() } func testBuildEmptyResponseShouldReturnLogicError() { - self.expectation = expectationWithDescription("REST adapter response.") + let expectation = expectationWithDescription("Uphold REST adapter response test.") let mockRequest = MockRequest(body: "foobar", code: 200, errorHandler: {(error: NSError) -> Void in}, headers: nil, method: "foo") let request = UpholdRestAdapter().buildRequest(mockRequest) @@ -79,7 +77,7 @@ class UpholdRestAdapterTest: UpholdTestCase { XCTAssertEqual(logicError.info, ["Logic error": "Response body should be empty."], "Failed: Wrong error message.") XCTAssertNil(logicError.code, "Failed: Wrong response HTTP status code.") - self.expectation.fulfill() + expectation.fulfill() return promise } @@ -88,15 +86,15 @@ class UpholdRestAdapterTest: UpholdTestCase { } func testBuildResponseShouldReturnBadRequestError() { - self.expectation = expectationWithDescription("REST adapter response.") + let expectation = expectationWithDescription("Uphold REST adapter response test.") - let jsonRate = "{" + + let json = "{" + "\"ask\":\"1.2\"," + "\"bid\":\"1\"," + "\"currency\":\"BTC\"," + "\"pair\":\"BTCBTC\"" + "}" - let mockRequest = MockRequest(body: jsonRate, code: 400, errorHandler: {(error: NSError) -> Void in}, headers: nil, method: "foo") + let mockRequest = MockRequest(body: json, code: 400, errorHandler: {(error: NSError) -> Void in}, headers: nil, method: "foo") let request = UpholdRestAdapter().buildRequest(mockRequest) let promise: Promise = UpholdRestAdapter().buildResponse(request) @@ -111,7 +109,7 @@ class UpholdRestAdapterTest: UpholdTestCase { XCTAssertEqual(badRequestError.code, 400, "Failed: Wrong response HTTP status code.") XCTAssertEqual(badRequestError.info["Bad request error"], "HTTP error 400 - Bad request.", "Failed: Wrong message.") - self.expectation.fulfill() + expectation.fulfill() return promise } @@ -120,7 +118,7 @@ class UpholdRestAdapterTest: UpholdTestCase { } func testBuildResponseShouldReturnEmptyBodyLogicError() { - self.expectation = expectationWithDescription("REST adapter response.") + let expectation = expectationWithDescription("Uphold REST adapter response test.") let mockRequest = MockRequest(body: nil, code: 200, errorHandler: {(error: NSError) -> Void in}, headers: nil, method: "foo") let request = UpholdRestAdapter().buildRequest(mockRequest) @@ -137,7 +135,7 @@ class UpholdRestAdapterTest: UpholdTestCase { XCTAssertEqual(logicError.info, ["Logic error": "Response body should not be empty."], "Failed: Wrong error message.") XCTAssertNil(logicError.code, "Failed: Wrong response HTTP status code.") - self.expectation.fulfill() + expectation.fulfill() return promise } @@ -146,15 +144,15 @@ class UpholdRestAdapterTest: UpholdTestCase { } func testBuildResponseShouldReturnFailedMapLogicError() { - self.expectation = expectationWithDescription("REST adapter response.") + let expectation = expectationWithDescription("Uphold REST adapter response test.") - let jsonRate = "{[" + + let json = "{[" + "\"ask\":\"1.2\"," + "\"bid\":\"1\"," + "\"currency\":\"BTC\"," + "\"pair\":\"BTCBTC\"" + "}" - let mockRequest = MockRequest(body: jsonRate, code: 200, errorHandler: {(error: NSError) -> Void in}, headers: nil, method: "foo") + let mockRequest = MockRequest(body: json, code: 200, errorHandler: {(error: NSError) -> Void in}, headers: nil, method: "foo") let request = UpholdRestAdapter().buildRequest(mockRequest) let promise: Promise = UpholdRestAdapter().buildResponse(request) @@ -169,7 +167,7 @@ class UpholdRestAdapterTest: UpholdTestCase { XCTAssertEqual(logicError.info, ["Logic error": "Failed to map the JSON object."], "Failed: Wrong error message.") XCTAssertNil(logicError.code, "Failed: Wrong response HTTP status code.") - self.expectation.fulfill() + expectation.fulfill() return promise } @@ -178,15 +176,15 @@ class UpholdRestAdapterTest: UpholdTestCase { } func testBuildResponseShouldReturnFulfilledPromise() { - self.expectation = expectationWithDescription("REST adapter response.") + let expectation = expectationWithDescription("Uphold REST adapter response test.") - let jsonRate = "{" + + let json = "{" + "\"ask\":\"1.2\"," + "\"bid\":\"1\"," + "\"currency\":\"BTC\"," + "\"pair\":\"BTCBTC\"" + "}" - let mockRequest = MockRequest(body: jsonRate, code: 200, errorHandler: {(error: NSError) -> Void in}, headers: nil, method: "foo") + let mockRequest = MockRequest(body: json, code: 200, errorHandler: {(error: NSError) -> Void in}, headers: nil, method: "foo") let request = UpholdRestAdapter().buildRequest(mockRequest) let promise: Promise = UpholdRestAdapter().buildResponse(request) @@ -196,17 +194,17 @@ class UpholdRestAdapterTest: UpholdTestCase { XCTAssertEqual(rate.currency, "BTC", "Failed: Wrong response object attribute.") XCTAssertEqual(rate.pair, "BTCBTC", "Failed: Wrong response object attribute.") - self.expectation.fulfill() + expectation.fulfill() } wait() } func testBuildResponseArrayShouldReturnBadRequestError() { - self.expectation = expectationWithDescription("REST adapter response.") + let expectation = expectationWithDescription("Uphold REST adapter response test.") - let jsonRates = "[{\"ask\":\"1\"}, {\"ask\":\"440.99\"}]" - let mockRequest = MockRequest(body: jsonRates, code: 400, errorHandler: {(error: NSError) -> Void in}, headers: nil, method: "foo") + let json = "[{\"ask\":\"1\"}, {\"ask\":\"440.99\"}]" + let mockRequest = MockRequest(body: json, code: 400, errorHandler: {(error: NSError) -> Void in}, headers: nil, method: "foo") let request = UpholdRestAdapter().buildRequest(mockRequest) let promise: Promise<[Rate]> = UpholdRestAdapter().buildResponse(request) @@ -221,7 +219,7 @@ class UpholdRestAdapterTest: UpholdTestCase { XCTAssertEqual(badRequestError.code, 400, "Failed: Wrong response HTTP status code.") XCTAssertEqual(badRequestError.info["Bad request error"], "HTTP error 400 - Bad request.", "Failed: Wrong message.") - self.expectation.fulfill() + expectation.fulfill() return promise } @@ -230,7 +228,7 @@ class UpholdRestAdapterTest: UpholdTestCase { } func testBuildResponseArrayShouldReturnEmptyBodyLogicError() { - self.expectation = expectationWithDescription("REST adapter response.") + let expectation = expectationWithDescription("Uphold REST adapter response test.") let mockRequest = MockRequest(body: nil, code: 200, errorHandler: {(error: NSError) -> Void in}, headers: nil, method: "foo") let request = UpholdRestAdapter().buildRequest(mockRequest) @@ -247,7 +245,7 @@ class UpholdRestAdapterTest: UpholdTestCase { XCTAssertEqual(logicError.info, ["Logic error": "Response body should not be empty."], "Failed: Wrong error message.") XCTAssertNil(logicError.code, "Failed: Wrong response HTTP status code.") - self.expectation.fulfill() + expectation.fulfill() return promise } @@ -256,7 +254,7 @@ class UpholdRestAdapterTest: UpholdTestCase { } func testBuildResponseArrayShouldReturnFailedMapLogicError() { - self.expectation = expectationWithDescription("REST adapter response.") + let expectation = expectationWithDescription("Uphold REST adapter response test.") let jsonRates = "[[{\"ask\":\"1\"}, {\"ask\":\"440.99\"}]" let mockRequest = MockRequest(body: jsonRates, code: 200, errorHandler: {(error: NSError) -> Void in}, headers: nil, method: "foo") @@ -274,7 +272,7 @@ class UpholdRestAdapterTest: UpholdTestCase { XCTAssertEqual(logicError.info, ["Logic error": "Failed to map the JSON object."], "Failed: Wrong error message.") XCTAssertNil(logicError.code, "Failed: Wrong response HTTP status code.") - self.expectation.fulfill() + expectation.fulfill() return promise } @@ -283,7 +281,7 @@ class UpholdRestAdapterTest: UpholdTestCase { } func testBuildResponseArrayShouldReturnFulfilledPromise() { - self.expectation = expectationWithDescription("REST adapter response.") + let expectation = expectationWithDescription("Uphold REST adapter response test.") let jsonRates = "[{\"ask\":\"1\"}, {\"ask\":\"440.99\"}]" let mockRequest = MockRequest(body: jsonRates, code: 200, errorHandler: {(error: NSError) -> Void in}, headers: nil, method: "foo") @@ -295,7 +293,7 @@ class UpholdRestAdapterTest: UpholdTestCase { XCTAssertEqual(rates[0].ask, "1", "Failed: Wrong response object attribute.") XCTAssertEqual(rates[1].ask, "440.99", "Failed: Wrong response object attribute.") - self.expectation.fulfill() + expectation.fulfill() } wait() diff --git a/Tests/Util/MockRequest.swift b/Tests/Util/MockRequest.swift index 69655b3..f334a66 100644 --- a/Tests/Util/MockRequest.swift +++ b/Tests/Util/MockRequest.swift @@ -53,28 +53,22 @@ public class MockRequest: Request { return .Success(response, body.dataUsingEncoding(NSUTF8StringEncoding)) } - /// Mocked SwiftClient Request class end method. + /** + Mock SwiftClient Request class end method. + + - parameter done: The completion handler. + - parameter errorHandler: The error handler. + */ public override func end(done: (SwiftClient.Response) -> Void, onError errorHandler: ((NSError) -> Void)? = nil) { let request = NSMutableURLRequest(URL: NSURL(string: self.mockURL)!) request.HTTPMethod = super.method - let response = builder(request) - let stubError = NSError(domain: "Stub error", code: -1, userInfo: ["Stub error": "Could not create stub."]) - switch response { + switch builder(request) { case let .Success(response, data): - guard let mockResponse = response as? NSHTTPURLResponse else { - errorHandler!(stubError) - - return - } + return done(self.transformer(SwiftClient.Response((response as? NSHTTPURLResponse)!, self, data))) - done(self.transformer(SwiftClient.Response(mockResponse, self, data))) - - break default: - errorHandler!(stubError) - - break + return } } diff --git a/Tests/Util/MockRestAdapter.swift b/Tests/Util/MockRestAdapter.swift new file mode 100644 index 0000000..f037628 --- /dev/null +++ b/Tests/Util/MockRestAdapter.swift @@ -0,0 +1,48 @@ +import Foundation +import ObjectMapper +import PromiseKit +import UpholdSdk +@testable import SwiftClient + +/// Uphold mock REST adapter. +public class MockRestAdapter: UpholdRestAdapter { + + /// The body to inject in the response. + let body: String + + /** + Constructor. + + - parameter body: The body to mock the response. + */ + public init(body: String) { + self.body = body + } + + /** + Mock response builder method. + + - parameter request: The HTTP request. + + - returns: The mock response. + */ + public override func buildResponse(request: Request) -> Promise { + let mockRequest = MockRequest(body: self.body, code: 200, errorHandler: {(error: NSError) -> Void in}, headers: nil, method: "foo") + + return UpholdRestAdapter().buildResponse(mockRequest) + } + + /** + Mock response builder method. + + - parameter request: The HTTP request. + + - returns: The mock response. + */ + public override func buildResponse(request: Request) -> Promise<[T]> { + let mockRequest = MockRequest(body: self.body, code: 200, errorHandler: {(error: NSError) -> Void in}, headers: nil, method: "foo") + + return UpholdRestAdapter().buildResponse(mockRequest) + } + +} diff --git a/UpholdSdk.xcodeproj/project.pbxproj b/UpholdSdk.xcodeproj/project.pbxproj index 34ecf8f..92edec4 100644 --- a/UpholdSdk.xcodeproj/project.pbxproj +++ b/UpholdSdk.xcodeproj/project.pbxproj @@ -42,6 +42,8 @@ BC4A30701BEA657C001B6249 /* BaseModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC4A306F1BEA657C001B6249 /* BaseModel.swift */; }; BC5C0A851BD4F77700CEF466 /* MockRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC5C0A841BD4F77700CEF466 /* MockRequest.swift */; }; BC622E991BD546CA007E767C /* UpholdRestAdapterTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC622E981BD546CA007E767C /* UpholdRestAdapterTest.swift */; }; + BC7E73FD1BEA7BB6006AAE0E /* UpholdClientTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC7E73FC1BEA7BB6006AAE0E /* UpholdClientTest.swift */; }; + BC7E74121BEA80D6006AAE0E /* MockRestAdapter.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC7E74111BEA80D6006AAE0E /* MockRestAdapter.swift */; }; BC8E25741BCFBB4F00C15899 /* ApiLimitExceedError.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC8E25731BCFBB4F00C15899 /* ApiLimitExceedError.swift */; }; BCB089911BD52DC20097C475 /* UpholdRestAdapter.swift in Sources */ = {isa = PBXBuildFile; fileRef = BCB089901BD52DC20097C475 /* UpholdRestAdapter.swift */; }; BCB089B81BD52F270097C475 /* PromiseKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BCB089A71BD52EEC0097C475 /* PromiseKit.framework */; }; @@ -254,6 +256,8 @@ BC4A306F1BEA657C001B6249 /* BaseModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BaseModel.swift; sourceTree = ""; }; BC5C0A841BD4F77700CEF466 /* MockRequest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MockRequest.swift; path = Util/MockRequest.swift; sourceTree = ""; }; BC622E981BD546CA007E767C /* UpholdRestAdapterTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = UpholdRestAdapterTest.swift; path = Client/UpholdRestAdapterTest.swift; sourceTree = ""; }; + BC7E73FC1BEA7BB6006AAE0E /* UpholdClientTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = UpholdClientTest.swift; path = Client/UpholdClientTest.swift; sourceTree = ""; }; + BC7E74111BEA80D6006AAE0E /* MockRestAdapter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MockRestAdapter.swift; path = Util/MockRestAdapter.swift; sourceTree = ""; }; BC8E25731BCFBB4F00C15899 /* ApiLimitExceedError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ApiLimitExceedError.swift; sourceTree = ""; }; BCB089901BD52DC20097C475 /* UpholdRestAdapter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UpholdRestAdapter.swift; sourceTree = ""; }; BCB0899A1BD52EEC0097C475 /* PromiseKit.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = PromiseKit.xcodeproj; path = Carthage/Checkouts/PromiseKit/PromiseKit.xcodeproj; sourceTree = ""; }; @@ -475,6 +479,7 @@ children = ( BC5C0A841BD4F77700CEF466 /* MockRequest.swift */, BC4473C41BD64E2B003B4196 /* UpholdTestCase.swift */, + BC7E74111BEA80D6006AAE0E /* MockRestAdapter.swift */, ); name = Util; sourceTree = ""; @@ -529,6 +534,7 @@ isa = PBXGroup; children = ( BC622E981BD546CA007E767C /* UpholdRestAdapterTest.swift */, + BC7E73FC1BEA7BB6006AAE0E /* UpholdClientTest.swift */, ); name = Client; sourceTree = ""; @@ -890,8 +896,10 @@ BC38DA001BB97E4F0044239A /* TickerServiceTest.swift in Sources */, BC2381821BB4627C0060CC80 /* TransactionTest.swift in Sources */, BC2381801BB4627C0060CC80 /* CardTest.swift in Sources */, + BC7E73FD1BEA7BB6006AAE0E /* UpholdClientTest.swift in Sources */, BCC9E0B01BD10F4300C5C3C8 /* HeaderTest.swift in Sources */, BC38D9FF1BB97E4F0044239A /* ReserveServiceTest.swift in Sources */, + BC7E74121BEA80D6006AAE0E /* MockRestAdapter.swift in Sources */, BC622E991BD546CA007E767C /* UpholdRestAdapterTest.swift in Sources */, BC2381811BB4627C0060CC80 /* RateTest.swift in Sources */, BC5C0A851BD4F77700CEF466 /* MockRequest.swift in Sources */,