server_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package server_test
  2. import (
  3. "context"
  4. "net/http"
  5. "net/http/httptest"
  6. "testing"
  7. . "github.com/onsi/ginkgo"
  8. . "github.com/onsi/gomega"
  9. "github.com/vladimirok5959/golang-ip2location/internal/server"
  10. )
  11. var _ = Describe("Server", func() {
  12. Context("Endpoint", func() {
  13. var ctx = context.Background()
  14. var srv *httptest.Server
  15. var client *http.Client
  16. AfterEach(func() {
  17. srv.Close()
  18. })
  19. Context("Routes", func() {
  20. BeforeEach(func() {
  21. mux := server.NewMux(ctx, nil, nil)
  22. srv = httptest.NewServer(mux)
  23. client = srv.Client()
  24. })
  25. AfterEach(func() {
  26. srv.Close()
  27. })
  28. Context("Route", func() {
  29. It("must exists", func() {
  30. var routes = []string{
  31. // Pages
  32. "/",
  33. // API
  34. "/api/v1/app/health",
  35. "/api/v1/app/status",
  36. "/api/v1/ip2location/127.0.0.1",
  37. // Assets
  38. "/styles.css",
  39. }
  40. for _, route := range routes {
  41. resp, err := client.Get(srv.URL + route)
  42. resp.Body.Close()
  43. Expect(err).To(Succeed())
  44. Expect(resp.StatusCode).NotTo(Equal(http.StatusNotFound))
  45. }
  46. })
  47. It("must response with 404", func() {
  48. resp, err := client.Get(srv.URL + "/qwertyuiopasdfghjklzxcvbnm")
  49. resp.Body.Close()
  50. Expect(err).To(Succeed())
  51. Expect(resp.StatusCode).To(Equal(http.StatusNotFound))
  52. })
  53. })
  54. })
  55. })
  56. })
  57. func TestSuite(t *testing.T) {
  58. RegisterFailHandler(Fail)
  59. RunSpecs(t, "Server")
  60. }