From 01156c1b1401934f890c611205e75df7c19a94c4 Mon Sep 17 00:00:00 2001 From: Aditya Vyas Date: Wed, 20 Nov 2024 00:49:08 +0530 Subject: [PATCH] Add test for GetHealth call --- internal/services/rpc_service_test.go | 52 +++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/internal/services/rpc_service_test.go b/internal/services/rpc_service_test.go index a9e27fc..fc4456c 100644 --- a/internal/services/rpc_service_test.go +++ b/internal/services/rpc_service_test.go @@ -10,11 +10,12 @@ import ( "strings" "testing" - "github.com/stellar/wallet-backend/internal/entities" - "github.com/stellar/wallet-backend/internal/utils" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" + + "github.com/stellar/wallet-backend/internal/entities" + "github.com/stellar/wallet-backend/internal/utils" ) type errorReader struct{} @@ -321,3 +322,50 @@ func TestGetTransactions(t *testing.T) { require.NoError(t, err) }) } + +func TestSendGetHealth(t *testing.T) { + mockHTTPClient := utils.MockHTTPClient{} + rpcURL := "http://api.vibrantapp.com/soroban/rpc" + rpcService, _ := NewRPCService(rpcURL, &mockHTTPClient) + + t.Run("successful", func(t *testing.T) { + payload := map[string]interface{}{ + "jsonrpc": "2.0", + "id": 1, + "method": "getHealth", + } + jsonData, _ := json.Marshal(payload) + + httpResponse := http.Response{ + StatusCode: http.StatusOK, + Body: io.NopCloser(strings.NewReader(`{ + "jsonrpc": "2.0", + "id": 8675309, + "result": { + "status": "healthy" + } + }`)), + } + + mockHTTPClient. + On("Post", rpcURL, "application/json", bytes.NewBuffer(jsonData)). + Return(&httpResponse, nil). + Once() + + result, err := rpcService.GetHealth() + require.NoError(t, err) + assert.Equal(t, entities.RPCGetHealthResult{Status: "healthy"}, result) + }) + + t.Run("rpc_request_fails", func(t *testing.T) { + mockHTTPClient. + On("Post", rpcURL, "application/json", mock.Anything). + Return(&http.Response{}, errors.New("connection failed")). + Once() + + result, err := rpcService.GetHealth() + require.Error(t, err) + assert.Equal(t, entities.RPCGetHealthResult{}, result) + assert.Equal(t, "sending getHealth request: sending POST request to RPC: connection failed", err.Error()) + }) +}