Errors: Add HTTP writer for errutil.Error (#57661)

This commit is contained in:
Emil Tullstedt
2022-10-26 16:57:53 +02:00
committed by GitHub
parent 05872283b5
commit 1e2b7c5368
2 changed files with 129 additions and 0 deletions
+29
View File
@@ -0,0 +1,29 @@
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.NewBase(errutil.StatusTimeout, 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())
}