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

13
main.go
View file

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

View file

@ -1,4 +1,4 @@
{{ define "users/index.tmpl" }} {{ define "index.tmpl" }}
<!doctype html> <!doctype html>
<html lang="en"> <html lang="en">
@ -14,7 +14,7 @@
<body> <body>
<div id="edit-pane"> <div id="edit-pane">
<div id="controls"> <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>
<div id="editor"></div> <div id="editor"></div>
</div> </div>

View file

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