ops_ingress_reject_handler_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package admin
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "testing"
  6. "github.com/Wei-Shaw/sub2api/internal/service"
  7. "github.com/gin-gonic/gin"
  8. "github.com/stretchr/testify/require"
  9. )
  10. func newIngressRejectHandlerForTest() *OpsHandler {
  11. return NewOpsHandler(service.NewOpsService(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil))
  12. }
  13. func TestListIngressRejectsValidatesFilters(t *testing.T) {
  14. gin.SetMode(gin.TestMode)
  15. for _, path := range []string{
  16. "/api/v1/admin/ops/ingress-rejections?reason=not-valid",
  17. "/api/v1/admin/ops/ingress-rejections?client_ip=not-an-ip",
  18. "/api/v1/admin/ops/ingress-rejections?user_id=0",
  19. "/api/v1/admin/ops/ingress-rejections?api_key_id=-1",
  20. } {
  21. t.Run(path, func(t *testing.T) {
  22. context, _ := gin.CreateTestContext(httptest.NewRecorder())
  23. context.Request = httptest.NewRequest(http.MethodGet, path, nil)
  24. newIngressRejectHandlerForTest().ListIngressRejects(context)
  25. require.Equal(t, http.StatusBadRequest, context.Writer.Status())
  26. })
  27. }
  28. }
  29. func TestGetIngressRejectHealthAvailable(t *testing.T) {
  30. recorder := httptest.NewRecorder()
  31. context, _ := gin.CreateTestContext(recorder)
  32. context.Request = httptest.NewRequest(http.MethodGet, "/api/v1/admin/ops/ingress-rejections/health", nil)
  33. newIngressRejectHandlerForTest().GetIngressRejectHealth(context)
  34. require.Equal(t, http.StatusOK, recorder.Code)
  35. require.Contains(t, recorder.Body.String(), `"capacity":8192`)
  36. }