diff --git a/beacon-chain/rpc/eth/beacon/handlers.go b/beacon-chain/rpc/eth/beacon/handlers.go index 063e7a65b11e..ed4bcec6ae55 100644 --- a/beacon-chain/rpc/eth/beacon/handlers.go +++ b/beacon-chain/rpc/eth/beacon/handlers.go @@ -62,7 +62,7 @@ func (s *Server) GetBlock(w http.ResponseWriter, r *http.Request) { return } - if httputil.SszRequested(r) { + if httputil.RespondWithSsz(r) { s.getBlockSSZ(ctx, w, blk) } else { s.getBlock(ctx, w, blk) @@ -105,7 +105,7 @@ func (s *Server) GetBlockV2(w http.ResponseWriter, r *http.Request) { return } - if httputil.SszRequested(r) { + if httputil.RespondWithSsz(r) { s.getBlockSSZV2(ctx, w, blk) } else { s.getBlockV2(ctx, w, blk) @@ -205,7 +205,7 @@ func (s *Server) GetBlindedBlock(w http.ResponseWriter, r *http.Request) { return } - if httputil.SszRequested(r) { + if httputil.RespondWithSsz(r) { s.getBlindedBlockSSZ(ctx, w, blk) } else { s.getBlindedBlock(ctx, w, blk) @@ -953,8 +953,7 @@ func (s *Server) PublishBlindedBlock(w http.ResponseWriter, r *http.Request) { if shared.IsSyncing(r.Context(), w, s.SyncChecker, s.HeadFetcher, s.TimeFetcher, s.OptimisticModeFetcher) { return } - isSSZ := httputil.SszRequested(r) - if isSSZ { + if httputil.IsRequestSsz(r) { s.publishBlindedBlockSSZ(ctx, w, r, false) } else { s.publishBlindedBlock(ctx, w, r, false) @@ -978,8 +977,7 @@ func (s *Server) PublishBlindedBlockV2(w http.ResponseWriter, r *http.Request) { if shared.IsSyncing(r.Context(), w, s.SyncChecker, s.HeadFetcher, s.TimeFetcher, s.OptimisticModeFetcher) { return } - isSSZ := httputil.SszRequested(r) - if isSSZ { + if httputil.IsRequestSsz(r) { s.publishBlindedBlockSSZ(ctx, w, r, true) } else { s.publishBlindedBlock(ctx, w, r, true) @@ -1250,8 +1248,7 @@ func (s *Server) PublishBlock(w http.ResponseWriter, r *http.Request) { if shared.IsSyncing(r.Context(), w, s.SyncChecker, s.HeadFetcher, s.TimeFetcher, s.OptimisticModeFetcher) { return } - isSSZ := httputil.SszRequested(r) - if isSSZ { + if httputil.IsRequestSsz(r) { s.publishBlockSSZ(ctx, w, r, false) } else { s.publishBlock(ctx, w, r, false) @@ -1273,8 +1270,7 @@ func (s *Server) PublishBlockV2(w http.ResponseWriter, r *http.Request) { if shared.IsSyncing(r.Context(), w, s.SyncChecker, s.HeadFetcher, s.TimeFetcher, s.OptimisticModeFetcher) { return } - isSSZ := httputil.SszRequested(r) - if isSSZ { + if httputil.IsRequestSsz(r) { s.publishBlockSSZ(ctx, w, r, true) } else { s.publishBlock(ctx, w, r, true) diff --git a/beacon-chain/rpc/eth/beacon/handlers_test.go b/beacon-chain/rpc/eth/beacon/handlers_test.go index c672a1a3470d..134b3c3c9c40 100644 --- a/beacon-chain/rpc/eth/beacon/handlers_test.go +++ b/beacon-chain/rpc/eth/beacon/handlers_test.go @@ -1169,7 +1169,7 @@ func TestPublishBlockSSZ(t *testing.T) { ssz, err := genericBlock.GetPhase0().MarshalSSZ() require.NoError(t, err) request := httptest.NewRequest(http.MethodPost, "http://foo.example", bytes.NewReader(ssz)) - request.Header.Set("Accept", api.OctetStreamMediaType) + request.Header.Set("Content-Type", api.OctetStreamMediaType) request.Header.Set(api.VersionHeader, version.String(version.Phase0)) writer := httptest.NewRecorder() writer.Body = &bytes.Buffer{} @@ -1199,7 +1199,7 @@ func TestPublishBlockSSZ(t *testing.T) { ssz, err := genericBlock.GetAltair().MarshalSSZ() require.NoError(t, err) request := httptest.NewRequest(http.MethodPost, "http://foo.example", bytes.NewReader(ssz)) - request.Header.Set("Accept", api.OctetStreamMediaType) + request.Header.Set("Content-Type", api.OctetStreamMediaType) request.Header.Set(api.VersionHeader, version.String(version.Altair)) writer := httptest.NewRecorder() writer.Body = &bytes.Buffer{} @@ -1224,7 +1224,7 @@ func TestPublishBlockSSZ(t *testing.T) { ssz, err := genericBlock.GetBellatrix().MarshalSSZ() require.NoError(t, err) request := httptest.NewRequest(http.MethodPost, "http://foo.example", bytes.NewReader(ssz)) - request.Header.Set("Accept", api.OctetStreamMediaType) + request.Header.Set("Content-Type", api.OctetStreamMediaType) request.Header.Set(api.VersionHeader, version.String(version.Bellatrix)) writer := httptest.NewRecorder() writer.Body = &bytes.Buffer{} @@ -1250,7 +1250,7 @@ func TestPublishBlockSSZ(t *testing.T) { ssz, err := genericBlock.GetCapella().MarshalSSZ() require.NoError(t, err) request := httptest.NewRequest(http.MethodPost, "http://foo.example", bytes.NewReader(ssz)) - request.Header.Set("Accept", api.OctetStreamMediaType) + request.Header.Set("Content-Type", api.OctetStreamMediaType) request.Header.Set(api.VersionHeader, version.String(version.Capella)) writer := httptest.NewRecorder() writer.Body = &bytes.Buffer{} @@ -1276,7 +1276,7 @@ func TestPublishBlockSSZ(t *testing.T) { ssz, err := genericBlock.GetDeneb().MarshalSSZ() require.NoError(t, err) request := httptest.NewRequest(http.MethodPost, "http://foo.example", bytes.NewReader(ssz)) - request.Header.Set("Accept", api.OctetStreamMediaType) + request.Header.Set("Content-Type", api.OctetStreamMediaType) request.Header.Set(api.VersionHeader, version.String(version.Deneb)) writer := httptest.NewRecorder() writer.Body = &bytes.Buffer{} @@ -1296,7 +1296,7 @@ func TestPublishBlockSSZ(t *testing.T) { ssz, err := genericBlock.GetBlindedBellatrix().MarshalSSZ() require.NoError(t, err) request := httptest.NewRequest(http.MethodPost, "http://foo.example", bytes.NewReader(ssz)) - request.Header.Set("Accept", api.OctetStreamMediaType) + request.Header.Set("Content-Type", api.OctetStreamMediaType) request.Header.Set(api.VersionHeader, version.String(version.Bellatrix)) writer := httptest.NewRecorder() writer.Body = &bytes.Buffer{} @@ -1317,7 +1317,7 @@ func TestPublishBlockSSZ(t *testing.T) { ssz, err := genericBlock.GetBellatrix().MarshalSSZ() require.NoError(t, err) request := httptest.NewRequest(http.MethodPost, "http://foo.example", bytes.NewReader(ssz)) - request.Header.Set("Accept", api.OctetStreamMediaType) + request.Header.Set("Content-Type", api.OctetStreamMediaType) request.Header.Set(api.VersionHeader, version.String(version.Capella)) writer := httptest.NewRecorder() writer.Body = &bytes.Buffer{} @@ -1335,7 +1335,7 @@ func TestPublishBlockSSZ(t *testing.T) { } request := httptest.NewRequest(http.MethodPost, "http://foo.example", bytes.NewReader([]byte("foo"))) - request.Header.Set("Accept", api.OctetStreamMediaType) + request.Header.Set("Content-Type", api.OctetStreamMediaType) writer := httptest.NewRecorder() writer.Body = &bytes.Buffer{} server.PublishBlock(writer, request) @@ -1531,7 +1531,7 @@ func TestPublishBlindedBlockSSZ(t *testing.T) { ssz, err := genericBlock.GetPhase0().MarshalSSZ() require.NoError(t, err) request := httptest.NewRequest(http.MethodPost, "http://foo.example", bytes.NewReader(ssz)) - request.Header.Set("Accept", api.OctetStreamMediaType) + request.Header.Set("Content-Type", api.OctetStreamMediaType) request.Header.Set(api.VersionHeader, version.String(version.Phase0)) writer := httptest.NewRecorder() writer.Body = &bytes.Buffer{} @@ -1561,7 +1561,7 @@ func TestPublishBlindedBlockSSZ(t *testing.T) { ssz, err := genericBlock.GetAltair().MarshalSSZ() require.NoError(t, err) request := httptest.NewRequest(http.MethodPost, "http://foo.example", bytes.NewReader(ssz)) - request.Header.Set("Accept", api.OctetStreamMediaType) + request.Header.Set("Content-Type", api.OctetStreamMediaType) request.Header.Set(api.VersionHeader, version.String(version.Altair)) writer := httptest.NewRecorder() writer.Body = &bytes.Buffer{} @@ -1587,7 +1587,7 @@ func TestPublishBlindedBlockSSZ(t *testing.T) { ssz, err := genericBlock.GetBlindedBellatrix().MarshalSSZ() require.NoError(t, err) request := httptest.NewRequest(http.MethodPost, "http://foo.example", bytes.NewReader(ssz)) - request.Header.Set("Accept", api.OctetStreamMediaType) + request.Header.Set("Content-Type", api.OctetStreamMediaType) request.Header.Set(api.VersionHeader, version.String(version.Bellatrix)) writer := httptest.NewRecorder() writer.Body = &bytes.Buffer{} @@ -1613,7 +1613,7 @@ func TestPublishBlindedBlockSSZ(t *testing.T) { ssz, err := genericBlock.GetBlindedCapella().MarshalSSZ() require.NoError(t, err) request := httptest.NewRequest(http.MethodPost, "http://foo.example", bytes.NewReader(ssz)) - request.Header.Set("Accept", api.OctetStreamMediaType) + request.Header.Set("Content-Type", api.OctetStreamMediaType) request.Header.Set(api.VersionHeader, version.String(version.Capella)) writer := httptest.NewRecorder() writer.Body = &bytes.Buffer{} @@ -1639,7 +1639,7 @@ func TestPublishBlindedBlockSSZ(t *testing.T) { ssz, err := genericBlock.GetBlindedDeneb().MarshalSSZ() require.NoError(t, err) request := httptest.NewRequest(http.MethodPost, "http://foo.example", bytes.NewReader(ssz)) - request.Header.Set("Accept", api.OctetStreamMediaType) + request.Header.Set("Content-Type", api.OctetStreamMediaType) request.Header.Set(api.VersionHeader, version.String(version.Deneb)) writer := httptest.NewRecorder() writer.Body = &bytes.Buffer{} @@ -1672,7 +1672,7 @@ func TestPublishBlindedBlockSSZ(t *testing.T) { ssz, err := genericBlock.GetBlindedBellatrix().MarshalSSZ() require.NoError(t, err) request := httptest.NewRequest(http.MethodPost, "http://foo.example", bytes.NewReader(ssz)) - request.Header.Set("Accept", api.OctetStreamMediaType) + request.Header.Set("Content-Type", api.OctetStreamMediaType) request.Header.Set(api.VersionHeader, version.String(version.Capella)) writer := httptest.NewRecorder() writer.Body = &bytes.Buffer{} @@ -1690,7 +1690,7 @@ func TestPublishBlindedBlockSSZ(t *testing.T) { } request := httptest.NewRequest(http.MethodPost, "http://foo.example", bytes.NewReader([]byte("foo"))) - request.Header.Set("Accept", api.OctetStreamMediaType) + request.Header.Set("Content-Type", api.OctetStreamMediaType) writer := httptest.NewRecorder() writer.Body = &bytes.Buffer{} server.PublishBlindedBlock(writer, request) @@ -1899,7 +1899,7 @@ func TestPublishBlockV2SSZ(t *testing.T) { ssz, err := genericBlock.GetPhase0().MarshalSSZ() require.NoError(t, err) request := httptest.NewRequest(http.MethodPost, "http://foo.example", bytes.NewReader(ssz)) - request.Header.Set("Accept", api.OctetStreamMediaType) + request.Header.Set("Content-Type", api.OctetStreamMediaType) request.Header.Set(api.VersionHeader, version.String(version.Phase0)) writer := httptest.NewRecorder() writer.Body = &bytes.Buffer{} @@ -1929,7 +1929,7 @@ func TestPublishBlockV2SSZ(t *testing.T) { ssz, err := genericBlock.GetAltair().MarshalSSZ() require.NoError(t, err) request := httptest.NewRequest(http.MethodPost, "http://foo.example", bytes.NewReader(ssz)) - request.Header.Set("Accept", api.OctetStreamMediaType) + request.Header.Set("Content-Type", api.OctetStreamMediaType) request.Header.Set(api.VersionHeader, version.String(version.Altair)) writer := httptest.NewRecorder() writer.Body = &bytes.Buffer{} @@ -1954,7 +1954,7 @@ func TestPublishBlockV2SSZ(t *testing.T) { ssz, err := genericBlock.GetBellatrix().MarshalSSZ() require.NoError(t, err) request := httptest.NewRequest(http.MethodPost, "http://foo.example", bytes.NewReader(ssz)) - request.Header.Set("Accept", api.OctetStreamMediaType) + request.Header.Set("Content-Type", api.OctetStreamMediaType) request.Header.Set(api.VersionHeader, version.String(version.Bellatrix)) writer := httptest.NewRecorder() writer.Body = &bytes.Buffer{} @@ -1980,7 +1980,7 @@ func TestPublishBlockV2SSZ(t *testing.T) { ssz, err := genericBlock.GetCapella().MarshalSSZ() require.NoError(t, err) request := httptest.NewRequest(http.MethodPost, "http://foo.example", bytes.NewReader(ssz)) - request.Header.Set("Accept", api.OctetStreamMediaType) + request.Header.Set("Content-Type", api.OctetStreamMediaType) request.Header.Set(api.VersionHeader, version.String(version.Capella)) writer := httptest.NewRecorder() writer.Body = &bytes.Buffer{} @@ -2006,7 +2006,7 @@ func TestPublishBlockV2SSZ(t *testing.T) { ssz, err := genericBlock.GetDeneb().MarshalSSZ() require.NoError(t, err) request := httptest.NewRequest(http.MethodPost, "http://foo.example", bytes.NewReader(ssz)) - request.Header.Set("Accept", api.OctetStreamMediaType) + request.Header.Set("Content-Type", api.OctetStreamMediaType) request.Header.Set(api.VersionHeader, version.String(version.Deneb)) writer := httptest.NewRecorder() writer.Body = &bytes.Buffer{} @@ -2026,7 +2026,7 @@ func TestPublishBlockV2SSZ(t *testing.T) { ssz, err := genericBlock.GetBlindedBellatrix().MarshalSSZ() require.NoError(t, err) request := httptest.NewRequest(http.MethodPost, "http://foo.example", bytes.NewReader(ssz)) - request.Header.Set("Accept", api.OctetStreamMediaType) + request.Header.Set("Content-Type", api.OctetStreamMediaType) request.Header.Set(api.VersionHeader, version.String(version.Bellatrix)) writer := httptest.NewRecorder() writer.Body = &bytes.Buffer{} @@ -2047,7 +2047,7 @@ func TestPublishBlockV2SSZ(t *testing.T) { ssz, err := genericBlock.GetBellatrix().MarshalSSZ() require.NoError(t, err) request := httptest.NewRequest(http.MethodPost, "http://foo.example", bytes.NewReader(ssz)) - request.Header.Set("Accept", api.OctetStreamMediaType) + request.Header.Set("Content-Type", api.OctetStreamMediaType) request.Header.Set(api.VersionHeader, version.String(version.Capella)) writer := httptest.NewRecorder() writer.Body = &bytes.Buffer{} @@ -2061,7 +2061,7 @@ func TestPublishBlockV2SSZ(t *testing.T) { } request := httptest.NewRequest(http.MethodPost, "http://foo.example", bytes.NewReader([]byte(rpctesting.CapellaBlock))) - request.Header.Set("Accept", api.OctetStreamMediaType) + request.Header.Set("Content-Type", api.OctetStreamMediaType) writer := httptest.NewRecorder() writer.Body = &bytes.Buffer{} server.PublishBlockV2(writer, request) @@ -2078,7 +2078,7 @@ func TestPublishBlockV2SSZ(t *testing.T) { } request := httptest.NewRequest(http.MethodPost, "http://foo.example", bytes.NewReader([]byte("foo"))) - request.Header.Set("Accept", api.OctetStreamMediaType) + request.Header.Set("Content-Type", api.OctetStreamMediaType) writer := httptest.NewRecorder() writer.Body = &bytes.Buffer{} server.PublishBlockV2(writer, request) @@ -2286,7 +2286,7 @@ func TestPublishBlindedBlockV2SSZ(t *testing.T) { ssz, err := genericBlock.GetPhase0().MarshalSSZ() require.NoError(t, err) request := httptest.NewRequest(http.MethodPost, "http://foo.example", bytes.NewReader(ssz)) - request.Header.Set("Accept", api.OctetStreamMediaType) + request.Header.Set("Content-Type", api.OctetStreamMediaType) request.Header.Set(api.VersionHeader, version.String(version.Phase0)) writer := httptest.NewRecorder() writer.Body = &bytes.Buffer{} @@ -2316,7 +2316,7 @@ func TestPublishBlindedBlockV2SSZ(t *testing.T) { ssz, err := genericBlock.GetAltair().MarshalSSZ() require.NoError(t, err) request := httptest.NewRequest(http.MethodPost, "http://foo.example", bytes.NewReader(ssz)) - request.Header.Set("Accept", api.OctetStreamMediaType) + request.Header.Set("Content-Type", api.OctetStreamMediaType) request.Header.Set(api.VersionHeader, version.String(version.Altair)) writer := httptest.NewRecorder() writer.Body = &bytes.Buffer{} @@ -2342,7 +2342,7 @@ func TestPublishBlindedBlockV2SSZ(t *testing.T) { ssz, err := genericBlock.GetBlindedBellatrix().MarshalSSZ() require.NoError(t, err) request := httptest.NewRequest(http.MethodPost, "http://foo.example", bytes.NewReader(ssz)) - request.Header.Set("Accept", api.OctetStreamMediaType) + request.Header.Set("Content-Type", api.OctetStreamMediaType) request.Header.Set(api.VersionHeader, version.String(version.Bellatrix)) writer := httptest.NewRecorder() writer.Body = &bytes.Buffer{} @@ -2368,7 +2368,7 @@ func TestPublishBlindedBlockV2SSZ(t *testing.T) { ssz, err := genericBlock.GetBlindedCapella().MarshalSSZ() require.NoError(t, err) request := httptest.NewRequest(http.MethodPost, "http://foo.example", bytes.NewReader(ssz)) - request.Header.Set("Accept", api.OctetStreamMediaType) + request.Header.Set("Content-Type", api.OctetStreamMediaType) request.Header.Set(api.VersionHeader, version.String(version.Capella)) writer := httptest.NewRecorder() writer.Body = &bytes.Buffer{} @@ -2394,7 +2394,7 @@ func TestPublishBlindedBlockV2SSZ(t *testing.T) { ssz, err := genericBlock.GetBlindedDeneb().MarshalSSZ() require.NoError(t, err) request := httptest.NewRequest(http.MethodPost, "http://foo.example", bytes.NewReader(ssz)) - request.Header.Set("Accept", api.OctetStreamMediaType) + request.Header.Set("Content-Type", api.OctetStreamMediaType) request.Header.Set(api.VersionHeader, version.String(version.Deneb)) writer := httptest.NewRecorder() writer.Body = &bytes.Buffer{} @@ -2427,7 +2427,7 @@ func TestPublishBlindedBlockV2SSZ(t *testing.T) { ssz, err := genericBlock.GetBlindedBellatrix().MarshalSSZ() require.NoError(t, err) request := httptest.NewRequest(http.MethodPost, "http://foo.example", bytes.NewReader(ssz)) - request.Header.Set("Accept", api.OctetStreamMediaType) + request.Header.Set("Content-Type", api.OctetStreamMediaType) request.Header.Set(api.VersionHeader, version.String(version.Capella)) writer := httptest.NewRecorder() writer.Body = &bytes.Buffer{} @@ -2441,7 +2441,7 @@ func TestPublishBlindedBlockV2SSZ(t *testing.T) { } request := httptest.NewRequest(http.MethodPost, "http://foo.example", bytes.NewReader([]byte(rpctesting.BlindedCapellaBlock))) - request.Header.Set("Accept", api.OctetStreamMediaType) + request.Header.Set("Content-Type", api.OctetStreamMediaType) writer := httptest.NewRecorder() writer.Body = &bytes.Buffer{} server.PublishBlockV2(writer, request) @@ -2458,7 +2458,7 @@ func TestPublishBlindedBlockV2SSZ(t *testing.T) { } request := httptest.NewRequest(http.MethodPost, "http://foo.example", bytes.NewReader([]byte("foo"))) - request.Header.Set("Accept", api.OctetStreamMediaType) + request.Header.Set("Content-Type", api.OctetStreamMediaType) writer := httptest.NewRecorder() writer.Body = &bytes.Buffer{} server.PublishBlindedBlockV2(writer, request) diff --git a/beacon-chain/rpc/eth/blob/handlers.go b/beacon-chain/rpc/eth/blob/handlers.go index af3fa3b63f1f..5e14e5efbed6 100644 --- a/beacon-chain/rpc/eth/blob/handlers.go +++ b/beacon-chain/rpc/eth/blob/handlers.go @@ -46,8 +46,7 @@ func (s *Server) Blobs(w http.ResponseWriter, r *http.Request) { for i := range verifiedBlobs { sidecars = append(sidecars, verifiedBlobs[i].BlobSidecar) } - ssz := httputil.SszRequested(r) - if ssz { + if httputil.RespondWithSsz(r) { sidecarResp := ð.BlobSidecars{ Sidecars: sidecars, } diff --git a/beacon-chain/rpc/eth/debug/handlers.go b/beacon-chain/rpc/eth/debug/handlers.go index aada8b690695..3a885d750ee5 100644 --- a/beacon-chain/rpc/eth/debug/handlers.go +++ b/beacon-chain/rpc/eth/debug/handlers.go @@ -54,7 +54,7 @@ func (s *Server) GetBeaconStateV2(w http.ResponseWriter, r *http.Request) { return } - if httputil.SszRequested(r) { + if httputil.RespondWithSsz(r) { s.getBeaconStateSSZV2(ctx, w, []byte(stateId)) } else { s.getBeaconStateV2(ctx, w, []byte(stateId)) diff --git a/beacon-chain/rpc/eth/validator/handlers_block.go b/beacon-chain/rpc/eth/validator/handlers_block.go index 805633a1c11b..0150836304cb 100644 --- a/beacon-chain/rpc/eth/validator/handlers_block.go +++ b/beacon-chain/rpc/eth/validator/handlers_block.go @@ -201,7 +201,7 @@ func (s *Server) ProduceBlockV3(w http.ResponseWriter, r *http.Request) { } func (s *Server) produceBlockV3(ctx context.Context, w http.ResponseWriter, r *http.Request, v1alpha1req *eth.BlockRequest, requiredType blockType) { - isSSZ := httputil.SszRequested(r) + isSSZ := httputil.RespondWithSsz(r) v1alpha1resp, err := s.V1Alpha1Server.GetBeaconBlock(ctx, v1alpha1req) if err != nil { httputil.HandleError(w, err.Error(), http.StatusInternalServerError) diff --git a/network/httputil/BUILD.bazel b/network/httputil/BUILD.bazel index a1fd6831eca8..bf966308263c 100644 --- a/network/httputil/BUILD.bazel +++ b/network/httputil/BUILD.bazel @@ -22,5 +22,6 @@ go_test( deps = [ "//api:go_default_library", "//testing/assert:go_default_library", + "//testing/require:go_default_library", ], ) diff --git a/network/httputil/reader.go b/network/httputil/reader.go index db1cbde4cb06..341a6a357a98 100644 --- a/network/httputil/reader.go +++ b/network/httputil/reader.go @@ -12,8 +12,8 @@ import ( // match a number with optional decimals var priorityRegex = regexp.MustCompile(`q=(\d+(?:\.\d+)?)`) -// SszRequested takes a http request and checks to see if it should be requesting a ssz response. -func SszRequested(req *http.Request) bool { +// RespondWithSsz takes a http request and checks to see if it should be requesting a ssz response. +func RespondWithSsz(req *http.Request) bool { accept := req.Header.Values("Accept") if len(accept) == 0 { return false @@ -51,3 +51,8 @@ func SszRequested(req *http.Request) bool { return currentType == api.OctetStreamMediaType } + +// IsRequestSsz checks if the request object should be interpreted as ssz +func IsRequestSsz(req *http.Request) bool { + return req.Header.Get("Content-Type") == api.OctetStreamMediaType +} diff --git a/network/httputil/reader_test.go b/network/httputil/reader_test.go index 7e85f65d1e93..584c5a8ba42f 100644 --- a/network/httputil/reader_test.go +++ b/network/httputil/reader_test.go @@ -1,88 +1,123 @@ package httputil import ( + "bytes" "fmt" + "net/http" "net/http/httptest" "testing" "github.com/prysmaticlabs/prysm/v4/api" "github.com/prysmaticlabs/prysm/v4/testing/assert" + "github.com/prysmaticlabs/prysm/v4/testing/require" ) -func TestSSZRequested(t *testing.T) { +func TestRespondWithSsz(t *testing.T) { t.Run("ssz_requested", func(t *testing.T) { - request := httptest.NewRequest("GET", "http://foo.example", nil) + request := httptest.NewRequest(http.MethodGet, "http://foo.example", nil) request.Header["Accept"] = []string{api.OctetStreamMediaType} - result := SszRequested(request) + result := RespondWithSsz(request) assert.Equal(t, true, result) }) t.Run("ssz_content_type_first", func(t *testing.T) { request := httptest.NewRequest("GET", "http://foo.example", nil) request.Header["Accept"] = []string{fmt.Sprintf("%s,%s", api.OctetStreamMediaType, api.JsonMediaType)} - result := SszRequested(request) + result := RespondWithSsz(request) assert.Equal(t, true, result) }) t.Run("ssz_content_type_preferred_1", func(t *testing.T) { - request := httptest.NewRequest("GET", "http://foo.example", nil) + request := httptest.NewRequest(http.MethodGet, "http://foo.example", nil) request.Header["Accept"] = []string{fmt.Sprintf("%s;q=0.9,%s", api.JsonMediaType, api.OctetStreamMediaType)} - result := SszRequested(request) + result := RespondWithSsz(request) assert.Equal(t, true, result) }) t.Run("ssz_content_type_preferred_2", func(t *testing.T) { - request := httptest.NewRequest("GET", "http://foo.example", nil) + request := httptest.NewRequest(http.MethodGet, "http://foo.example", nil) request.Header["Accept"] = []string{fmt.Sprintf("%s;q=0.95,%s;q=0.9", api.OctetStreamMediaType, api.JsonMediaType)} - result := SszRequested(request) + result := RespondWithSsz(request) assert.Equal(t, true, result) }) t.Run("other_content_type_preferred", func(t *testing.T) { request := httptest.NewRequest("GET", "http://foo.example", nil) request.Header["Accept"] = []string{fmt.Sprintf("%s,%s;q=0.9", api.JsonMediaType, api.OctetStreamMediaType)} - result := SszRequested(request) + result := RespondWithSsz(request) assert.Equal(t, false, result) }) t.Run("other_params", func(t *testing.T) { - request := httptest.NewRequest("GET", "http://foo.example", nil) + request := httptest.NewRequest(http.MethodGet, "http://foo.example", nil) request.Header["Accept"] = []string{fmt.Sprintf("%s,%s;q=0.9,otherparam=xyz", api.JsonMediaType, api.OctetStreamMediaType)} - result := SszRequested(request) + result := RespondWithSsz(request) assert.Equal(t, false, result) }) t.Run("no_header", func(t *testing.T) { - request := httptest.NewRequest("GET", "http://foo.example", nil) - result := SszRequested(request) + request := httptest.NewRequest(http.MethodGet, "http://foo.example", nil) + result := RespondWithSsz(request) assert.Equal(t, false, result) }) t.Run("empty_header", func(t *testing.T) { - request := httptest.NewRequest("GET", "http://foo.example", nil) + request := httptest.NewRequest(http.MethodGet, "http://foo.example", nil) request.Header["Accept"] = []string{} - result := SszRequested(request) + result := RespondWithSsz(request) assert.Equal(t, false, result) }) t.Run("empty_header_value", func(t *testing.T) { - request := httptest.NewRequest("GET", "http://foo.example", nil) + request := httptest.NewRequest(http.MethodGet, "http://foo.example", nil) request.Header["Accept"] = []string{""} - result := SszRequested(request) + result := RespondWithSsz(request) assert.Equal(t, false, result) }) t.Run("other_content_type", func(t *testing.T) { - request := httptest.NewRequest("GET", "http://foo.example", nil) + request := httptest.NewRequest(http.MethodGet, "http://foo.example", nil) request.Header["Accept"] = []string{"application/other"} - result := SszRequested(request) + result := RespondWithSsz(request) assert.Equal(t, false, result) }) t.Run("garbage", func(t *testing.T) { - request := httptest.NewRequest("GET", "http://foo.example", nil) + request := httptest.NewRequest(http.MethodGet, "http://foo.example", nil) request.Header["Accept"] = []string{"This is Sparta!!!"} - result := SszRequested(request) + result := RespondWithSsz(request) + assert.Equal(t, false, result) + }) +} + +func TestIsRequestSsz(t *testing.T) { + t.Run("ssz Post happy path", func(t *testing.T) { + var body bytes.Buffer + _, err := body.WriteString("something") + require.NoError(t, err) + request := httptest.NewRequest(http.MethodPost, "http://foo.example", &body) + request.Header["Content-Type"] = []string{api.OctetStreamMediaType} + result := IsRequestSsz(request) + assert.Equal(t, true, result) + }) + + t.Run("ssz Post missing header", func(t *testing.T) { + request := httptest.NewRequest(http.MethodPost, "http://foo.example", nil) + result := IsRequestSsz(request) + assert.Equal(t, false, result) + }) + + t.Run("ssz Post wrong content type", func(t *testing.T) { + request := httptest.NewRequest(http.MethodPost, "http://foo.example", nil) + request.Header["Content-Type"] = []string{"application/other"} + result := IsRequestSsz(request) + assert.Equal(t, false, result) + }) + + t.Run("ssz Post json content type", func(t *testing.T) { + request := httptest.NewRequest(http.MethodPost, "http://foo.example", nil) + request.Header["Content-Type"] = []string{api.JsonMediaType} + result := IsRequestSsz(request) assert.Equal(t, false, result) }) }