Add Render test

pull/455/head
Fenny 2020-06-07 22:23:10 +02:00
parent d7dd49f01a
commit d48583619a
5 changed files with 17 additions and 11 deletions

1
.github/index.tmpl vendored Normal file
View File

@ -0,0 +1 @@
<h1>{{.Title}}</h1>

6
ctx.go
View File

@ -752,21 +752,19 @@ func (ctx *Ctx) Render(name string, bind interface{}) (err error) {
} else {
// Render raw template using 'name' as filepath if no engine is set
var tmpl *template.Template
raw := bytebufferpool.Get()
defer bytebufferpool.Put(raw)
// Read file
f, err := os.Open(filepath.Clean(name))
if err != nil {
return err
}
if _, err = raw.ReadFrom(f); err != nil {
if _, err = buf.ReadFrom(f); err != nil {
return err
}
if err = f.Close(); err != nil {
return err
}
// Parse template
if tmpl, err = template.New("").Parse(raw.String()); err != nil {
if tmpl, err = template.New("").Parse(buf.String()); err != nil {
return err
}
// Render template

View File

@ -1049,10 +1049,17 @@ func Test_Ctx_Redirect(t *testing.T) {
utils.AssertEqual(t, "http://example.com", string(ctx.Fasthttp.Response.Header.Peek(HeaderLocation)))
}
// ViewEngine is coming in v1.10
// func Test_Ctx_Render(t *testing.T) {
// // TODO
// }
// go test -run Test_Ctx_Render
func Test_Ctx_Render(t *testing.T) {
t.Parallel()
app := New()
ctx := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(ctx)
ctx.Render("./.github/index.tmpl", Map{
"Title": "Hello, World!",
})
utils.AssertEqual(t, "<h1>Hello, World!</h1>", string(ctx.Fasthttp.Response.Body()))
}
// go test -run Test_Ctx_Render_Go_Template
func Test_Ctx_Render_Go_Template(t *testing.T) {

2
go.mod
View File

@ -3,7 +3,7 @@ module github.com/gofiber/fiber
go 1.11
require (
github.com/gofiber/utils v0.0.5
github.com/gofiber/utils v0.0.6
github.com/gorilla/schema v1.1.0
github.com/valyala/bytebufferpool v1.0.0
github.com/valyala/fasthttp v1.14.0

4
go.sum
View File

@ -1,7 +1,7 @@
github.com/andybalholm/brotli v1.0.0 h1:7UCwP93aiSfvWpapti8g88vVVGp2qqtGyePsSuDafo4=
github.com/andybalholm/brotli v1.0.0/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
github.com/gofiber/utils v0.0.5 h1:2nTakZAQk2GvOJwmBk+jez4sOm4c2X9I0uK3HWZMdV0=
github.com/gofiber/utils v0.0.5/go.mod h1:pacRFtghAE3UoknMOUiXh2Io/nLWSUHtQCi/3QASsOc=
github.com/gofiber/utils v0.0.6 h1:lWuJfXQ06aFPVA2/y7lUw9ahS4TuXhbwXnxKl1D/fuY=
github.com/gofiber/utils v0.0.6/go.mod h1:pacRFtghAE3UoknMOUiXh2Io/nLWSUHtQCi/3QASsOc=
github.com/gorilla/schema v1.1.0 h1:CamqUDOFUBqzrvxuz2vEwo8+SUdwsluFh7IlzJh30LY=
github.com/gorilla/schema v1.1.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU=
github.com/klauspost/compress v1.10.4 h1:jFzIFaf586tquEB5EhzQG0HwGNSlgAJpG53G6Ss11wc=