48 lines
1.4 KiB
Go
48 lines
1.4 KiB
Go
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 Cloudflare‑protected 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")
|
||
}
|
||
}
|