| 1 | package home |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "net/http/httptest" |
| 6 | "net/url" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "amadan.net/rastrillo/rastrillo" |
| 11 | ) |
| 12 | |
| 13 | // frameworkRoutes reports what rastrillo's own layer added to the handler |
| 14 | // Serve would run, on host: a rastrillo_scheme cookie from POST /_theme, |
| 15 | // the speculation ruleset at GET /_speculation-rules, and a |
| 16 | // Speculation-Rules header on an ordinary page. |
| 17 | func frameworkRoutes(t *testing.T, h http.Handler, host string) []string { |
| 18 | t.Helper() |
| 19 | var found []string |
| 20 | |
| 21 | form := url.Values{"scheme": {"dark"}} |
| 22 | req := httptest.NewRequest(http.MethodPost, "/_theme", strings.NewReader(form.Encode())) |
| 23 | req.Host = host |
| 24 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 25 | req.Header.Set("Origin", "https://"+host) |
| 26 | req.Header.Set("Sec-Fetch-Site", "same-origin") |
| 27 | rec := httptest.NewRecorder() |
| 28 | h.ServeHTTP(rec, req) |
| 29 | for _, c := range rec.Result().Cookies() { |
| 30 | if c.Name == rastrillo.SchemeCookie { |
| 31 | found = append(found, "POST /_theme set "+c.Name) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | req = httptest.NewRequest(http.MethodGet, rastrillo.SpeculationRulesPath, nil) |
| 36 | req.Host = host |
| 37 | rec = httptest.NewRecorder() |
| 38 | h.ServeHTTP(rec, req) |
| 39 | if strings.Contains(rec.Header().Get("Content-Type"), "speculationrules") { |
| 40 | found = append(found, "GET "+rastrillo.SpeculationRulesPath+" served the ruleset") |
| 41 | } |
| 42 | |
| 43 | req = httptest.NewRequest(http.MethodGet, "/", nil) |
| 44 | req.Host = host |
| 45 | req.Header.Set("Accept", "text/html") |
| 46 | rec = httptest.NewRecorder() |
| 47 | h.ServeHTTP(rec, req) |
| 48 | if v := rec.Header().Get("Speculation-Rules"); v != "" { |
| 49 | found = append(found, "Speculation-Rules header "+v) |
| 50 | } |
| 51 | return found |
| 52 | } |
| 53 | |
| 54 | // TestServeAddsNoFrameworkRoutes builds the handler rastrillo.Serve runs |
| 55 | // (rastrillo.Handler over ServeOptions, exactly what cmd/home passes) and |
| 56 | // proves that, on both origins, none of v0.38.0's framework endpoints is |
| 57 | // there: no POST /_theme and its non-__Host- rastrillo_scheme cookie, no |
| 58 | // speculation ruleset, no Speculation-Rules header. Home's router tests |
| 59 | // cannot see this layer, which is why it is tested here. The control |
| 60 | // half turns the two options back off and expects every one of them, so |
| 61 | // a probe that has gone blind fails rather than passes. |
| 62 | func TestServeAddsNoFrameworkRoutes(t *testing.T) { |
| 63 | app := newTestApp(t) |
| 64 | opts := app.ServeOptions(rastrillo.Options{Logger: quietLogger()}) |
| 65 | if !opts.NoSchemeSwitch || !opts.NoSpeculationRules { |
| 66 | t.Fatalf("ServeOptions: NoSchemeSwitch=%v NoSpeculationRules=%v, want both true", opts.NoSchemeSwitch, opts.NoSpeculationRules) |
| 67 | } |
| 68 | h, closeFn, err := rastrillo.Handler(opts) |
| 69 | if err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | t.Cleanup(func() { _ = closeFn() }) |
| 73 | for _, host := range []string{credentialHost, directoryHost} { |
| 74 | if found := frameworkRoutes(t, h, host); len(found) != 0 { |
| 75 | t.Errorf("%s: rastrillo's layer adds %v", host, found) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | control := opts |
| 80 | control.NoSchemeSwitch, control.NoSpeculationRules = false, false |
| 81 | ch, closeControl, err := rastrillo.Handler(control) |
| 82 | if err != nil { |
| 83 | t.Fatal(err) |
| 84 | } |
| 85 | t.Cleanup(func() { _ = closeControl() }) |
| 86 | for _, host := range []string{credentialHost, directoryHost} { |
| 87 | if found := frameworkRoutes(t, ch, host); len(found) != 3 { |
| 88 | t.Errorf("%s: control saw %v, want all three framework routes (the probe is blind)", host, found) |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |