@@ -4,12 +4,11 @@ import (
4
4
"context"
5
5
"encoding/json"
6
6
"fmt"
7
- "io/ioutil"
8
7
"net/http"
9
8
"strconv"
10
- "strings"
11
9
"testing"
12
10
11
+ "github.com/stretchr/testify/assert"
13
12
"github.com/stretchr/testify/require"
14
13
"go.uber.org/fx"
15
14
@@ -25,11 +24,34 @@ import (
25
24
// celestia-node. They will be removed upon refactoring of the RPC
26
25
// architecture and Public API. @renaynay @Wondertan.
27
26
27
+ const testHeight = uint64 (2 )
28
+
28
29
// TestNamespacedSharesRequest tests the `/namespaced_shares` endpoint.
29
30
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 ()
30
53
nd := setupNodeWithModifiedRPC (t )
31
54
// create several requests for header at height 2
32
- height := uint64 (2 )
33
55
var tests = []struct {
34
56
nID string
35
57
expectedErr bool
@@ -53,8 +75,8 @@ func TestNamespacedSharesRequest(t *testing.T) {
53
75
54
76
for i , tt := range tests {
55
77
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 )
58
80
resp , err := http .Get (endpoint )
59
81
defer func () {
60
82
err = resp .Body .Close ()
@@ -63,20 +85,20 @@ func TestNamespacedSharesRequest(t *testing.T) {
63
85
// check resp
64
86
if tt .expectedErr {
65
87
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 )
66
92
67
- buf , err := ioutil .ReadAll (resp .Body )
68
93
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 )
71
95
72
96
return
73
97
}
74
98
require .NoError (t , err )
75
99
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 )
80
102
})
81
103
}
82
104
}
0 commit comments