1
0
Fork 0

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"
)
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)
})

28
main.go
View file

@ -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, "")
})