mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-09 11:11:20 +00:00
4c47756aed
* remove validation package * structs cleanup * merge with apimiddleware removal * more validation and Bls capitalization * builder test fix * use strconv for uint->str conversions * use DecodeHexWithLength * use exact param names * rename http package to httputil * change conversions to fmt.Sprintf * handle query paramsd and route variables * spans and receiver name * split structs, move bytes helper * missing ok check * fix reference to indexed failure * errors fixup * add godoc to helper * fix BLS casing and chainhead ref * review * fix import in tests * gzl
52 lines
1.4 KiB
Go
52 lines
1.4 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"
|
|
"github.com/prysmaticlabs/prysm/v4/network/httputil"
|
|
"github.com/prysmaticlabs/prysm/v4/testing/assert"
|
|
)
|
|
|
|
// 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 := &httputil.DefaultErrorJson{}
|
|
assert.NoError(t, json.Unmarshal(writer.Body.Bytes(), e), "failed to unmarshal response")
|
|
}
|
|
}
|