Skip to content

Commit

Permalink
Fix: parsing node error
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Oct 1, 2023
1 parent 11d1dc6 commit 56cf229
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
4 changes: 1 addition & 3 deletions internal/noderpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ func (rpc *NodeRPC) checkStatusCode(r io.Reader, statusCode int, checkStatusCode
return err
}
invalidResponseErr.Raw = data
if err := json.Unmarshal(data, &invalidResponseErr.Errors); err != nil {
return errors.Wrap(invalidResponseErr, err.Error())
}
_ = json.Unmarshal(data, &invalidResponseErr.Errors)
return invalidResponseErr
default:
return nil
Expand Down
66 changes: 66 additions & 0 deletions internal/noderpc/rpc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package noderpc

import (
"io"
"strings"
"testing"

"github.com/stretchr/testify/require"
)

func TestNodeRPC_checkStatusCode(t *testing.T) {
tests := []struct {
name string
r io.Reader
statusCode int
checkStatusCode bool
wantErr bool
errString string
}{
{
name: "404 error",
r: strings.NewReader("404 page not found"),
statusCode: 400,
checkStatusCode: true,
wantErr: true,
errString: "404 page not found",
}, {
name: "200",
r: strings.NewReader(""),
statusCode: 200,
checkStatusCode: true,
wantErr: false,
}, {
name: "501",
r: strings.NewReader("node unavailiable"),
statusCode: 501,
checkStatusCode: true,
wantErr: true,
errString: "is unavailiable: 501",
}, {
name: "no checking",
r: strings.NewReader("node unavailiable"),
statusCode: 400,
checkStatusCode: false,
wantErr: false,
errString: "",
}, {
name: "501 with no checking",
r: strings.NewReader("node unavailiable"),
statusCode: 501,
checkStatusCode: false,
wantErr: true,
errString: "is unavailiable: 501",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rpc := new(NodeRPC)
err := rpc.checkStatusCode(tt.r, tt.statusCode, tt.checkStatusCode)
require.Equal(t, tt.wantErr, err != nil)
if err != nil {
require.ErrorContains(t, err, tt.errString)
}
})
}
}

0 comments on commit 56cf229

Please sign in to comment.