grafana/pkg/util/errutil/errhttp/writer_test.go
Marcus Efraimsson 040b7d2571
Chore: Add errutils helpers (#73577)
Add helpers for the errutil package in favor of errutil.NewBase.
2023-08-22 12:52:24 +02:00

30 lines
746 B
Go

package errhttp
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/grafana/grafana/pkg/util/errutil"
)
func TestWrite(t *testing.T) {
ctx := context.Background()
const msgID = "test.thisIsExpected"
base := errutil.Timeout(msgID)
handler := func(writer http.ResponseWriter, request *http.Request) {
Write(ctx, base.Errorf("got expected error"), writer)
}
req := httptest.NewRequest("GET", "http://localhost:3000/fake", nil)
recorder := httptest.NewRecorder()
handler(recorder, req)
assert.Equal(t, http.StatusGatewayTimeout, recorder.Code)
assert.JSONEq(t, `{"message": "Timeout", "messageId": "test.thisIsExpected", "statusCode": 504}`, recorder.Body.String())
}