package protocol import "testing" func TestParseClient(t *testing.T) { for _, c := range []struct { in, app, version string ok bool }{ {"mac 0.4.12", "mac", "0.4.12", true}, {"iphone 0.4.12", "iphone", "0.4.12", true}, {"ipad 1", "ipad", "1", true}, {"native", "native", "", true}, {"mac 9999.9999.9999.9999", "mac", "9999.9999.9999.9999", true}, {"mac 10000.1", "", "", false}, {"mac 0.4.12.1.2", "", "", false}, {"mac", "", "", false}, {"mac ", "", "", false}, {"mac 0..4", "", "", false}, {"mac 0.4a", "", "", false}, {"web 882eab8", "", "", false}, {"windows 1.0", "", "", false}, {"Mac 0.4.12", "", "", false}, {"mac 0.4.12", "", "", false}, {"mac 0.4.12 extra", "", "", false}, } { app, version, ok := ParseClient(c.in) if app != c.app || version != c.version || ok != c.ok { t.Errorf("ParseClient(%q) = %q, %q, %v; want %q, %q, %v", c.in, app, version, ok, c.app, c.version, c.ok) } } } func TestCompareVersions(t *testing.T) { for _, c := range []struct { a, b string want int }{ {"0.4.2", "0.4.2", 0}, {"0.4.10", "0.4.2", 1}, {"0.4.1", "0.4.2", -1}, {"0.4", "0.4.0", 0}, {"0.4", "0.4.1", -1}, {"1.0", "0.99.99", 1}, {"0.10.0", "0.9.9", 1}, } { if got := CompareVersions(c.a, c.b); got != c.want { t.Errorf("CompareVersions(%q, %q) = %d, want %d", c.a, c.b, got, c.want) } } } func TestClientFor(t *testing.T) { const browser = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.5 Safari/605.1.15" for _, c := range []struct{ header, ua, want string }{ {"mac 0.4.12", "Go-http-client/1.1", "mac 0.4.12"}, {"iphone 0.4.12", "Go-http-client/2.0", "iphone 0.4.12"}, {"", "Go-http-client/1.1", "native"}, {"", "Go-http-client/2.0", "native"}, {"", browser, ""}, {"", "", ""}, // A header that is not one: never trusted, and never turned // into "native" either, because it did say something. {"mac latest", "Go-http-client/1.1", ""}, {"native", "Go-http-client/1.1", ""}, {"mac 0.4.12", browser, "mac 0.4.12"}, } { if got := ClientFor(c.header, c.ua); got != c.want { t.Errorf("ClientFor(%q, %q) = %q, want %q", c.header, c.ua, got, c.want) } } }