package fetch import ( "compress/gzip" "context" "errors" "net/http" "net/http/httptest" "net/url" "os" "path/filepath" "runtime" "strings" "testing" "time" fhttp "github.com/bogdanfinn/fhttp" ) func TestSecCHUA(t *testing.T) { // Real values, from tls-client's examples. for major, want := range map[int]string{ 105: `"Google Chrome";v="105", "Not)A;Brand";v="8", "Chromium";v="105"`, 107: `"Google Chrome";v="107", "Chromium";v="107", "Not=A?Brand";v="24"`, } { if got := secCHUA(major); got != want { t.Errorf("secCHUA(%d) = %s, want %s", major, got, want) } } } func TestAcceptLanguage(t *testing.T) { for host, want := range map[string]string{ "www.amazon.de": "de-DE,", "www.Amazon.co.uk": "en-GB,", "www.amazon.fr": "fr-FR,", "www.amazon.it": "it-IT,", "www.amazon.es": "es-ES,", "www.amazon.nl": "nl-NL,", "www.amazon.com": "en-US,", "www.amazon.co.jp": "en-US,", "127.0.0.1": "en-US,", } { if got := acceptLanguage(host); !strings.HasPrefix(got, want) { t.Errorf("acceptLanguage(%q) = %q, want prefix %q", host, got, want) } } } func TestDefaultBrowserMatchesProfile(t *testing.T) { b := DefaultBrowser() if !strings.Contains(b.UserAgent, "Chrome/152.0.0.0") { t.Errorf("user agent %q does not match Chrome %d", b.UserAgent, b.Major) } want := map[string]string{"windows": "Windows", "darwin": "macOS", "linux": "Linux"}[runtime.GOOS] if want != "" && b.Platform != want { t.Errorf("platform = %q, want %q", b.Platform, want) } } func TestClassify(t *testing.T) { cf := http.Header{"Cf-Mitigated": {"challenge"}} for name, tc := range map[string]struct { status int header http.Header finalURL string body string verdict Verdict challenge string }{ "product page": {200, nil, "https://www.amazon.de/dp/X", "Metro", OK, ""}, "cf script on normal page": {200, nil, "", `", Challenge, "cloudflare"}, "cf title": {403, nil, "", "Just a moment...", Challenge, "cloudflare"}, "amazon captcha 200": {200, nil, "", `
`, Challenge, "amazon-captcha"}, "amazon captcha url": {200, nil, "https://www.amazon.de/errors/validateCaptcha?x=1", "", Challenge, "amazon-captcha"}, "datadome": {403, nil, "", `