panel_egress_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package integration
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "sync/atomic"
  6. "testing"
  7. "time"
  8. "github.com/mhsanaei/3x-ui/v3/internal/util/netproxy"
  9. )
  10. func recordingProxy(t *testing.T, hits *int64) *httptest.Server {
  11. t.Helper()
  12. return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  13. atomic.AddInt64(hits, 1)
  14. w.WriteHeader(http.StatusOK)
  15. _, _ = w.Write([]byte("ok"))
  16. }))
  17. }
  18. func originServer(t *testing.T, hits *int64) *httptest.Server {
  19. t.Helper()
  20. return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  21. atomic.AddInt64(hits, 1)
  22. w.WriteHeader(http.StatusOK)
  23. _, _ = w.Write([]byte("ok"))
  24. }))
  25. }
  26. func TestPanelEgress_NetproxyHelperRoutesThroughProxy(t *testing.T) {
  27. var proxyHits, originHits int64
  28. proxy := recordingProxy(t, &proxyHits)
  29. defer proxy.Close()
  30. origin := originServer(t, &originHits)
  31. defer origin.Close()
  32. client, err := netproxy.NewHTTPClient(proxy.URL, 5*time.Second)
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. resp, err := client.Get(origin.URL)
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. _ = resp.Body.Close()
  41. if atomic.LoadInt64(&proxyHits) != 1 {
  42. t.Fatalf("expected panel proxy to be hit once, got %d (origin hits=%d)", proxyHits, originHits)
  43. }
  44. }