mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-08 02:31:19 +00:00
cc2b4db582
* beacon-chain:rpc/eth/shared: prevent mutiple error messages This commit prevents the error writing function from writing multiple JSON objects. An error message with more than one JSON object will not unmarshal into the default error response. * beacon-chain/rpc/eth/beacon: add a test case for missing state This commit adds a test on beacon states finality checkpoints endpoint to cover a case when state is not found. * beacon-chain/rpc/eth: update error response to meet the spec This commit updates error message on beacon states finality checkpoints endpoint to ensure that the response complies to Ethereum Beacon-API specification. * beacon-chain/rpc/eth/shared: add build dependency * beacon-chain/rpc/eth/shared: update test on state not found --------- Co-authored-by: Radosław Kapka <rkapka@wp.pl>
60 lines
1.6 KiB
Go
60 lines
1.6 KiB
Go
package shared
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/pkg/errors"
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/lookup"
|
|
http2 "github.com/prysmaticlabs/prysm/v4/network/http"
|
|
"github.com/prysmaticlabs/prysm/v4/testing/assert"
|
|
)
|
|
|
|
func TestDecodeError(t *testing.T) {
|
|
e := errors.New("not a number")
|
|
de := NewDecodeError(e, "Z")
|
|
de = NewDecodeError(de, "Y")
|
|
de = NewDecodeError(de, "X")
|
|
assert.Equal(t, "could not decode X.Y.Z: not a number", de.Error())
|
|
}
|
|
|
|
// TestWriteStateFetchError tests the WriteStateFetchError function
|
|
// to ensure that the correct error message and code are written to the response
|
|
// as an expected JSON format.
|
|
func TestWriteStateFetchError(t *testing.T) {
|
|
cases := []struct {
|
|
err error
|
|
expectedMessage string
|
|
expectedCode int
|
|
}{
|
|
{
|
|
err: &lookup.StateNotFoundError{},
|
|
expectedMessage: "State not found",
|
|
expectedCode: http.StatusNotFound,
|
|
},
|
|
{
|
|
err: &lookup.StateIdParseError{},
|
|
expectedMessage: "Invalid state ID",
|
|
expectedCode: http.StatusBadRequest,
|
|
},
|
|
{
|
|
err: errors.New("state not found"),
|
|
expectedMessage: "Could not get state",
|
|
expectedCode: http.StatusInternalServerError,
|
|
},
|
|
}
|
|
|
|
for _, c := range cases {
|
|
writer := httptest.NewRecorder()
|
|
WriteStateFetchError(writer, c.err)
|
|
|
|
assert.Equal(t, c.expectedCode, writer.Code, "incorrect status code")
|
|
assert.StringContains(t, c.expectedMessage, writer.Body.String(), "incorrect error message")
|
|
|
|
e := &http2.DefaultErrorJson{}
|
|
assert.NoError(t, json.Unmarshal(writer.Body.Bytes(), e), "failed to unmarshal response")
|
|
}
|
|
}
|