-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f782757
commit c4593d3
Showing
2 changed files
with
9 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,3 +80,5 @@ final class RPCController: RouteCollection { | |
} | ||
} | ||
} | ||
|
||
extension RPCController: @unchecked Sendable {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,19 @@ | ||
@testable import RPC | ||
import Testing | ||
import Vapor | ||
import XCTest | ||
|
||
final class RPCControllerTests: XCTestCase { | ||
final class RPCControllerTests: @unchecked Sendable { | ||
var app: Application! | ||
|
||
override func setUp() { | ||
super.setUp() | ||
app = Application(.testing) | ||
init() async throws { | ||
app = try await Application.make() | ||
|
||
let rpcController = RPCController() | ||
try app.register(collection: rpcController) | ||
try await app.execute() | ||
} | ||
|
||
override func tearDown() { | ||
app.shutdown() | ||
super.tearDown() | ||
} | ||
|
||
func testRPCRequest() throws { | ||
let request = RPCRequest<AnyContent>(jsonrpc: "2.0", method: "chain_getBlock", params: BlockParams(blockHash: "dummyHash"), id: 1) | ||
let requestData = try JSONEncoder().encode(request) | ||
|
||
try app.test(.POST, "rpc", headers: ["Content-Type": "application/json"], body: ByteBuffer(data: requestData)) { res in | ||
XCTAssertEqual(res.status, .ok) | ||
let rpcResponse = try res.content.decode(RPCResponse<AnyContent>.self) | ||
XCTAssertEqual(rpcResponse.jsonrpc, "2.0") | ||
XCTAssertEqual(rpcResponse.id, 1) | ||
} | ||
} | ||
|
||
func testInvalidRPCRequest() throws { | ||
let invalidJSON = """ | ||
{ | ||
"jsonrpc": "2.0", | ||
"method": "unknown_method", | ||
"id": 1 | ||
} | ||
""" | ||
let requestData = invalidJSON.data(using: .utf8)! | ||
|
||
try app.test(.POST, "rpc", headers: ["Content-Type": "application/json"], body: ByteBuffer(data: requestData)) { res in | ||
XCTAssertEqual(res.status, .badRequest) | ||
let rpcResponse = try res.content.decode(RPCResponse<RPCError>.self) | ||
XCTAssertEqual(rpcResponse.jsonrpc, "2.0") | ||
XCTAssertEqual(rpcResponse.error?.code, -32601) // Method not found | ||
} | ||
} | ||
|
||
func testWebSocketRPCRequest() throws { | ||
let request = RPCRequest<AnyContent>(jsonrpc: "2.0", method: "chain_getHeader", params: HeaderParams(blockHash: "dummyHash"), id: 1) | ||
let requestData = try JSONEncoder().encode(request) | ||
let requestString = String(decoding: responseData, as: UTF8.self) | ||
|
||
try app.testable().ws("ws") { ws in | ||
ws.send(requestString) | ||
ws.onText { _, text in | ||
let rpcResponse = try JSONDecoder().decode(RPCResponse<AnyContent>.self, from: Data(text.utf8)) | ||
XCTAssertEqual(rpcResponse.jsonrpc, "2.0") | ||
XCTAssertEqual(rpcResponse.id, 1) | ||
} | ||
} | ||
@Test func serviceInited() { | ||
#expect(app != nil) | ||
} | ||
} |