Skip to content

Commit

Permalink
Merge pull request luraproject#708 from luraproject/error_content_type
Browse files Browse the repository at this point in the history
Add the encoding to the HTTPResponseError
  • Loading branch information
kpacha authored Feb 16, 2024
2 parents 7255a26 + f91f22e commit a5d1480
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 13 deletions.
2 changes: 1 addition & 1 deletion proxy/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 0 additions & 8 deletions router/mux/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,6 @@ func TestDefaultFactory_ko(t *testing.T) {
Method: "GETTT",
Backend: []*config.Backend{},
},
{
Endpoint: "/also-ignored",
Method: "PUT",
Backend: []*config.Backend{
{},
{},
},
},
},
}

Expand Down
4 changes: 2 additions & 2 deletions test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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":"[email protected]","error_backend_a":{"http_status_code":429,"http_body":"sad panda\n"},"name":"a"}`,
expBody: `{"email":"[email protected]","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",
Expand Down Expand Up @@ -454,7 +455,6 @@ func testKrakenD(t *testing.T, runRouter func(logging.Logger, *config.ServiceCon
}
})
}

}

func setupBackend(t *testing.T) (*config.ServiceConfig, error) {
Expand Down
3 changes: 3 additions & 0 deletions test/lura.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
7 changes: 7 additions & 0 deletions transport/http/client/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,15 @@ func newHTTPResponseError(resp *http.Response) HTTPResponseError {
return HTTPResponseError{
Code: resp.StatusCode,
Msg: string(body),
Enc: resp.Header.Get("Content-Type"),
}
}

// HTTPResponseError is the error to be returned by the ErrorHTTPStatusHandler
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
Expand All @@ -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
Expand Down
11 changes: 9 additions & 2 deletions transport/http/client/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package client
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"testing"
Expand All @@ -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{}{
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
Expand All @@ -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)
}
}
}

Expand Down

0 comments on commit a5d1480

Please sign in to comment.