WIP testing and logging

This commit is contained in:
Alex Myasoedov 2016-03-01 20:14:44 +02:00
parent 20d1b189ca
commit 1357663bf7
2 changed files with 39 additions and 6 deletions

View file

@ -8,10 +8,15 @@ import (
"testing" "testing"
) )
func performRequest(method, path string) *httptest.ResponseRecorder { const Cookie = "Set-Cookie"
func client(method, path, session string) *httptest.ResponseRecorder {
gin.SetMode("test") gin.SetMode("test")
app := NewApp() app := NewApp()
req, _ := http.NewRequest(method, path, nil) req, _ := http.NewRequest(method, path, nil)
if len(session) != 0 {
req.Header.Set(Cookie, session)
}
w := httptest.NewRecorder() w := httptest.NewRecorder()
app.ServeHTTP(w, req) app.ServeHTTP(w, req)
return w return w
@ -20,19 +25,23 @@ func performRequest(method, path string) *httptest.ResponseRecorder {
func Test(t *testing.T) { func Test(t *testing.T) {
g := Goblin(t) g := Goblin(t)
g.Describe("App api", func() { g.Describe("App api", func() {
var session string
g.It("Should return 200 on / ", func() { g.It("Should return 200 on / ", func() {
w := performRequest("GET", "/") w := client("GET", "/", "")
g.Assert(w.Code).Equal(200) g.Assert(w.Code).Equal(200)
session = w.HeaderMap.Get(Cookie)
}) })
g.It("Should return 200 on /slides.md ", func() { 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.Assert(w.Code).Equal(200)
}) })
g.It("Should return 200 on PUT /slides.md ", func() { 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) g.Assert(w.Code).Equal(200)
}) })

28
main.go
View file

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
log "github.com/Sirupsen/logrus"
"github.com/atrox/haikunatorgo" "github.com/atrox/haikunatorgo"
"github.com/gin-gonic/contrib/sessions" "github.com/gin-gonic/contrib/sessions"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -9,12 +10,14 @@ import (
"os" "os"
) )
const SessionHeader = "slide-session"
func NewApp() *gin.Engine { func NewApp() *gin.Engine {
r := gin.Default() r := gin.Default()
store := sessions.NewCookieStore([]byte("secret")) store := sessions.NewCookieStore([]byte("secret"))
r.Use(sessions.Sessions("mysession", store)) r.Use(sessions.Sessions(SessionHeader, store))
r.LoadHTMLGlob("templates/index.tmpl") r.LoadHTMLGlob("templates/index.tmpl")
r.Static("/static", "./static") r.Static("/static", "./static")
@ -24,6 +27,9 @@ func NewApp() *gin.Engine {
haikunator.TokenLength = 0 haikunator.TokenLength = 0
name := haikunator.Haikunate() name := haikunator.Haikunate()
path := fmt.Sprintf("slides/%s.md", name) path := fmt.Sprintf("slides/%s.md", name)
log.WithFields(log.Fields{
"path": path,
}).Info("A new session")
session := sessions.Default(c) session := sessions.Default(c)
session.Set("name", path) session.Set("name", path)
session.Save() session.Save()
@ -33,15 +39,22 @@ func NewApp() *gin.Engine {
}) })
}) })
r.GET("/slides.md", func(c *gin.Context) { r.GET("/slides.md", func(c *gin.Context) {
session := sessions.Default(c) session := sessions.Default(c)
val := session.Get("name") 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) path, ok := val.(string)
if !ok { if !ok {
c.String(400, "No context") c.String(400, "No context")
} }
if _, err := os.Stat(path); err != nil { 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") body, err := ioutil.ReadFile("initial-slides.md")
if err != nil { if err != nil {
panic(err) panic(err)
@ -61,12 +74,23 @@ func NewApp() *gin.Engine {
r.PUT("/slides.md", func(c *gin.Context) { r.PUT("/slides.md", func(c *gin.Context) {
session := sessions.Default(c) session := sessions.Default(c)
val := session.Get("name") 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) path, ok := val.(string)
if !ok { if !ok {
c.String(400, "No context") c.String(400, "No context")
return
} }
body, _ := ioutil.ReadAll(c.Request.Body) body, _ := ioutil.ReadAll(c.Request.Body)
ioutil.WriteFile(path, body, 0644) ioutil.WriteFile(path, body, 0644)
log.WithFields(log.Fields{
"size": len(body),
"file": path,
}).Info("Wrote to file")
c.String(200, "") c.String(200, "")
}) })