handler_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package health
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. "github.com/imgproxy/imgproxy/v3/httpheaders"
  9. "github.com/imgproxy/imgproxy/v3/server/responsewriter"
  10. )
  11. func TestHealthHandler(t *testing.T) {
  12. // Create responsewriter.Factory
  13. rwConf := responsewriter.NewDefaultConfig()
  14. rwf, err := responsewriter.NewFactory(&rwConf)
  15. require.NoError(t, err)
  16. // Create a ResponseRecorder to record the response
  17. rr := httptest.NewRecorder()
  18. // Create a new health handler
  19. h := New()
  20. // Call the handler function directly (no need for actual HTTP request)
  21. h.Execute("test-req-id", rwf.NewWriter(rr), nil)
  22. // Check that we get a valid response (either 200 or 500 depending on vips state)
  23. assert.True(t, rr.Code == http.StatusOK || rr.Code == http.StatusInternalServerError)
  24. // Check headers are set correctly
  25. assert.Equal(t, "text/plain", rr.Header().Get(httpheaders.ContentType))
  26. assert.Equal(t, "no-cache", rr.Header().Get(httpheaders.CacheControl))
  27. // Verify response format and content
  28. body := rr.Body.String()
  29. assert.NotEmpty(t, body)
  30. assert.Equal(t, "imgproxy is running", body)
  31. }