func TestContentTypeNil(t *testing.T) { is := is.New(t) testData := TestStruct{2, "Hehehehe"} w, err := genericRenderTest(render.ContentType(nil), testData, func(r *http.Request) { r.Header.Set("Accept", "text/html") }) is.NotErr(err) is.Equal(w.Header().Get("Content-Type"), "text/html;charset=utf-8") // XXX: too lazy to test this properly }
func TestContentTypeHTML(t *testing.T) { is := is.New(t) testData := TestStruct{2, "Hehehehe"} testTpl, _ := template.New("index").Parse("<b>{{ .A }}</b><i>{{ .B }}</i>") w, err := genericRenderTest(render.ContentType(testTpl), testData, func(r *http.Request) { r.Header.Set("Accept", "text/html") }) is.NotErr(err) is.Equal(w.Header().Get("Content-Type"), "text/html;charset=utf-8") is.Equal(w.Body.String(), "<b>2</b><i>Hehehehe</i>") }
func TestContentTypeXML(t *testing.T) { is := is.New(t) testData := TestStruct{3, "Hahahahah"} w, err := genericRenderTest(render.ContentType(nil), testData, func(r *http.Request) { r.Header.Set("Accept", "application/xml") }) is.NotErr(err) is.Equal(w.Header().Get("Content-Type"), "application/xml;charset=utf-8") var res TestStruct is.NotErr(xml.Unmarshal(w.Body.Bytes(), &res)) is.Equal(res, testData) }
func TestContentTypeParsesAccept(t *testing.T) { is := is.New(t) contentTypes := []struct { Expected string Received string }{ {"application/xml", "application/xml;charset=utf-8"}, {"application/json", "application/json;charset=utf-8"}, {"text/xml", "text/xml;charset=utf-8"}, {"text/html", "text/html;charset=utf-8"}, {"", "application/json;charset=utf-8"}, } for _, ct := range contentTypes { w, err := genericRenderTest(render.ContentType(nil), nil, func(r *http.Request) { if ct.Expected != "" { r.Header.Set("Accept", ct.Expected) } }) is.NotErr(err) is.Equal(w.Header().Get("Content-Type"), ct.Received) } }