node_probe_body_cap_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package service
  2. import (
  3. "context"
  4. "net/http"
  5. "net/http/httptest"
  6. "net/url"
  7. "strconv"
  8. "strings"
  9. "testing"
  10. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  11. )
  12. // A node answers the probe over a connection the master does not control in the
  13. // skip/pin TLS modes, so an oversized status body must be rejected rather than
  14. // buffered whole by encoding/json.
  15. func TestProbeRejectsOversizedStatusBody(t *testing.T) {
  16. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
  17. w.Header().Set("Content-Type", "application/json")
  18. _, _ = w.Write([]byte(`{"success":true,"obj":{"cpuPct":1,"panelVersion":"`))
  19. pad := strings.Repeat("x", 1<<20)
  20. for i := 0; i < 3; i++ {
  21. _, _ = w.Write([]byte(pad))
  22. }
  23. _, _ = w.Write([]byte(`"}}`))
  24. }))
  25. defer srv.Close()
  26. u, err := url.Parse(srv.URL)
  27. if err != nil {
  28. t.Fatalf("parse url: %v", err)
  29. }
  30. port, err := strconv.Atoi(u.Port())
  31. if err != nil {
  32. t.Fatalf("parse port: %v", err)
  33. }
  34. n := &model.Node{
  35. Id: 1, Name: "big", Scheme: "http", Address: u.Hostname(), Port: port,
  36. BasePath: "/", Enable: true, AllowPrivateAddress: true, TlsVerifyMode: "skip",
  37. }
  38. svc := &NodeService{}
  39. if _, err := svc.Probe(context.Background(), n); err == nil {
  40. t.Fatal("Probe accepted a 3 MiB status body, want an error")
  41. }
  42. }