| 1 | package callscore |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "net/http/httptest" |
| 6 | "testing" |
| 7 | |
| 8 | "amadan.net/oficina/calls/protocol" |
| 9 | ) |
| 10 | |
| 11 | func TestSetClient(t *testing.T) { |
| 12 | t.Cleanup(func() { client.label = "" }) |
| 13 | for _, c := range []struct { |
| 14 | app, version string |
| 15 | ok bool |
| 16 | }{ |
| 17 | {"mac", "0.4.12", true}, |
| 18 | {"iphone", "0.4.12", true}, |
| 19 | {"ipad", "0.4.12", true}, |
| 20 | {"native", "", false}, |
| 21 | {"mac", "", false}, |
| 22 | {"mac", "0.4.12 (26)", false}, |
| 23 | {"web", "882eab8", false}, |
| 24 | } { |
| 25 | client.label = "" |
| 26 | if got := SetClient(c.app, c.version); got != c.ok { |
| 27 | t.Errorf("SetClient(%q, %q) = %v, want %v", c.app, c.version, got, c.ok) |
| 28 | } |
| 29 | if want := map[bool]string{true: c.app + " " + c.version}[c.ok]; ClientLabel() != want { |
| 30 | t.Errorf("after SetClient(%q, %q): label %q, want %q", c.app, c.version, ClientLabel(), want) |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | // Every request the core makes says which app it is, once set; and says |
| 36 | // nothing before, as an app from before §15.28 does. |
| 37 | func TestRequestsCarryTheClient(t *testing.T) { |
| 38 | t.Cleanup(func() { client.label = "" }) |
| 39 | var got []string |
| 40 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 41 | got = append(got, r.Header.Get(protocol.ClientHeader)) |
| 42 | w.WriteHeader(http.StatusNoContent) |
| 43 | })) |
| 44 | defer srv.Close() |
| 45 | tr, err := newTransport(srv.URL, "abc123", srv.Client()) |
| 46 | if err != nil { |
| 47 | t.Skipf("transport refuses the test origin: %v", err) |
| 48 | } |
| 49 | client.label = "" |
| 50 | if res, err := tr.request(t.Context(), http.MethodGet, "info", nil, nil); err == nil { |
| 51 | res.Body.Close() |
| 52 | } |
| 53 | SetClient("mac", "0.4.12") |
| 54 | if res, err := tr.request(t.Context(), http.MethodGet, "info", nil, nil); err == nil { |
| 55 | res.Body.Close() |
| 56 | } |
| 57 | if len(got) != 2 || got[0] != "" || got[1] != "mac 0.4.12" { |
| 58 | t.Fatalf("headers seen %q, want [\"\" \"mac 0.4.12\"]", got) |
| 59 | } |
| 60 | } |
| 61 | |