logger_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package logger_test
  2. import (
  3. "bytes"
  4. "fmt"
  5. "log"
  6. "net/http"
  7. "net/http/httptest"
  8. "testing"
  9. . "github.com/onsi/ginkgo"
  10. . "github.com/onsi/gomega"
  11. "github.com/vladimirok5959/golang-utils/utils/http/logger"
  12. )
  13. var _ = Describe("logger", func() {
  14. Context("LogRequests", func() {
  15. var srv *httptest.Server
  16. var client *http.Client
  17. var getTestHandler = func() http.HandlerFunc {
  18. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  19. if _, err := w.Write([]byte("Index")); err != nil {
  20. fmt.Printf("%s\n", err.Error())
  21. }
  22. })
  23. }
  24. BeforeEach(func() {
  25. srv = httptest.NewServer(logger.LogRequests(getTestHandler()))
  26. client = srv.Client()
  27. })
  28. AfterEach(func() {
  29. srv.Close()
  30. })
  31. It("log http requests", func() {
  32. buf := new(bytes.Buffer)
  33. log.SetOutput(buf)
  34. resp, err := client.Get(srv.URL + "/")
  35. Expect(err).To(Succeed())
  36. defer resp.Body.Close()
  37. Expect(resp.StatusCode).To(Equal(http.StatusOK))
  38. Expect(buf.String()).To(ContainSubstring(`"GET /" 200`))
  39. Expect(buf.String()).To(ContainSubstring(`Go-http-client`))
  40. })
  41. })
  42. })
  43. func TestSuite(t *testing.T) {
  44. RegisterFailHandler(Fail)
  45. RunSpecs(t, "logger")
  46. }