A test of a local web server needs a browser that signed in, and that sends
its requests as a page of the server’s own origin does.
localwebtest.Login gives it a
localwebtest.Client, and
localwebtest.Reader reads the events of a stream.
srv := httptest.NewServer(handler)
c := localwebtest.Login(t, srv.URL, token)
c.Header.Set("X-Prog-CSRF", c.CSRF(t, "/"))
resp := c.PostForm(t, "/actions/stop", url.Values{"id": {"1"}})
NOTE
A server behind localweb.Hosts answers only to the address it bound. Serve
the handler through httptest.NewServer, whose URL is 127.0.0.1 and its
port, and give the Hosts check that Host.
_ = func(t *testing.T, handler http.Handler, token string) {
srv := httptest.NewServer(handler)
defer srv.Close()
c := localwebtest.Login(t, srv.URL, token)
c.Header.Set("X-Prog-CSRF", c.CSRF(t, "/"))
resp := c.PostForm(t, "/runs/1/stop", url.Values{"signal": {"INT"}})
_ = resp.Body.Close()
if resp.StatusCode != http.StatusSeeOther {
t.Errorf("stop: %s, want 303 See Other", resp.Status)
}
}localweb/localwebtest/example_test.go. CI compiles it.