diff --git a/proxy/http_test.go b/proxy/http_test.go index 1d67a10e0..9fd1f3cfc 100644 --- a/proxy/http_test.go +++ b/proxy/http_test.go @@ -240,7 +240,7 @@ func TestNewHTTPProxy_badStatusCode_detailed(t *testing.T) { t.Errorf("unexpected error code: %d", response.Metadata.StatusCode) } b, _ := json.Marshal(response.Data) - if string(b) != `{"error_some":{"http_status_code":500,"http_body":"booom\n"}}` { + if string(b) != `{"error_some":{"http_status_code":500,"http_body":"booom\n","http_body_encoding":"text/plain; charset=utf-8"}}` { t.Errorf("unexpected response content: %s", string(b)) } select { diff --git a/router/mux/router_test.go b/router/mux/router_test.go index 955084212..f7f0b58a8 100644 --- a/router/mux/router_test.go +++ b/router/mux/router_test.go @@ -174,14 +174,6 @@ func TestDefaultFactory_ko(t *testing.T) { Method: "GETTT", Backend: []*config.Backend{}, }, - { - Endpoint: "/also-ignored", - Method: "PUT", - Backend: []*config.Backend{ - {}, - {}, - }, - }, }, } diff --git a/test/integration_test.go b/test/integration_test.go index 7f5d96130..b41bc00dc 100644 --- a/test/integration_test.go +++ b/test/integration_test.go @@ -73,6 +73,7 @@ func TestKrakenD_ginRouter(t *testing.T) { "trusted_proxies": []interface{}{"127.0.0.1/32", "::1"}, "remote_ip_headers": []interface{}{"x-forwarded-for"}, "forwarded_by_client_ip": true, + "return_error_msg": true, } ignoredChan := make(chan string) @@ -238,7 +239,7 @@ func testKrakenD(t *testing.T, runRouter func(logging.Logger, *config.ServiceCon url: "/detail_error", headers: map[string]string{}, expHeaders: incompleteHeader, - expBody: `{"email":"some@email.com","error_backend_a":{"http_status_code":429,"http_body":"sad panda\n"},"name":"a"}`, + expBody: `{"email":"some@email.com","error_backend_a":{"http_status_code":429,"http_body":"sad panda\n","http_body_encoding":"text/plain; charset=utf-8"},"name":"a"}`, }, { name: "querystring-params-no-params", @@ -454,7 +455,6 @@ func testKrakenD(t *testing.T, runRouter func(logging.Logger, *config.ServiceCon } }) } - } func setupBackend(t *testing.T) (*config.ServiceConfig, error) { diff --git a/test/lura.json b/test/lura.json index 6ee7da48f..ee74c76a1 100644 --- a/test/lura.json +++ b/test/lura.json @@ -8,6 +8,9 @@ "extra_config":{ "github_com/luraproject/lura/router/gin":{ "return_error_msg":true + }, + "github_com/luraproject/lura/router/mux":{ + "return_error_msg":true } }, "endpoints": [ diff --git a/transport/http/client/status.go b/transport/http/client/status.go index 25d846162..893a77dc8 100644 --- a/transport/http/client/status.go +++ b/transport/http/client/status.go @@ -87,6 +87,7 @@ func newHTTPResponseError(resp *http.Response) HTTPResponseError { return HTTPResponseError{ Code: resp.StatusCode, Msg: string(body), + Enc: resp.Header.Get("Content-Type"), } } @@ -94,6 +95,7 @@ func newHTTPResponseError(resp *http.Response) HTTPResponseError { type HTTPResponseError struct { Code int `json:"http_status_code"` Msg string `json:"http_body,omitempty"` + Enc string `json:"http_body_encoding,omitempty"` } // Error returns the error message @@ -106,6 +108,11 @@ func (r HTTPResponseError) StatusCode() int { return r.Code } +// Encoding returns the content type returned by the backend +func (r HTTPResponseError) Encoding() string { + return r.Enc +} + // NamedHTTPResponseError is the error to be returned by the DetailedHTTPStatusHandler type NamedHTTPResponseError struct { HTTPResponseError diff --git a/transport/http/client/status_test.go b/transport/http/client/status_test.go index 82912f281..45b1ae898 100644 --- a/transport/http/client/status_test.go +++ b/transport/http/client/status_test.go @@ -5,6 +5,7 @@ package client import ( "bytes" "context" + "fmt" "io" "net/http" "testing" @@ -14,6 +15,7 @@ import ( func TestDetailedHTTPStatusHandler(t *testing.T) { expectedErrName := "some" + expectedEncoding := "application/json; charset=utf-8" cfg := &config.Backend{ ExtraConfig: config.ExtraConfig{ Namespace: map[string]interface{}{ @@ -47,7 +49,8 @@ func TestDetailedHTTPStatusHandler(t *testing.T) { resp := &http.Response{ StatusCode: code, - Body: io.NopCloser(bytes.NewBufferString(msg)), + Body: io.NopCloser(bytes.NewBufferString(fmt.Sprintf(`{"msg":%q}`, msg))), + Header: http.Header{"Content-Type": []string{expectedEncoding}}, } r, err := sh(context.Background(), resp) @@ -68,7 +71,7 @@ func TestDetailedHTTPStatusHandler(t *testing.T) { return } - if e.Error() != msg { + if e.Error() != fmt.Sprintf(`{"msg":%q}`, msg) { t.Errorf("#%d unexpected message: %s", i, e.Msg) return } @@ -77,6 +80,10 @@ func TestDetailedHTTPStatusHandler(t *testing.T) { t.Errorf("#%d unexpected error name: %s", i, e.name) return } + + if e.Encoding() != expectedEncoding { + t.Errorf("#%d unexpected encoding: %s", i, e.Enc) + } } }