node_probe_unsampled_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. // An older node answers success with a null obj while its status is unsampled,
  13. // which used to surface as "success=false: " with nothing after the colon.
  14. func TestProbeNamesANodeThatHasNoStatusYet(t *testing.T) {
  15. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
  16. w.Header().Set("Content-Type", "application/json")
  17. _, _ = w.Write([]byte(`{"success":true,"msg":"","obj":null}`))
  18. }))
  19. defer srv.Close()
  20. u, err := url.Parse(srv.URL)
  21. if err != nil {
  22. t.Fatalf("parse url: %v", err)
  23. }
  24. port, err := strconv.Atoi(u.Port())
  25. if err != nil {
  26. t.Fatalf("parse port: %v", err)
  27. }
  28. n := &model.Node{
  29. Id: 1, Name: "cold", Scheme: "http", Address: u.Hostname(), Port: port,
  30. BasePath: "/", Enable: true, AllowPrivateAddress: true, TlsVerifyMode: "skip",
  31. }
  32. svc := &NodeService{}
  33. patch, err := svc.Probe(context.Background(), n)
  34. if err == nil {
  35. t.Fatal("Probe accepted a status response with no obj, want an error")
  36. }
  37. if strings.Contains(patch.LastError, "success=false") {
  38. t.Fatalf("LastError = %q, want the missing status named instead of a bare success=false", patch.LastError)
  39. }
  40. if !strings.Contains(patch.LastError, "no status yet") {
  41. t.Fatalf("LastError = %q, want it to say the remote reported no status yet", patch.LastError)
  42. }
  43. }