From 2ee0fdec665eb8d0da5fe24adbc0763ac1475507 Mon Sep 17 00:00:00 2001 From: Stefano Mondino Date: Fri, 28 Jan 2022 18:19:10 +0100 Subject: [PATCH 1/2] critical fix: encoding parameter set to AF request --- Sources/swift-algorand-sdk/v2/client/Request.swift | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Sources/swift-algorand-sdk/v2/client/Request.swift b/Sources/swift-algorand-sdk/v2/client/Request.swift index c5608f2..e30cdcb 100644 --- a/Sources/swift-algorand-sdk/v2/client/Request.swift +++ b/Sources/swift-algorand-sdk/v2/client/Request.swift @@ -50,10 +50,11 @@ public struct RequestParameters { components.queryItems = queryParameters.map { URLQueryItem(name: $0.key, value: $0.value) } let headers = self.headers.merging(client.defaultHTTPHeaders) { _, global in global } return client.session.request(components.url?.absoluteString ?? "", - method: method, - parameters: bodyParameters, - headers: .init(headers), - requestModifier: { $0.timeoutInterval = 120.0 }) + method: method, + parameters: bodyParameters, + encoding: encoding, + headers: .init(headers), + requestModifier: { $0.timeoutInterval = 120.0 }) .validate() } } From 59afce65408f2ceeec3019115537181221ab252b Mon Sep 17 00:00:00 2001 From: Stefano Mondino Date: Fri, 28 Jan 2022 19:25:35 +0100 Subject: [PATCH 2/2] setup for json mocks + versions endpoint mocked --- .../xcschemes/swift-algorand-sdk.xcscheme | 14 +++++++++++++ Package.swift | 3 ++- .../AlgodClientTests.swift | 12 +++++++++-- Tests/swift-algorand-sdkTests/HTTPMock.swift | 21 +++++++++++++++++-- .../mocks/algod/versions.json | 16 ++++++++++++++ 5 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 Tests/swift-algorand-sdkTests/mocks/algod/versions.json diff --git a/.swiftpm/xcode/xcshareddata/xcschemes/swift-algorand-sdk.xcscheme b/.swiftpm/xcode/xcshareddata/xcschemes/swift-algorand-sdk.xcscheme index a625d20..f39cc17 100644 --- a/.swiftpm/xcode/xcshareddata/xcschemes/swift-algorand-sdk.xcscheme +++ b/.swiftpm/xcode/xcshareddata/xcschemes/swift-algorand-sdk.xcscheme @@ -48,6 +48,20 @@ ReferencedContainer = "container:"> + + + + (for request: CustomRequest, + json: String? = nil, with mock: CustomRequest.ResponseType, file: StaticString = #filePath, line: UInt = #line, comparing compare: @escaping(CustomRequest.ResponseType?, CustomRequest.ResponseType?) -> Void) where CustomRequest: swift_algorand_sdk.Request { - MockURLProtocol.response(for: request, with: mock) + if let json = json, + let url = Bundle.module.url(forResource: json, withExtension: "json"), + let data = try? Data(contentsOf: url) { + MockURLProtocol.response(for: request, with: data) + } else { + MockURLProtocol.response(for: request, with: mock) + } let expectation = XCTestExpectation(description: "Performing request for \(request.self)") request.execute { response in compare(response.data, mock) @@ -26,11 +33,13 @@ extension XCTestCase { wait(for: [expectation], timeout: 1) } func assertSuccessfulResponse(for request: CustomRequest, + json: String? = nil, with mock: CustomRequest.ResponseType, file: StaticString = #filePath, line: UInt = #line) where CustomRequest: swift_algorand_sdk.Request, CustomRequest.ResponseType: Equatable { assertSuccessfulResponse(for: request, + json: json, with: mock, file: file, line: line, @@ -116,11 +125,19 @@ final class MockURLProtocol: URLProtocol { with mock: CustomRequest.ResponseType) where CustomRequest: swift_algorand_sdk.Request { let encoder = JSONEncoder() + let data = try? encoder.encode(mock) + self.response(for: request, with: data) + } + + static func response(for request: CustomRequest, + with data: Data?) + where CustomRequest: swift_algorand_sdk.Request { + MockURLProtocol.responseType = MockURLProtocol.ResponseType .success(HTTPURLResponse(url: URL(string: "https://something.useless")!, statusCode: 200, httpVersion: nil, headerFields: nil)!, - try? encoder.encode(mock)) + data) } } diff --git a/Tests/swift-algorand-sdkTests/mocks/algod/versions.json b/Tests/swift-algorand-sdkTests/mocks/algod/versions.json new file mode 100644 index 0000000..89853d4 --- /dev/null +++ b/Tests/swift-algorand-sdkTests/mocks/algod/versions.json @@ -0,0 +1,16 @@ +{ + "genesis_hash_b64": "123", + "genesis_id": "1234", + "versions": [ + "1.0", + "2.0" + ], + "build": { + "branch": "theBranch", + "build_number": 123, + "channel": "theChannel", + "commit_hash": "theCommitHash", + "major": 123, + "minor": 123 + } +}