From 405ab131b0ef45701ea8b5a98b594c63a7e59d3a Mon Sep 17 00:00:00 2001 From: Leonid Bugaev Date: Tue, 14 Feb 2017 19:04:44 +0300 Subject: [PATCH] getAPISpec should handle nil APISpec response getAPISpec calls handle `nil` situation, but not `getAPISpec` itself. --- api_definition_test.go | 76 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/api_definition_test.go b/api_definition_test.go index 1b1bbb83a482..17c8d6644df0 100644 --- a/api_definition_test.go +++ b/api_definition_test.go @@ -4,6 +4,8 @@ import ( "net/http" "net/url" "testing" + + "github.com/lonelycode/gorpc" ) const sampleDefiniton = `{ @@ -391,3 +393,77 @@ func TestBlacklistLinksMulti(t *testing.T) { t.Error(status) } } + +func startRPCMock(dispatcher *gorpc.Dispatcher) *gorpc.Server { + config.SlaveOptions.UseRPC = true + config.SlaveOptions.RPCKey = "test_org" + config.SlaveOptions.APIKey = "test" + + server := gorpc.NewTCPServer(":9090", dispatcher.NewHandlerFunc()) + go server.Serve() + config.SlaveOptions.ConnectionString = server.Addr + + RPCCLientSingleton = gorpc.NewTCPClient(server.Addr) + RPCCLientSingleton.Conns = 1 + RPCCLientSingleton.Start() + RPCClientIsConnected = true + RPCFuncClientSingleton = GetDispatcher().NewFuncClient(RPCCLientSingleton) + + return server +} + +func stopRPCMock(server *gorpc.Server) { + config.SlaveOptions.ConnectionString = "" + config.SlaveOptions.RPCKey = "" + config.SlaveOptions.APIKey = "" + config.SlaveOptions.UseRPC = false + + server.Listener.Close() + server.Stop() + + RPCCLientSingleton.Stop() + RPCClientIsConnected = false + RPCClients = map[string]chan int{} + RPCCLientSingleton = nil + RPCFuncClientSingleton = nil +} + +func TestGetAPISpecsRPCFailure(t *testing.T) { + // Mock RPC + dispatcher := gorpc.NewDispatcher() + dispatcher.AddFunc("GetApiDefinitions", func(clientAddr string, dr *DefRequest) (string, error) { + return "malformed json", nil + }) + dispatcher.AddFunc("Login", func(clientAddr string, userKey string) bool { + return true + }) + + rpc := startRPCMock(dispatcher) + + specs := getAPISpecs() + if len(specs) != 0 { + t.Error("Should return empty value for malformed rpc response", specs) + } + + stopRPCMock(rpc) +} + +func TestGetAPISpecsRPCSuccess(t *testing.T) { + // Mock RPC + dispatcher := gorpc.NewDispatcher() + dispatcher.AddFunc("GetApiDefinitions", func(clientAddr string, dr *DefRequest) (string, error) { + return "[{}]", nil + }) + dispatcher.AddFunc("Login", func(clientAddr string, userKey string) bool { + return true + }) + + rpc := startRPCMock(dispatcher) + + specs := getAPISpecs() + if len(specs) != 1 { + t.Error("Should return array with one spec", specs) + } + + stopRPCMock(rpc) +}