Skip to content

Commit

Permalink
Merge pull request #24 from stefanomondino/main
Browse files Browse the repository at this point in the history
Fix encoding parameter not set to AF request
  • Loading branch information
Jesulonimi21 authored Jan 29, 2022
2 parents c0dbf36 + 59afce6 commit 12ad10e
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 9 deletions.
14 changes: 14 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/swift-algorand-sdk.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "swift-algorand-sdk_swift-algorand-sdkTests"
BuildableName = "swift-algorand-sdk_swift-algorand-sdkTests"
BlueprintName = "swift-algorand-sdk_swift-algorand-sdkTests"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
Expand Down
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ let package = Package(

.testTarget(
name: "swift-algorand-sdkTests",
dependencies: ["swift-algorand-sdk"]
dependencies: ["swift-algorand-sdk"],
resources: [.process("mocks")]
),

]
Expand Down
9 changes: 5 additions & 4 deletions Sources/swift-algorand-sdk/v2/client/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down
12 changes: 10 additions & 2 deletions Tests/swift-algorand-sdkTests/AlgodClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,17 @@ public class AlgodClientTests: XCTestCase {
}

func testGetVersion() {
let object = Version(build: .init(build_number: 1234))
let object = Version(build: .init(branch: "theBranch",
build_number: 123,
channel: "theChannel",
commit_hash: "theCommitHash",
major: 123,
minor: 123),
genesis_hash_string: "123",
genesis_id: "1234",
versions: ["1.0", "2.0"])
let request = GetVersion(client: client)
assertSuccessfulResponse(for: request, with: object)
assertSuccessfulResponse(for: request, json: "versions", with: object)
assertErrorResponse(for: request)
}

Expand Down
21 changes: 19 additions & 2 deletions Tests/swift-algorand-sdkTests/HTTPMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@ import XCTest

extension XCTestCase {
func assertSuccessfulResponse<CustomRequest>(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)
Expand All @@ -26,11 +33,13 @@ extension XCTestCase {
wait(for: [expectation], timeout: 1)
}
func assertSuccessfulResponse<CustomRequest>(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,
Expand Down Expand Up @@ -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<CustomRequest>(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)
}
}

Expand Down
16 changes: 16 additions & 0 deletions Tests/swift-algorand-sdkTests/mocks/algod/versions.json
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 12ad10e

Please sign in to comment.