diff --git a/beacon-chain/rpc/apimiddleware/custom_handlers.go b/beacon-chain/rpc/apimiddleware/custom_handlers.go index 3439c387be..7baa7bcf1c 100644 --- a/beacon-chain/rpc/apimiddleware/custom_handlers.go +++ b/beacon-chain/rpc/apimiddleware/custom_handlers.go @@ -185,7 +185,11 @@ func receiveEvents(eventChan <-chan *sse.Event, w http.ResponseWriter, req *http case msg := <-eventChan: var data interface{} - switch strings.TrimSpace(string(msg.Event)) { + // The message's event comes to us with trailing whitespace. Remove it here for + // ease of future procesing. + msg.Event = bytes.TrimSpace(msg.Event) + + switch string(msg.Event) { case events.HeadTopic: data = &eventHeadJson{} case events.BlockTopic: @@ -215,7 +219,7 @@ func receiveEvents(eventChan <-chan *sse.Event, w http.ResponseWriter, req *http data = &eventErrorJson{} default: return &gateway.DefaultErrorJson{ - Message: fmt.Sprintf("Event type '%s' not supported", strings.TrimSpace(string(msg.Event))), + Message: fmt.Sprintf("Event type '%s' not supported", string(msg.Event)), Code: http.StatusInternalServerError, } } diff --git a/beacon-chain/rpc/apimiddleware/custom_handlers_test.go b/beacon-chain/rpc/apimiddleware/custom_handlers_test.go index 43a87b6746..ac68a2ef13 100644 --- a/beacon-chain/rpc/apimiddleware/custom_handlers_test.go +++ b/beacon-chain/rpc/apimiddleware/custom_handlers_test.go @@ -191,6 +191,40 @@ func TestReceiveEvents_EventNotSupported(t *testing.T) { assert.Equal(t, "Event type 'not_supported' not supported", errJson.Msg()) } +func TestReceiveEvents_TrailingSpace(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + ch := make(chan *sse.Event) + w := httptest.NewRecorder() + w.Body = &bytes.Buffer{} + req := httptest.NewRequest("GET", "http://foo.example", &bytes.Buffer{}) + req = req.WithContext(ctx) + + go func() { + base64Val := "Zm9v" + data := &eventFinalizedCheckpointJson{ + Block: base64Val, + State: base64Val, + Epoch: "1", + } + bData, err := json.Marshal(data) + require.NoError(t, err) + msg := &sse.Event{ + Data: bData, + Event: []byte("finalized_checkpoint "), + } + ch <- msg + time.Sleep(time.Second) + cancel() + }() + + errJson := receiveEvents(ch, w, req) + assert.Equal(t, true, errJson == nil) + assert.Equal(t, `event: finalized_checkpoint +data: {"block":"0x666f6f","state":"0x666f6f","epoch":"1"} + +`, w.Body.String()) +} + func TestWriteEvent(t *testing.T) { base64Val := "Zm9v" data := &eventFinalizedCheckpointJson{