| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "io" |
| 6 | "net/http" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestBrowserPrivacyHTTPContract(t *testing.T) { |
| 12 | s := testServer(t) |
| 13 | s.ready = true |
| 14 | calls := 0 |
| 15 | s.browserControl = browserClientForTest(t, s, browserTransport(func(r *http.Request) (*http.Response, error) { |
| 16 | calls++ |
| 17 | if r.Header.Get("Authorization") != "Bearer "+s.credentials.Token { |
| 18 | t.Fatal("guest authentication missing") |
| 19 | } |
| 20 | var input browserInput |
| 21 | if err := json.NewDecoder(r.Body).Decode(&input); err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | switch r.URL.Path { |
| 25 | case "/new": |
| 26 | if !input.Incognito || input.URL != "https://example.test/" { |
| 27 | t.Fatalf("private flag lost: %+v", input) |
| 28 | } |
| 29 | case "/cookies": |
| 30 | if input.ID != "private-tab" || input.Mode != "delete" || input.Scope != "site" || input.Query != "session" || input.Page != 2 || input.CookieKey != strings.Repeat("a", 64) || input.URL != "https://example.test/" { |
| 31 | t.Fatalf("cookie input lost: %+v", input) |
| 32 | } |
| 33 | default: |
| 34 | t.Fatalf("unexpected action: %s", r.URL.Path) |
| 35 | } |
| 36 | return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(`{"ok":true}`)), Header: make(http.Header)}, nil |
| 37 | })) |
| 38 | for _, body := range []struct{ path, input string }{ |
| 39 | {"new", `{"url":"https://example.test/","incognito":true}`}, |
| 40 | {"cookies", `{"id":"private-tab","url":"https://example.test/","mode":"delete","scope":"site","query":"session","page":2,"cookieKey":"` + strings.Repeat("a", 64) + `"}`}, |
| 41 | } { |
| 42 | response := request(s, "POST", "/api/browser/"+body.path, s.origin, body.input, true) |
| 43 | if response.Code != 200 { |
| 44 | t.Fatalf("%s: %d %s", body.path, response.Code, response.Body.String()) |
| 45 | } |
| 46 | } |
| 47 | if calls != 2 { |
| 48 | t.Fatalf("calls = %d", calls) |
| 49 | } |
| 50 | for _, input := range []string{ |
| 51 | `{"mode":"clear","scope":"all"}`, |
| 52 | `{"id":"tab","mode":"clear","scope":"arbitrary-context"}`, |
| 53 | `{"id":"tab","mode":"delete","scope":"all","cookieKey":"bad"}`, |
| 54 | `{"id":"tab","mode":"list","scope":"site","page":-1}`, |
| 55 | } { |
| 56 | response := request(s, "POST", "/api/browser/cookies", s.origin, input, true) |
| 57 | if response.Code != http.StatusBadRequest { |
| 58 | t.Fatalf("invalid input accepted: %s: %d", input, response.Code) |
| 59 | } |
| 60 | } |
| 61 | if calls != 2 { |
| 62 | t.Fatal("invalid request reached guest") |
| 63 | } |
| 64 | response := request(s, "POST", "/api/browser/cookies", s.origin, `{"id":"tab","mode":"list","scope":"all"}`, false) |
| 65 | if response.Code != http.StatusUnauthorized { |
| 66 | t.Fatalf("unauthenticated request: %d", response.Code) |
| 67 | } |
| 68 | s.account.lock() |
| 69 | response = request(s, "POST", "/api/browser/cookies", s.origin, `{"id":"tab","mode":"list","scope":"all"}`, true) |
| 70 | if response.Code != http.StatusLocked { |
| 71 | t.Fatalf("locked request: %d", response.Code) |
| 72 | } |
| 73 | if calls != 2 { |
| 74 | t.Fatal("unauthorized request reached guest") |
| 75 | } |
| 76 | } |