Files
findos/internal/clouddetect/detect_test.go
Dev 4d51c65060
Some checks failed
Go CI / test (push) Has been cancelled
up
2025-09-13 12:30:01 +03:00

48 lines
1.4 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package clouddetect
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
)
// TestDetect_HeaderBasedDetection verifies that Detect can identify a Cloudflare proxy
// by inspecting the Server header returned from an HTTP HEAD request.
func TestDetect_HeaderBasedDetection(t *testing.T) {
// Create a test server that mimics a Cloudflareprotected endpoint.
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Server", "cloudflare")
w.WriteHeader(http.StatusOK)
}))
defer ts.Close()
// Extract host without scheme (e.g., "127.0.0.1:XXXXX").
host := strings.TrimPrefix(ts.URL, "http://")
// Run detection against the test server.
res, err := Detect(host)
if err != nil {
t.Fatalf("Detect returned error: %v", err)
}
if !res.IsProxy {
t.Fatalf("Expected proxy detection, got IsProxy=false")
}
if res.Provider != "Cloudflare" {
t.Fatalf("Expected provider Cloudflare, got %s", res.Provider)
}
}
// TestDetect_NoProxy ensures that a target without known CIDR or header signatures
// is reported as not being behind a proxy.
func TestDetect_NoProxy(t *testing.T) {
// Use localhost where no special headers are set.
res, err := Detect("localhost")
if err != nil {
t.Fatalf("Detect returned error: %v", err)
}
if res.IsProxy {
t.Fatalf("Expected no proxy detection, got IsProxy=true")
}
}