From 875be55076959e2f1744509c4c7f4315519d2fdf Mon Sep 17 00:00:00 2001 From: Muyao CHEN Date: Tue, 2 Jul 2024 13:57:45 +0200 Subject: [PATCH] Writing tests for Render package --- internal/render/render.go | 9 +++- internal/render/render_test.go | 76 ++++++++++++++++++++++++++++++++++ internal/render/setup_test.go | 52 +++++++++++++++++++++++ 3 files changed, 135 insertions(+), 2 deletions(-) create mode 100644 internal/render/render_test.go create mode 100644 internal/render/setup_test.go diff --git a/internal/render/render.go b/internal/render/render.go index 14ae06b..8b0ea55 100644 --- a/internal/render/render.go +++ b/internal/render/render.go @@ -2,6 +2,7 @@ package render import ( "bytes" + "errors" "fmt" "go-udemy-web-1/internal/config" "go-udemy-web-1/internal/models" @@ -35,7 +36,7 @@ func AddDefaultData(td *models.TemplateData, r *http.Request) *models.TemplateDa } // RenderTemplate renders a HTML template file -func RenderTemplate(w http.ResponseWriter, r *http.Request, tmpl string, td *models.TemplateData) { +func RenderTemplate(w http.ResponseWriter, r *http.Request, tmpl string, td *models.TemplateData) error { var tc map[string]*template.Template if app.UseCache { // get the template cache from the app config @@ -47,7 +48,7 @@ func RenderTemplate(w http.ResponseWriter, r *http.Request, tmpl string, td *mod // get requested template from cache t, ok := tc[tmpl] if !ok { - log.Fatal("Could not get template from template cache") + return errors.New("could not get template from template cache") } // Write to a buffer to make sure that the template can be read and @@ -59,13 +60,17 @@ func RenderTemplate(w http.ResponseWriter, r *http.Request, tmpl string, td *mod err := t.Execute(buf, td) if err != nil { log.Println(err) + return err } // render the template _, err = buf.WriteTo(w) if err != nil { log.Println(err) + return err } + + return nil } func CreateTemplateCache() (map[string]*template.Template, error) { diff --git a/internal/render/render_test.go b/internal/render/render_test.go new file mode 100644 index 0000000..c33e2b0 --- /dev/null +++ b/internal/render/render_test.go @@ -0,0 +1,76 @@ +package render + +import ( + "go-udemy-web-1/internal/models" + "net/http" + "testing" +) + +func TestAddDefaultData(t *testing.T) { + var td models.TemplateData + + r, err := getSession() + if err != nil { + t.Error(err) + } + + session.Put(r.Context(), "flash", "123") + + result := AddDefaultData(&td, r) + + if result.Flash != "123" { + t.Error("flash value of 123 not found in session") + } +} + +func TestRenderTemplate(t *testing.T) { + pathToTemplates = "../../templates" + tc, err := CreateTemplateCache() + if err != nil { + t.Error(err) + } + + app.TemplateCahce = tc + + r, err := getSession() + if err != nil { + t.Error(err) + } + + var ww myWriter + + err = RenderTemplate(&ww, r, "home.page.tmpl", &models.TemplateData{}) + if err != nil { + t.Error("error writiing template to browser") + } + + err = RenderTemplate(&ww, r, "non-existent.page.html", &models.TemplateData{}) + if err == nil { + t.Error("rendered template that doesn't exist") + } +} + +func getSession() (*http.Request, error) { + r, err := http.NewRequest("GET", "/some-url", nil) + if err != nil { + return nil, err + } + + ctx := r.Context() + ctx, _ = session.Load(ctx, r.Header.Get("X-Session")) + r = r.WithContext(ctx) + + return r, nil +} + +func TestNewTemplate(t *testing.T) { + NewTemplates(app) +} + +func TestCreateTemplateCache(t *testing.T) { + pathToTemplates = "../../templates" + _, err := CreateTemplateCache() + if err != nil { + t.Error(err) + } +} diff --git a/internal/render/setup_test.go b/internal/render/setup_test.go new file mode 100644 index 0000000..27120a8 --- /dev/null +++ b/internal/render/setup_test.go @@ -0,0 +1,52 @@ +package render + +import ( + "encoding/gob" + "go-udemy-web-1/internal/config" + "go-udemy-web-1/internal/models" + "net/http" + "os" + "testing" + "time" + + "github.com/alexedwards/scs/v2" +) + +var ( + session *scs.SessionManager + testApp config.AppConfig +) + +func TestMain(m *testing.M) { + // what am I going to put in the session + gob.Register(models.Reservation{}) + + // change this to true when in production + testApp.InProduction = false + + session = scs.New() + session.Lifetime = 24 * time.Hour + session.Cookie.Persist = true + session.Cookie.SameSite = http.SameSiteLaxMode + session.Cookie.Secure = testApp.InProduction + + testApp.Session = session + + app = &testApp + + os.Exit(m.Run()) +} + +type myWriter struct{} + +func (tw *myWriter) Header() http.Header { + var h http.Header + return h +} + +func (tw *myWriter) WriteHeader(i int) {} + +func (tw *myWriter) Write(b []byte) (int, error) { + length := len(b) + return length, nil +}