1
0

suite.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package integration
  2. import (
  3. "context"
  4. "fmt"
  5. "net"
  6. "net/http"
  7. "github.com/imgproxy/imgproxy/v3"
  8. "github.com/imgproxy/imgproxy/v3/httpheaders"
  9. "github.com/imgproxy/imgproxy/v3/logger"
  10. "github.com/imgproxy/imgproxy/v3/testutil"
  11. )
  12. type TestServer struct {
  13. Addr net.Addr
  14. Shutdown context.CancelFunc
  15. }
  16. // Suite is a test suite for integration tests.
  17. //
  18. // It lazily initializes [imgproxy.Config] and [imgproxy.Imgproxy] when they are accessed.
  19. //
  20. // It provides the [Suite.GET] method that lazily initializes a test imgproxy server
  21. // and performs a GET request against it.
  22. //
  23. // Take note that Suite utilizes SetupSuite and TearDownSuite for setup and cleanup.
  24. // If you define them for your test suite, make sure to call the base methods.
  25. type Suite struct {
  26. testutil.LazySuite
  27. TestData *testutil.TestDataProvider
  28. Config testutil.LazyObj[*imgproxy.Config]
  29. Imgproxy testutil.LazyObj[*imgproxy.Imgproxy]
  30. Server testutil.LazyObj[*TestServer]
  31. }
  32. func (s *Suite) SetupSuite() {
  33. // Silence all the logs
  34. logger.Mute()
  35. // Initialize test data provider (local test files)
  36. s.TestData = testutil.NewTestDataProvider(s.T)
  37. s.Config, _ = testutil.NewLazySuiteObj(s, func() (*imgproxy.Config, error) {
  38. // TODO: replace with NewDefaultConfig when we get rid of global config
  39. c, err := imgproxy.LoadConfigFromEnv(nil)
  40. s.Require().NoError(err)
  41. c.Server.Bind = ":0"
  42. c.Fetcher.Transport.Local.Root = s.TestData.Root()
  43. c.Fetcher.Transport.HTTP.ClientKeepAliveTimeout = 0
  44. return c, nil
  45. })
  46. s.Imgproxy, _ = testutil.NewLazySuiteObj(s, func() (*imgproxy.Imgproxy, error) {
  47. return imgproxy.New(s.T().Context(), s.Config())
  48. })
  49. s.Server, _ = testutil.NewLazySuiteObj(
  50. s,
  51. func() (*TestServer, error) {
  52. return s.startServer(s.Imgproxy()), nil
  53. },
  54. func(s *TestServer) error {
  55. s.Shutdown()
  56. return nil
  57. },
  58. )
  59. }
  60. func (s *Suite) TearDownSuite() {
  61. logger.Unmute()
  62. }
  63. // startServer starts imgproxy instance's server for the tests.
  64. // Returns [TestServer] that contains the server address and shutdown function
  65. func (s *Suite) startServer(i *imgproxy.Imgproxy) *TestServer {
  66. ctx, cancel := context.WithCancel(s.T().Context())
  67. addrCh := make(chan net.Addr)
  68. go func() {
  69. err := i.StartServer(ctx, addrCh)
  70. if err != nil {
  71. s.T().Errorf("Imgproxy stopped with error: %v", err)
  72. }
  73. }()
  74. return &TestServer{
  75. Addr: <-addrCh,
  76. Shutdown: cancel,
  77. }
  78. }
  79. // GET performs a GET request to the imageproxy real server
  80. func (s *Suite) GET(path string, header ...http.Header) *http.Response {
  81. url := fmt.Sprintf("http://%s%s", s.Server().Addr, path)
  82. // Perform GET request to an url
  83. req, err := http.NewRequest("GET", url, nil)
  84. s.Require().NoError(err)
  85. // Copy headers from the provided http.Header to the request
  86. for _, h := range header {
  87. httpheaders.CopyAll(h, req.Header, true)
  88. }
  89. // Do the request
  90. resp, err := http.DefaultClient.Do(req)
  91. s.Require().NoError(err)
  92. return resp
  93. }