@@ -22,7 +22,10 @@ import io.emeraldpay.dshackle.TlsSetup
|
||||
import io.emeraldpay.dshackle.config.ProxyConfig
|
||||
import io.emeraldpay.dshackle.rpc.NativeCall
|
||||
import io.emeraldpay.dshackle.test.TestingCommons
|
||||
import io.emeraldpay.dshackle.upstream.rpcclient.JsonRpcResponse
|
||||
import io.infinitape.etherjar.rpc.RpcException
|
||||
import reactor.core.publisher.Flux
|
||||
import reactor.core.publisher.Mono
|
||||
import reactor.test.StepVerifier
|
||||
import spock.lang.Specification
|
||||
|
||||
@@ -65,4 +68,24 @@ class ProxyServerSpec extends Specification {
|
||||
.expectComplete()
|
||||
.verify(Duration.ofSeconds(1))
|
||||
}
|
||||
|
||||
def "Return error on invalid request"() {
|
||||
setup:
|
||||
ReadRpcJson read = Mock(ReadRpcJson) {
|
||||
1 * apply(_) >> { throw new RpcException(-32123, "test", new JsonRpcResponse.IntId(4)) }
|
||||
}
|
||||
def server = new ProxyServer(
|
||||
Stub(ProxyConfig),
|
||||
read,
|
||||
Stub(WriteRpcJson), Stub(NativeCall), Stub(TlsSetup)
|
||||
)
|
||||
when:
|
||||
def act = server.processRequest(Common.ChainRef.CHAIN_ETHEREUM, Mono.just("".bytes))
|
||||
.map { new String(it.array()) }
|
||||
then:
|
||||
StepVerifier.create(act)
|
||||
.expectNext('{"jsonrpc":"2.0","id":4,"error":{"code":-32123,"message":"test"}}')
|
||||
.expectComplete()
|
||||
.verify(Duration.ofSeconds(1))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package io.emeraldpay.dshackle.proxy
|
||||
|
||||
import io.emeraldpay.dshackle.test.TestingCommons
|
||||
import io.emeraldpay.dshackle.upstream.rpcclient.JsonRpcResponse
|
||||
import io.infinitape.etherjar.rpc.RpcException
|
||||
import spock.lang.Specification
|
||||
|
||||
@@ -174,4 +175,53 @@ class ReadRpcJsonSpec extends Specification {
|
||||
payload.toStringUtf8() == '[143,false]'
|
||||
}
|
||||
}
|
||||
|
||||
def "Error if id is not set"() {
|
||||
when:
|
||||
reader.apply('{"jsonrpc":"2.0", "method":"net_peerCount", "params":[]}'.bytes)
|
||||
then:
|
||||
def t = thrown(RpcException)
|
||||
t.code == -32600
|
||||
t.rpcMessage.toLowerCase() == "id is not set"
|
||||
}
|
||||
|
||||
def "Error if jsonrpc is not set"() {
|
||||
when:
|
||||
reader.apply('{"id":2, "method":"net_peerCount", "params":[]}'.bytes)
|
||||
then:
|
||||
def t = thrown(RpcException)
|
||||
t.code == -32600
|
||||
t.rpcMessage.toLowerCase() == "jsonrpc version is not set"
|
||||
t.details == new JsonRpcResponse.IntId(2)
|
||||
}
|
||||
|
||||
def "Error if jsonrpc version is invalid"() {
|
||||
when:
|
||||
reader.apply('{"id":2, "jsonrpc":"3.0", "method":"net_peerCount", "params":[]}'.bytes)
|
||||
then:
|
||||
def t = thrown(RpcException)
|
||||
t.code == -32600
|
||||
t.rpcMessage.toLowerCase() == "unsupported json rpc version: 3.0"
|
||||
t.details == new JsonRpcResponse.IntId(2)
|
||||
}
|
||||
|
||||
def "Error if method is not set"() {
|
||||
when:
|
||||
reader.apply('{"id":2, "jsonrpc":"2.0", "params":[]}'.bytes)
|
||||
then:
|
||||
def t = thrown(RpcException)
|
||||
t.code == -32600
|
||||
t.rpcMessage.toLowerCase() == "method is not set"
|
||||
t.details == new JsonRpcResponse.IntId(2)
|
||||
}
|
||||
|
||||
def "Error if params is not array"() {
|
||||
when:
|
||||
reader.apply('{"id":2, "jsonrpc":"2.0", "method":"test", "params":123}'.bytes)
|
||||
then:
|
||||
def t = thrown(RpcException)
|
||||
t.code == -32600
|
||||
t.rpcMessage.toLowerCase() == "params must be an array"
|
||||
t.details == new JsonRpcResponse.IntId(2)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user