All tests passes

This commit is contained in:
Alex Myasoedov 2016-03-03 11:38:57 +02:00
parent 5b877cfbdc
commit b94899938d
4 changed files with 30 additions and 11 deletions

View file

@ -1,6 +1,7 @@
package main
import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
@ -12,9 +13,14 @@ import (
const Cookie = "Set-Cookie"
func client(method, path, cookie string) *httptest.ResponseRecorder {
return request(method, path, cookie, "")
}
func request(method, path, cookie string, body string) *httptest.ResponseRecorder {
gin.SetMode("test")
app := NewApp()
req, _ := http.NewRequest(method, path, nil)
payload := bytes.NewBufferString(body)
req, _ := http.NewRequest(method, path, payload)
if len(cookie) != 0 {
req.Header.Set("Cookie", cookie)
}
@ -41,7 +47,7 @@ func Test(t *testing.T) {
})
g.It("Should return 200 on PUT /slides.md ", func() {
w := client("PUT", "/slides.md", cookie)
w := request("PUT", "/slides.md", cookie, "whatever")
g.Assert(w.Code).Equal(200)
})
@ -51,7 +57,7 @@ func Test(t *testing.T) {
})
g.It("Should return specific slide in preview", func() {
w := client("GET", "/published/shy-cell.md", cookie)
w := client("GET", "/published/slides/shy-cell.md", cookie)
g.Assert(w.Code).Equal(200)
})
@ -60,6 +66,16 @@ func Test(t *testing.T) {
g.Assert(w.Code).Equal(200)
})
g.It("Should return specific slide in preview without session", func() {
w := client("GET", "/published/slides/shy-cell.md", "")
g.Assert(w.Code).Equal(200)
})
g.It("Should return specific slide in edit mode without session", func() {
w := client("GET", "/stash/edit/shy-cell.md", "")
g.Assert(w.Code).Equal(200)
})
g.It("Should works")
})

13
main.go
View file

@ -3,7 +3,6 @@ package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
@ -22,7 +21,7 @@ func NewApp() *gin.Engine {
store := sessions.NewCookieStore([]byte("secret"))
r.Use(sessions.Sessions(SessionHeader, store))
r.LoadHTMLGlob("templates/index.tmpl")
r.LoadHTMLGlob("templates/*.tmpl")
r.Static("/static", "./static")
r.GET("/", func(c *gin.Context) {
@ -43,7 +42,7 @@ func NewApp() *gin.Engine {
session.Set("name", path)
session.Save()
c.HTML(200, "users/index.tmpl", gin.H{
c.HTML(200, "index.tmpl", gin.H{
"pubTo": path,
})
})
@ -133,12 +132,12 @@ func NewApp() *gin.Engine {
session.Set("name", path)
session.Save()
c.HTML(200, "users/index.tmpl", gin.H{
c.HTML(200, "index.tmpl", gin.H{
"pubTo": path,
})
})
r.GET("/published/:name", func(c *gin.Context) {
r.GET("/published/slides/:name", func(c *gin.Context) {
name := c.Param("name")
log.WithFields(log.Fields{
@ -152,7 +151,9 @@ func NewApp() *gin.Engine {
session := sessions.Default(c)
session.Set("name", path)
session.Save()
c.Redirect(http.StatusMovedPermanently, "static/preview")
c.HTML(200, "slides.tmpl", gin.H{
"pubTo": path,
})
})
return r

View file

@ -1,4 +1,4 @@
{{ define "users/index.tmpl" }}
{{ define "index.tmpl" }}
<!doctype html>
<html lang="en">
@ -14,7 +14,7 @@
<body>
<div id="edit-pane">
<div id="controls">
<a href="static/slides.html" target="_blank" onclick="save();"> Present</a>
<a href="/published/{{ .pubTo}}" target="_blank" onclick="save();"> Present</a>
</div>
<div id="editor"></div>
</div>

View file

@ -1,3 +1,4 @@
{{ define "slides.tmpl" }}
<!doctype html>
<html lang="en">
@ -47,3 +48,4 @@
<script src="/static/js/slides.js"></script>
</body>
</html>
{{ end }}