add tests for c.JSON and c.HTML renderers

This commit is contained in:
2024-11-13 14:56:55 +01:00
parent f9990d37c0
commit 749cacedc9
2 changed files with 82 additions and 2 deletions
+6 -2
View File
@@ -20,7 +20,7 @@ type Engine struct {
Renderer *Render
}
func (c *Context) JSON(code int, data Data) {
func (c *Context) JSON(code int, data any) {
c.W.WriteHeader(code)
c.W.Header().Set("Content-Type", "application/json")
encoder := json.NewEncoder(c.W)
@@ -31,9 +31,13 @@ func (c *Context) JSON(code int, data Data) {
func (c *Context) HTML(code int, name string, data Data) {
c.W.WriteHeader(code)
c.E.Renderer.Template(c.W, name, &TemplateData{
c.W.Header().Set("Content-Type", "text/html; charset=utf-8")
err := c.E.Renderer.Template(c.W, name, &TemplateData{
Data: data,
})
if err != nil {
http.Error(c.W, err.Error(), http.StatusInternalServerError)
}
}
func New() *Engine {