Added session to tests
This commit is contained in:
parent
1357663bf7
commit
8ddd1d61bb
2 changed files with 17 additions and 15 deletions
20
app_test.go
20
app_test.go
|
@ -1,21 +1,22 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
. "github.com/franela/goblin"
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
. "github.com/franela/goblin"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
const Cookie = "Set-Cookie"
|
const Cookie = "Set-Cookie"
|
||||||
|
|
||||||
func client(method, path, session string) *httptest.ResponseRecorder {
|
func client(method, path, cookie 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 {
|
if len(cookie) != 0 {
|
||||||
req.Header.Set(Cookie, session)
|
req.Header.Set("Cookie", cookie)
|
||||||
}
|
}
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
app.ServeHTTP(w, req)
|
app.ServeHTTP(w, req)
|
||||||
|
@ -25,23 +26,22 @@ func client(method, path, session 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
|
var cookie string
|
||||||
|
|
||||||
g.It("Should return 200 on / ", func() {
|
g.It("Should return 200 on / ", func() {
|
||||||
w := client("GET", "/", "")
|
w := client("GET", "/", "")
|
||||||
|
|
||||||
g.Assert(w.Code).Equal(200)
|
g.Assert(w.Code).Equal(200)
|
||||||
session = w.HeaderMap.Get(Cookie)
|
cookie = w.HeaderMap.Get(Cookie)
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
g.It("Should return 200 on /slides.md ", func() {
|
g.It("Should return 200 on /slides.md ", func() {
|
||||||
w := client("GET", "/slides.md", session)
|
w := client("GET", "/slides.md", cookie)
|
||||||
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 := client("PUT", "/slides.md", session)
|
w := client("PUT", "/slides.md", cookie)
|
||||||
g.Assert(w.Code).Equal(200)
|
g.Assert(w.Code).Equal(200)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
12
main.go
12
main.go
|
@ -2,12 +2,13 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
log "github.com/Sirupsen/logrus"
|
|
||||||
"github.com/atrox/haikunatorgo"
|
|
||||||
"github.com/gin-gonic/contrib/sessions"
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
log "github.com/Sirupsen/logrus"
|
||||||
|
haikunator "github.com/atrox/haikunatorgo"
|
||||||
|
"github.com/gin-gonic/contrib/sessions"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
const SessionHeader = "slide-session"
|
const SessionHeader = "slide-session"
|
||||||
|
@ -39,12 +40,12 @@ 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 {
|
if val == nil {
|
||||||
c.String(400, "No context")
|
c.String(400, "No context")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
log.WithFields(log.Fields{
|
log.WithFields(log.Fields{
|
||||||
"path": val,
|
"path": val,
|
||||||
|
@ -76,6 +77,7 @@ func NewApp() *gin.Engine {
|
||||||
val := session.Get("name")
|
val := session.Get("name")
|
||||||
if val == nil {
|
if val == nil {
|
||||||
c.String(400, "No context")
|
c.String(400, "No context")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
log.WithFields(log.Fields{
|
log.WithFields(log.Fields{
|
||||||
"path": val,
|
"path": val,
|
||||||
|
|
Loading…
Reference in a new issue