Alex Myasoedov пре 8 година
родитељ
комит
1357663bf7
2 измењених фајлова са 39 додато и 6 уклоњено
  1. 13 4
      app_test.go
  2. 26 2
      main.go

+ 13 - 4
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)
 		})
 

+ 26 - 2
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, "")
 	})