Skip to content

Commit 5843c5e

Browse files
tzdybalWondertan
authored andcommitted
tests(node): add TestNamespacedDataRequest, refactor tests
1 parent 967d2b6 commit 5843c5e

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

Diff for: node/rpc_test.go

+34-12
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7-
"io/ioutil"
87
"net/http"
98
"strconv"
10-
"strings"
119
"testing"
1210

11+
"github.com/stretchr/testify/assert"
1312
"github.com/stretchr/testify/require"
1413
"go.uber.org/fx"
1514

@@ -25,11 +24,34 @@ import (
2524
// celestia-node. They will be removed upon refactoring of the RPC
2625
// architecture and Public API. @renaynay @Wondertan.
2726

27+
const testHeight = uint64(2)
28+
2829
// TestNamespacedSharesRequest tests the `/namespaced_shares` endpoint.
2930
func TestNamespacedSharesRequest(t *testing.T) {
31+
testGetNamespacedRequest(t, "namespaced_shares", func(t *testing.T, resp *http.Response) {
32+
t.Helper()
33+
namespacedShares := new(rpc.NamespacedSharesResponse)
34+
err := json.NewDecoder(resp.Body).Decode(namespacedShares)
35+
assert.NoError(t, err)
36+
assert.Equal(t, testHeight, namespacedShares.Height)
37+
})
38+
}
39+
40+
// TestNamespacedDataRequest tests the `/namespaced_shares` endpoint.
41+
func TestNamespacedDataRequest(t *testing.T) {
42+
testGetNamespacedRequest(t, "namespaced_data", func(t *testing.T, resp *http.Response) {
43+
t.Helper()
44+
namespacedData := new(rpc.NamespacedDataResponse)
45+
err := json.NewDecoder(resp.Body).Decode(namespacedData)
46+
assert.NoError(t, err)
47+
assert.Equal(t, testHeight, namespacedData.Height)
48+
})
49+
}
50+
51+
func testGetNamespacedRequest(t *testing.T, endpointName string, assertResponseOK func(*testing.T, *http.Response)) {
52+
t.Helper()
3053
nd := setupNodeWithModifiedRPC(t)
3154
// create several requests for header at height 2
32-
height := uint64(2)
3355
var tests = []struct {
3456
nID string
3557
expectedErr bool
@@ -53,8 +75,8 @@ func TestNamespacedSharesRequest(t *testing.T) {
5375

5476
for i, tt := range tests {
5577
t.Run(strconv.Itoa(i), func(t *testing.T) {
56-
endpoint := fmt.Sprintf("http://127.0.0.1:%s/namespaced_shares/%s/height/%d",
57-
nd.RPCServer.ListenAddr()[5:], tt.nID, height)
78+
endpoint := fmt.Sprintf("http://127.0.0.1:%s/%s/%s/height/%d",
79+
nd.RPCServer.ListenAddr()[5:], endpointName, tt.nID, testHeight)
5880
resp, err := http.Get(endpoint)
5981
defer func() {
6082
err = resp.Body.Close()
@@ -63,20 +85,20 @@ func TestNamespacedSharesRequest(t *testing.T) {
6385
// check resp
6486
if tt.expectedErr {
6587
require.False(t, resp.StatusCode == http.StatusOK)
88+
require.Equal(t, "application/json", resp.Header.Get("Content-Type"))
89+
90+
var errorMessage string
91+
err := json.NewDecoder(resp.Body).Decode(&errorMessage)
6692

67-
buf, err := ioutil.ReadAll(resp.Body)
6893
require.NoError(t, err)
69-
expectedErr := strings.Trim(string(buf), "\"\"") //nolint:staticcheck
70-
require.Equal(t, tt.errMsg, expectedErr)
94+
require.Equal(t, tt.errMsg, errorMessage)
7195

7296
return
7397
}
7498
require.NoError(t, err)
7599
require.True(t, resp.StatusCode == http.StatusOK)
76-
// decode resp
77-
namespacedShares := new(rpc.NamespacedSharesResponse)
78-
err = json.NewDecoder(resp.Body).Decode(namespacedShares)
79-
require.Equal(t, height, namespacedShares.Height)
100+
101+
assertResponseOK(t, resp)
80102
})
81103
}
82104
}

0 commit comments

Comments
 (0)