@@ -3,6 +3,7 @@ package transport
33import (
44 "bytes"
55 "context"
6+ "encoding/json"
67 "io"
78 "net/http"
89 "net/http/httptest"
@@ -13,14 +14,18 @@ import (
1314
1415 "github.com/go-kit/log"
1516 "github.com/pkg/errors"
17+ v1 "github.com/prometheus/client_golang/api/prometheus/v1"
1618 "github.com/prometheus/client_golang/prometheus"
1719 promtest "github.com/prometheus/client_golang/prometheus/testutil"
1820 "github.com/stretchr/testify/assert"
1921 "github.com/stretchr/testify/require"
2022 "github.com/weaveworks/common/httpgrpc"
2123 "github.com/weaveworks/common/user"
24+ "google.golang.org/grpc/codes"
2225
2326 querier_stats "github.com/cortexproject/cortex/pkg/querier/stats"
27+ util_api "github.com/cortexproject/cortex/pkg/util/api"
28+ util_log "github.com/cortexproject/cortex/pkg/util/log"
2429)
2530
2631type roundTripperFunc func (* http.Request ) (* http.Response , error )
@@ -34,19 +39,111 @@ func TestWriteError(t *testing.T) {
3439 status int
3540 err error
3641 additionalHeaders http.Header
42+ expectedErrResp util_api.Response
3743 }{
38- {http .StatusInternalServerError , errors .New ("unknown" ), http.Header {"User-Agent" : []string {"Golang" }}},
39- {http .StatusInternalServerError , errors .New ("unknown" ), nil },
40- {http .StatusGatewayTimeout , context .DeadlineExceeded , nil },
41- {StatusClientClosedRequest , context .Canceled , nil },
42- {StatusClientClosedRequest , context .Canceled , http.Header {"User-Agent" : []string {"Golang" }}},
43- {StatusClientClosedRequest , context .Canceled , http.Header {"User-Agent" : []string {"Golang" }, "Content-Type" : []string {"application/json" }}},
44- {http .StatusBadRequest , httpgrpc .Errorf (http .StatusBadRequest , "" ), http.Header {}},
45- {http .StatusRequestEntityTooLarge , errors .New ("http: request body too large" ), http.Header {}},
44+ {
45+ http .StatusInternalServerError ,
46+ errors .New ("unknown" ),
47+ http.Header {"User-Agent" : []string {"Golang" }},
48+ util_api.Response {
49+ Status : "error" ,
50+ ErrorType : v1 .ErrServer ,
51+ Error : "unknown" ,
52+ },
53+ },
54+ {
55+ http .StatusInternalServerError ,
56+ errors .New ("unknown" ),
57+ nil ,
58+ util_api.Response {
59+ Status : "error" ,
60+ ErrorType : v1 .ErrServer ,
61+ Error : "unknown" ,
62+ },
63+ },
64+ {
65+ http .StatusGatewayTimeout ,
66+ context .DeadlineExceeded ,
67+ nil ,
68+ util_api.Response {
69+ Status : "error" ,
70+ ErrorType : v1 .ErrTimeout ,
71+ Error : "" ,
72+ },
73+ },
74+ {
75+ StatusClientClosedRequest ,
76+ context .Canceled ,
77+ nil ,
78+ util_api.Response {
79+ Status : "error" ,
80+ ErrorType : v1 .ErrCanceled ,
81+ Error : "" ,
82+ },
83+ },
84+ {
85+ StatusClientClosedRequest ,
86+ context .Canceled ,
87+ http.Header {"User-Agent" : []string {"Golang" }},
88+ util_api.Response {
89+ Status : "error" ,
90+ ErrorType : v1 .ErrCanceled ,
91+ Error : "" ,
92+ },
93+ },
94+ {
95+ StatusClientClosedRequest ,
96+ context .Canceled ,
97+ http.Header {"User-Agent" : []string {"Golang" }, "Content-Type" : []string {"application/json" }},
98+ util_api.Response {
99+ Status : "error" ,
100+ ErrorType : v1 .ErrCanceled ,
101+ Error : "" ,
102+ },
103+ },
104+ {http .StatusBadRequest ,
105+ httpgrpc .Errorf (http .StatusBadRequest , "" ),
106+ http.Header {},
107+ util_api.Response {
108+ Status : "error" ,
109+ ErrorType : v1 .ErrBadData ,
110+ Error : "" ,
111+ },
112+ },
113+ {
114+ http .StatusRequestEntityTooLarge ,
115+ errors .New ("http: request body too large" ),
116+ http.Header {},
117+ util_api.Response {
118+ Status : "error" ,
119+ ErrorType : v1 .ErrBadData ,
120+ Error : "http: request body too large" ,
121+ },
122+ },
123+ {
124+ http .StatusUnprocessableEntity ,
125+ httpgrpc .Errorf (http .StatusUnprocessableEntity , "limit hit" ),
126+ http.Header {},
127+ util_api.Response {
128+ Status : "error" ,
129+ ErrorType : v1 .ErrExec ,
130+ Error : "limit hit" ,
131+ },
132+ },
133+ {
134+ http .StatusUnprocessableEntity ,
135+ httpgrpc .Errorf (int (codes .PermissionDenied ), "permission denied" ),
136+ http.Header {},
137+ util_api.Response {
138+ Status : "error" ,
139+ ErrorType : v1 .ErrBadData ,
140+ Error : "permission denied" ,
141+ },
142+ },
46143 } {
47144 t .Run (test .err .Error (), func (t * testing.T ) {
48145 w := httptest .NewRecorder ()
49- writeError (w , test .err , test .additionalHeaders )
146+ writeError (util_log . Logger , w , test .err , test .additionalHeaders )
50147 require .Equal (t , test .status , w .Result ().StatusCode )
51148 expectedAdditionalHeaders := test .additionalHeaders
52149 if expectedAdditionalHeaders != nil {
@@ -56,6 +153,18 @@ func TestWriteError(t *testing.T) {
56153 }
57154 }
58155 }
156+ data , err := io .ReadAll (w .Result ().Body )
157+ require .NoError (t , err )
158+ var res util_api.Response
159+ err = json .Unmarshal (data , & res )
160+ require .NoError (t , err )
161+ resp , ok := httpgrpc .HTTPResponseFromError (test .err )
162+ if ok {
163+ require .Equal (t , string (resp .Body ), res .Error )
164+ } else {
165+ require .Equal (t , test .err .Error (), res .Error )
166+
167+ }
59168 })
60169 }
61170}
0 commit comments