From 1357663bf7cacf9b6c96675b81313fd31d162c9a Mon Sep 17 00:00:00 2001 From: Alex Myasoedov Date: Tue, 1 Mar 2016 20:14:44 +0200 Subject: [PATCH] WIP testing and logging --- app_test.go | 17 +++++++++++++---- main.go | 28 ++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/app_test.go b/app_test.go index a7a1449..a285258 100644 --- a/app_test.go +++ b/app_test.go @@ -8,10 +8,15 @@ import ( "testing" ) -func performRequest(method, path string) *httptest.ResponseRecorder { +const Cookie = "Set-Cookie" + +func client(method, path, session string) *httptest.ResponseRecorder { gin.SetMode("test") app := NewApp() req, _ := http.NewRequest(method, path, nil) + if len(session) != 0 { + req.Header.Set(Cookie, session) + } w := httptest.NewRecorder() app.ServeHTTP(w, req) return w @@ -20,19 +25,23 @@ func performRequest(method, path string) *httptest.ResponseRecorder { func Test(t *testing.T) { g := Goblin(t) g.Describe("App api", func() { + var session string g.It("Should return 200 on / ", func() { - w := performRequest("GET", "/") + w := client("GET", "/", "") + g.Assert(w.Code).Equal(200) + session = w.HeaderMap.Get(Cookie) + }) g.It("Should return 200 on /slides.md ", func() { - w := performRequest("GET", "/slides.md") + w := client("GET", "/slides.md", session) g.Assert(w.Code).Equal(200) }) g.It("Should return 200 on PUT /slides.md ", func() { - w := performRequest("PUT", "/slides.md") + w := client("PUT", "/slides.md", session) g.Assert(w.Code).Equal(200) }) diff --git a/main.go b/main.go index bbf9698..8cbacb0 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "fmt" + log "github.com/Sirupsen/logrus" "github.com/atrox/haikunatorgo" "github.com/gin-gonic/contrib/sessions" "github.com/gin-gonic/gin" @@ -9,12 +10,14 @@ import ( "os" ) +const SessionHeader = "slide-session" + func NewApp() *gin.Engine { r := gin.Default() store := sessions.NewCookieStore([]byte("secret")) - r.Use(sessions.Sessions("mysession", store)) + r.Use(sessions.Sessions(SessionHeader, store)) r.LoadHTMLGlob("templates/index.tmpl") r.Static("/static", "./static") @@ -24,6 +27,9 @@ func NewApp() *gin.Engine { haikunator.TokenLength = 0 name := haikunator.Haikunate() path := fmt.Sprintf("slides/%s.md", name) + log.WithFields(log.Fields{ + "path": path, + }).Info("A new session") session := sessions.Default(c) session.Set("name", path) session.Save() @@ -33,15 +39,22 @@ func NewApp() *gin.Engine { }) }) + r.GET("/slides.md", func(c *gin.Context) { session := sessions.Default(c) val := session.Get("name") + if val == nil { + c.String(400, "No context") + } + log.WithFields(log.Fields{ + "path": val, + }).Info("Got session") path, ok := val.(string) if !ok { c.String(400, "No context") } if _, err := os.Stat(path); err != nil { - // coppy sapmle markdown file to the path + // copy sample markdown file to the path body, err := ioutil.ReadFile("initial-slides.md") if err != nil { panic(err) @@ -61,12 +74,23 @@ func NewApp() *gin.Engine { r.PUT("/slides.md", func(c *gin.Context) { session := sessions.Default(c) val := session.Get("name") + if val == nil { + c.String(400, "No context") + } + log.WithFields(log.Fields{ + "path": val, + }).Info("Got session") path, ok := val.(string) if !ok { c.String(400, "No context") + return } body, _ := ioutil.ReadAll(c.Request.Body) ioutil.WriteFile(path, body, 0644) + log.WithFields(log.Fields{ + "size": len(body), + "file": path, + }).Info("Wrote to file") c.String(200, "") })