up
Some checks failed
Go CI / test (push) Has been cancelled

This commit is contained in:
Dev
2025-09-13 12:30:01 +03:00
commit 4d51c65060
14 changed files with 1227 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
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")
}
}