I'm trying to test a handler in Go using Echo. Here is the handler:
func submitted(c echo.Context) error {
// do stuff with JSON here
return c.Render(http.StatusOK, "submitted", "true")
}
and the test:
import (
"testing"
"net/http/httptest"
"net/http"
"github.com/labstack/echo"
"strings"
"fmt"
"html/template"
)
var userJSON = `{<values go here>}`
func TestFunction(t *testing.T) {
te := &Template{
templates: template.Must(template.ParseGlob("public/views/*.html")),
}
e := echo.New()
e.Renderer = te
req := httptest.NewRequest(http.MethodPost, "/submitted", strings.NewReader(userJSON))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
fmt.Println(rec.Body.String())
}
However, no body is returned it seems - even though the handler returns submitted.html file when running regularly. Any ideas?