solution: test ProxyServer
This commit is contained in:
@@ -41,12 +41,12 @@ import kotlin.concurrent.write
|
||||
*/
|
||||
class ProxyServer(
|
||||
private var config: ProxyConfig,
|
||||
private val readRpcJson: ReadRpcJson,
|
||||
private val writeRpcJson: WriteRpcJson,
|
||||
private val nativeCall: NativeCall,
|
||||
private val nativeSubscribe: NativeSubscribe,
|
||||
readRpcJson: ReadRpcJson,
|
||||
writeRpcJson: WriteRpcJson,
|
||||
nativeCall: NativeCall,
|
||||
nativeSubscribe: NativeSubscribe,
|
||||
private val tlsSetup: TlsSetup,
|
||||
private val accessHandler: AccessHandlerHttp.HandlerFactory
|
||||
accessHandler: AccessHandlerHttp.HandlerFactory
|
||||
) {
|
||||
|
||||
companion object {
|
||||
@@ -90,7 +90,10 @@ class ProxyServer(
|
||||
log.debug("Proxy server is not enabled")
|
||||
return
|
||||
}
|
||||
log.info("Listening Proxy on ${config.host}:${config.port}")
|
||||
log.info("Start HTTP JSON RPC Proxy on ${connectAddress("http")}")
|
||||
if (config.websocketEnabled) {
|
||||
log.info("Start Websocket JSON RPC Proxy on ${connectAddress("ws")}")
|
||||
}
|
||||
var serverBuilder = HttpServer.create()
|
||||
.doOnChannelInit { _, channel, _ ->
|
||||
channel.pipeline().addFirst(errorHandler)
|
||||
@@ -116,6 +119,11 @@ class ProxyServer(
|
||||
}
|
||||
}
|
||||
|
||||
fun connectAddress(baseSchema: String): String {
|
||||
val schema = if (config.tls != null) baseSchema + "s" else baseSchema
|
||||
return "$schema://${config.host}:${config.port}"
|
||||
}
|
||||
|
||||
interface RequestMetricsFactory {
|
||||
fun get(chain: Chain, method: String): RequestMetrics
|
||||
}
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
package io.emeraldpay.dshackle.proxy
|
||||
|
||||
import io.emeraldpay.dshackle.TlsSetup
|
||||
import io.emeraldpay.dshackle.config.AuthConfig
|
||||
import io.emeraldpay.dshackle.config.ProxyConfig
|
||||
import io.emeraldpay.dshackle.monitoring.accesslog.AccessHandlerHttp
|
||||
import io.emeraldpay.dshackle.rpc.NativeCall
|
||||
import io.emeraldpay.dshackle.rpc.NativeSubscribe
|
||||
import io.emeraldpay.grpc.Chain
|
||||
import reactor.netty.http.server.HttpServerRoutes
|
||||
import spock.lang.Specification
|
||||
|
||||
class ProxyServerSpec extends Specification {
|
||||
|
||||
def "Setup routes"() {
|
||||
setup:
|
||||
def config1 = new ProxyConfig()
|
||||
config1.routes = [
|
||||
new ProxyConfig.Route("test", Chain.ETHEREUM)
|
||||
]
|
||||
def proxyServer = new ProxyServer(
|
||||
config1,
|
||||
new ReadRpcJson(), new WriteRpcJson(),
|
||||
Stub(NativeCall), Stub(NativeSubscribe),
|
||||
Stub(TlsSetup), new AccessHandlerHttp.NoOpFactory()
|
||||
)
|
||||
|
||||
def routes = Mock(HttpServerRoutes)
|
||||
when:
|
||||
proxyServer.setupRoutes(routes)
|
||||
|
||||
then:
|
||||
1 * routes.post("/test", _)
|
||||
1 * routes.ws("/test", _)
|
||||
}
|
||||
|
||||
def "Setup routes when WS is disabled"() {
|
||||
setup:
|
||||
def config1 = new ProxyConfig()
|
||||
config1.websocketEnabled = false
|
||||
config1.routes = [
|
||||
new ProxyConfig.Route("test", Chain.ETHEREUM)
|
||||
]
|
||||
def proxyServer = new ProxyServer(
|
||||
config1,
|
||||
new ReadRpcJson(), new WriteRpcJson(),
|
||||
Stub(NativeCall), Stub(NativeSubscribe),
|
||||
Stub(TlsSetup), new AccessHandlerHttp.NoOpFactory()
|
||||
)
|
||||
|
||||
def routes = Mock(HttpServerRoutes)
|
||||
when:
|
||||
proxyServer.setupRoutes(routes)
|
||||
|
||||
then:
|
||||
1 * routes.post("/test", _)
|
||||
0 * routes.ws(_, _)
|
||||
}
|
||||
|
||||
def "Generate Connect Address"() {
|
||||
def config1 = new ProxyConfig()
|
||||
config1.host = "192.168.0.1"
|
||||
config1.port = 1000
|
||||
def proxyServer = new ProxyServer(
|
||||
config1,
|
||||
new ReadRpcJson(), new WriteRpcJson(),
|
||||
Stub(NativeCall), Stub(NativeSubscribe),
|
||||
Stub(TlsSetup), new AccessHandlerHttp.NoOpFactory()
|
||||
)
|
||||
when:
|
||||
def act = proxyServer.connectAddress("http")
|
||||
then:
|
||||
act == "http://192.168.0.1:1000"
|
||||
}
|
||||
|
||||
def "Generate Connect Address with TLS"() {
|
||||
def config1 = new ProxyConfig()
|
||||
config1.host = "192.168.0.1"
|
||||
config1.port = 1000
|
||||
config1.tls = new AuthConfig.ServerTlsAuth()
|
||||
def proxyServer = new ProxyServer(
|
||||
config1,
|
||||
new ReadRpcJson(), new WriteRpcJson(),
|
||||
Stub(NativeCall), Stub(NativeSubscribe),
|
||||
Stub(TlsSetup), new AccessHandlerHttp.NoOpFactory()
|
||||
)
|
||||
when:
|
||||
def act = proxyServer.connectAddress("ws")
|
||||
then:
|
||||
act == "wss://192.168.0.1:1000"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user