render_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package render_test
  2. import (
  3. "net/http"
  4. "testing"
  5. . "github.com/onsi/ginkgo"
  6. . "github.com/onsi/gomega"
  7. "github.com/vladimirok5959/golang-utils/utils/http/helpers"
  8. "github.com/vladimirok5959/golang-utils/utils/http/render"
  9. )
  10. var _ = Describe("render", func() {
  11. var w *helpers.FakeResponseWriter
  12. var r *http.Request
  13. BeforeEach(func() {
  14. w = helpers.NewFakeResponseWriter()
  15. r = &http.Request{}
  16. })
  17. Context("HTML", func() {
  18. It("render", func() {
  19. var data struct {
  20. URL string
  21. }
  22. data.URL = "/example.html"
  23. Expect(render.HTML(w, r, nil, &data, "Url: {{$.Data.URL}}", http.StatusNotFound)).To(BeTrue())
  24. Expect(w.Body).To(Equal([]byte("Url: /example.html")))
  25. Expect(w.Headers).To(Equal(http.Header{
  26. "Content-Type": []string{"text/html"},
  27. }))
  28. Expect(w.StatusCode).To(Equal(http.StatusNotFound))
  29. })
  30. })
  31. Context("JSON", func() {
  32. It("render", func() {
  33. var data struct{ Field string }
  34. Expect(render.JSON(w, r, data)).To(BeTrue())
  35. Expect(w.Body).To(Equal([]byte(`{"Field":""}`)))
  36. Expect(w.Headers).To(Equal(http.Header{
  37. "Content-Type": []string{"application/json"},
  38. }))
  39. Expect(w.StatusCode).To(Equal(http.StatusOK))
  40. })
  41. })
  42. })
  43. func TestSuite(t *testing.T) {
  44. RegisterFailHandler(Fail)
  45. RunSpecs(t, "render")
  46. }