Skip to content

Commit

Permalink
support error templates for text/xml (#3392)
Browse files Browse the repository at this point in the history
(cherry picked from commit d88e9a5)
  • Loading branch information
gernest authored and Tyk Bot committed Nov 24, 2020
1 parent e785ea6 commit 819319d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
3 changes: 3 additions & 0 deletions gateway/handler_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ func (e *ErrorHandler) HandleError(w http.ResponseWriter, r *http.Request, errMs
case headers.ApplicationXML:
templateExtension = "xml"
contentType = headers.ApplicationXML
case headers.TextXML:
templateExtension = "xml"
contentType = headers.TextXML
default:
templateExtension = "json"
contentType = headers.ApplicationJSON
Expand Down
53 changes: 53 additions & 0 deletions gateway/handler_error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package gateway

import (
"bytes"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"

"github.com/TykTechnologies/tyk/config"
"github.com/TykTechnologies/tyk/headers"
"github.com/TykTechnologies/tyk/test"
)

func TestHandleError_text_xml(t *testing.T) {
file := filepath.Join(config.Global().TemplatePath, "error_500.xml")
xml := `<error>
<code>500</code>
<message>{{.Message}}</message>
</error>`
err := ioutil.WriteFile(file, []byte(xml), 0600)
if err != nil {
t.Fatal(err)
}
defer os.Remove(file)
expect := `
<error>
<code>500</code>
<message>There was a problem proxying the request</message>
</error>
`
ts := StartTest()
defer ts.Close()
h := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
h.Close()
BuildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
spec.Proxy.TargetURL = ts.URL
})
ts.Run(t, test.TestCase{
Path: "/",
Code: http.StatusInternalServerError,
Headers: map[string]string{
headers.ContentType: headers.TextXML,
},
BodyMatchFunc: func(b []byte) bool {
return strings.TrimSpace(expect) == string(bytes.TrimSpace(b))
},
})
}
1 change: 1 addition & 0 deletions headers/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
TykHookshot = "Tyk-Hookshot"
ApplicationJSON = "application/json"
ApplicationXML = "application/xml"
TextXML = "text/xml"
)

const (
Expand Down

0 comments on commit 819319d

Please sign in to comment.