2023-08-03 22:24:23 +00:00
|
|
|
package shared
|
|
|
|
|
|
|
|
import (
|
2023-10-13 17:21:38 +00:00
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2023-08-03 22:24:23 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2023-10-13 17:21:38 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/lookup"
|
2023-12-08 20:37:20 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/network/httputil"
|
2023-08-03 22:24:23 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/testing/assert"
|
|
|
|
)
|
|
|
|
|
2023-10-13 17:21:38 +00:00
|
|
|
// 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")
|
|
|
|
|
2023-12-15 03:26:48 +00:00
|
|
|
e := &httputil.DefaultJsonError{}
|
2023-10-13 17:21:38 +00:00
|
|
|
assert.NoError(t, json.Unmarshal(writer.Body.Bytes(), e), "failed to unmarshal response")
|
|
|
|
}
|
|
|
|
}
|