| 1 | package protocol |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | func TestParseClient(t *testing.T) { |
| 6 | for _, c := range []struct { |
| 7 | in, app, version string |
| 8 | ok bool |
| 9 | }{ |
| 10 | {"mac 0.4.12", "mac", "0.4.12", true}, |
| 11 | {"iphone 0.4.12", "iphone", "0.4.12", true}, |
| 12 | {"ipad 1", "ipad", "1", true}, |
| 13 | {"native", "native", "", true}, |
| 14 | {"mac 9999.9999.9999.9999", "mac", "9999.9999.9999.9999", true}, |
| 15 | {"mac 10000.1", "", "", false}, |
| 16 | {"mac 0.4.12.1.2", "", "", false}, |
| 17 | {"mac", "", "", false}, |
| 18 | {"mac ", "", "", false}, |
| 19 | {"mac 0..4", "", "", false}, |
| 20 | {"mac 0.4a", "", "", false}, |
| 21 | {"web 882eab8", "", "", false}, |
| 22 | {"windows 1.0", "", "", false}, |
| 23 | {"Mac 0.4.12", "", "", false}, |
| 24 | {"mac 0.4.12", "", "", false}, |
| 25 | {"mac 0.4.12 extra", "", "", false}, |
| 26 | } { |
| 27 | app, version, ok := ParseClient(c.in) |
| 28 | if app != c.app || version != c.version || ok != c.ok { |
| 29 | t.Errorf("ParseClient(%q) = %q, %q, %v; want %q, %q, %v", c.in, app, version, ok, c.app, c.version, c.ok) |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | func TestCompareVersions(t *testing.T) { |
| 35 | for _, c := range []struct { |
| 36 | a, b string |
| 37 | want int |
| 38 | }{ |
| 39 | {"0.4.2", "0.4.2", 0}, |
| 40 | {"0.4.10", "0.4.2", 1}, |
| 41 | {"0.4.1", "0.4.2", -1}, |
| 42 | {"0.4", "0.4.0", 0}, |
| 43 | {"0.4", "0.4.1", -1}, |
| 44 | {"1.0", "0.99.99", 1}, |
| 45 | {"0.10.0", "0.9.9", 1}, |
| 46 | } { |
| 47 | if got := CompareVersions(c.a, c.b); got != c.want { |
| 48 | t.Errorf("CompareVersions(%q, %q) = %d, want %d", c.a, c.b, got, c.want) |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func TestClientFor(t *testing.T) { |
| 54 | 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" |
| 55 | for _, c := range []struct{ header, ua, want string }{ |
| 56 | {"mac 0.4.12", "Go-http-client/1.1", "mac 0.4.12"}, |
| 57 | {"iphone 0.4.12", "Go-http-client/2.0", "iphone 0.4.12"}, |
| 58 | {"", "Go-http-client/1.1", "native"}, |
| 59 | {"", "Go-http-client/2.0", "native"}, |
| 60 | {"", browser, ""}, |
| 61 | {"", "", ""}, |
| 62 | // A header that is not one: never trusted, and never turned |
| 63 | // into "native" either, because it did say something. |
| 64 | {"mac latest", "Go-http-client/1.1", ""}, |
| 65 | {"native", "Go-http-client/1.1", ""}, |
| 66 | {"mac 0.4.12", browser, "mac 0.4.12"}, |
| 67 | } { |
| 68 | if got := ClientFor(c.header, c.ua); got != c.want { |
| 69 | t.Errorf("ClientFor(%q, %q) = %q, want %q", c.header, c.ua, got, c.want) |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |