package render
import (
"encoding/xml"
"net/http"
"net/http/httptest"
"testing"
)
type GreetingXML struct {
XMLName xml.Name `xml:"greeting"`
One string `xml:"one,attr"`
Two string `xml:"two,attr"`
}
func TestXMLBasic(t *testing.T) {
render := New(Options{
// nothing here to configure
})
var err error
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err = render.XML(w, 299, GreetingXML{One: "hello", Two: "world"})
})
res := httptest.NewRecorder()
req, _ := http.NewRequestWithContext(ctx, "GET", "/foo", nil)
h.ServeHTTP(res, req)
expectNil(t, err)
expect(t, res.Code, 299)
expect(t, res.Header().Get(ContentType), ContentXML+"; charset=UTF-8")
expect(t, res.Body.String(), "")
}
func TestXMLPrefix(t *testing.T) {
prefix := "\n"
render := New(Options{
PrefixXML: []byte(prefix),
})
var err error
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err = render.XML(w, 300, GreetingXML{One: "hello", Two: "world"})
})
res := httptest.NewRecorder()
req, _ := http.NewRequestWithContext(ctx, "GET", "/foo", nil)
h.ServeHTTP(res, req)
expectNil(t, err)
expect(t, res.Code, 300)
expect(t, res.Header().Get(ContentType), ContentXML+"; charset=UTF-8")
expect(t, res.Body.String(), prefix+"")
}
func TestXMLIndented(t *testing.T) {
render := New(Options{
IndentXML: true,
})
var err error
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err = render.XML(w, http.StatusOK, GreetingXML{One: "hello", Two: "world"})
})
res := httptest.NewRecorder()
req, _ := http.NewRequestWithContext(ctx, "GET", "/foo", nil)
h.ServeHTTP(res, req)
expectNil(t, err)
expect(t, res.Code, http.StatusOK)
expect(t, res.Header().Get(ContentType), ContentXML+"; charset=UTF-8")
expect(t, res.Body.String(), "\n")
}
func TestXMLWithError(t *testing.T) {
render := New(Options{
// nothing here to configure
})
var err error
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err = render.XML(w, 299, map[string]string{"foo": "bar"})
})
res := httptest.NewRecorder()
req, _ := http.NewRequestWithContext(ctx, "GET", "/foo", nil)
h.ServeHTTP(res, req)
expectNotNil(t, err)
expect(t, res.Code, 500)
}
func TestXMLCustomContentType(t *testing.T) {
render := New(Options{
XMLContentType: "application/customxml",
})
var err error
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err = render.XML(w, http.StatusOK, GreetingXML{One: "hello", Two: "world"})
})
res := httptest.NewRecorder()
req, _ := http.NewRequestWithContext(ctx, "GET", "/foo", nil)
h.ServeHTTP(res, req)
expectNil(t, err)
expect(t, res.Code, http.StatusOK)
expect(t, res.Header().Get(ContentType), "application/customxml; charset=UTF-8")
expect(t, res.Body.String(), "")
}
func TestXMLDisabledCharset(t *testing.T) {
render := New(Options{
DisableCharset: true,
})
var err error
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err = render.XML(w, http.StatusOK, GreetingXML{One: "hello", Two: "world"})
})
res := httptest.NewRecorder()
req, _ := http.NewRequestWithContext(ctx, "GET", "/foo", nil)
h.ServeHTTP(res, req)
expectNil(t, err)
expect(t, res.Code, http.StatusOK)
expect(t, res.Header().Get(ContentType), ContentXML)
expect(t, res.Body.String(), "")
}