app_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package main
  2. import (
  3. "bytes"
  4. "net/http"
  5. "net/http/httptest"
  6. "testing"
  7. . "github.com/franela/goblin"
  8. "github.com/gin-gonic/gin"
  9. )
  10. const Cookie = "Set-Cookie"
  11. func client(method, path, cookie string) *httptest.ResponseRecorder {
  12. return request(method, path, cookie, "")
  13. }
  14. func request(method, path, cookie string, body string) *httptest.ResponseRecorder {
  15. gin.SetMode("test")
  16. app := NewApp()
  17. payload := bytes.NewBufferString(body)
  18. req, _ := http.NewRequest(method, path, payload)
  19. if len(cookie) != 0 {
  20. req.Header.Set("Cookie", cookie)
  21. }
  22. w := httptest.NewRecorder()
  23. app.ServeHTTP(w, req)
  24. return w
  25. }
  26. func Test(t *testing.T) {
  27. g := Goblin(t)
  28. g.Describe("App api", func() {
  29. var cookie string
  30. g.It("Should return 302 on / to redirect to file name ", func() {
  31. w := client("GET", "/", "")
  32. g.Assert(w.Code).Equal(302)
  33. cookie = w.HeaderMap.Get(Cookie)
  34. })
  35. g.It("Should return 200 on /slides.md ", func() {
  36. w := client("GET", "/slides.md", cookie)
  37. g.Assert(w.Code).Equal(200)
  38. })
  39. g.It("Should return 200 on PUT /slides.md ", func() {
  40. w := request("PUT", "/slides.md", cookie, "whatever")
  41. g.Assert(w.Code).Equal(200)
  42. })
  43. g.It("Should return list of slides ", func() {
  44. w := client("GET", "/stash", cookie)
  45. g.Assert(w.Code).Equal(200)
  46. })
  47. g.It("Should return specific slide in preview", func() {
  48. w := client("GET", "/published/slides/shy-cell.md", cookie)
  49. g.Assert(w.Code).Equal(200)
  50. })
  51. g.It("Should return specific slide in edit mode", func() {
  52. w := client("GET", "/stash/edit/shy-cell.md", cookie)
  53. g.Assert(w.Code).Equal(200)
  54. })
  55. g.It("Should return specific slide in preview without session", func() {
  56. w := client("GET", "/published/slides/shy-cell.md", "")
  57. g.Assert(w.Code).Equal(200)
  58. })
  59. g.It("Should return specific slide in edit mode without session", func() {
  60. w := client("GET", "/stash/edit/shy-cell.md", "")
  61. g.Assert(w.Code).Equal(200)
  62. })
  63. g.It("Should works")
  64. })
  65. }