adguard_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package adapters
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/gnacho/netpulse/server-go/internal/config"
  6. )
  7. func TestAdGuardClientEndpoint(t *testing.T) {
  8. tests := []struct {
  9. name string
  10. rawURL string
  11. wantScheme string
  12. wantHost string
  13. wantPort int
  14. }{
  15. {name: "HTTP default", rawURL: "http://adguard.example.com", wantScheme: "http", wantHost: "adguard.example.com", wantPort: 80},
  16. {name: "HTTPS default", rawURL: "https://adguard.example.com", wantScheme: "https", wantHost: "adguard.example.com", wantPort: 443},
  17. {name: "HTTP explicit port", rawURL: "http://adguard.example.com:3000", wantScheme: "http", wantHost: "adguard.example.com", wantPort: 3000},
  18. {name: "HTTPS explicit port", rawURL: "https://adguard.example.com:8443", wantScheme: "https", wantHost: "adguard.example.com", wantPort: 8443},
  19. }
  20. for _, tt := range tests {
  21. t.Run(tt.name, func(t *testing.T) {
  22. client := NewAdGuardClient(tt.rawURL, "admin", "secret")
  23. scheme, host, port := client.endpoint()
  24. if scheme != tt.wantScheme || host != tt.wantHost || port != tt.wantPort {
  25. t.Fatalf("endpoint = %s://%s:%d, want %s://%s:%d", scheme, host, port, tt.wantScheme, tt.wantHost, tt.wantPort)
  26. }
  27. })
  28. }
  29. }
  30. func TestPollAdGuardInactivePreservesEndpoint(t *testing.T) {
  31. live := NewLive(&config.Config{Adguard: &config.AdGuard{
  32. URL: "https://adguard.example.com",
  33. User: "admin",
  34. Pass: "secret",
  35. }}, nil, nil, nil)
  36. ctx, cancel := context.WithCancel(context.Background())
  37. cancel()
  38. stats := live.pollAdGuard(ctx)
  39. if stats == nil {
  40. t.Fatal("inactive stats are nil")
  41. }
  42. if stats.Scheme != "https" || stats.Host != "adguard.example.com" || stats.Port != 443 {
  43. t.Fatalf("inactive endpoint = %s://%s:%d, want https://adguard.example.com:443", stats.Scheme, stats.Host, stats.Port)
  44. }
  45. }