forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwriter_test.go
50 lines (41 loc) · 1.35 KB
/
writer_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package errhttp
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"k8s.io/apiserver/pkg/endpoints/request"
"github.com/grafana/grafana/pkg/apimachinery/errutil"
)
func TestWrite(t *testing.T) {
// Error without k8s context
recorder := doError(t, context.Background())
assert.Equal(t, http.StatusGatewayTimeout, recorder.Code)
assert.JSONEq(t, `{"message": "Timeout", "messageId": "test.thisIsExpected", "statusCode": 504}`, recorder.Body.String())
// Another request, but within the k8s framework
recorder = doError(t, request.WithRequestInfo(context.Background(), &request.RequestInfo{
APIGroup: "TestGroup",
}))
assert.Equal(t, http.StatusGatewayTimeout, recorder.Code)
assert.JSONEq(t, `{
"status": "Failure",
"reason": "Timeout",
"metadata": {},
"message": "Timeout",
"details": { "uid": "test.thisIsExpected" },
"code": 504
}`, recorder.Body.String())
}
func doError(t *testing.T, ctx context.Context) *httptest.ResponseRecorder {
t.Helper()
const msgID = "test.thisIsExpected"
base := errutil.Timeout(msgID)
handler := func(writer http.ResponseWriter, _ *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)
return recorder
}