❯ go-clikit
v0.10.0 pkg.go.dev ↗

packages › local web › localweb/localwebtest

localwebtest

import "github.com/GSI-HPC/go-clikit/localweb/localwebtest"

A client that signs in with the launch token, and a reader of event streams, for a test of a local web server.

since v0.8.0 imports: localweb/session, localweb/sse decisions 34
The place of localweb/localwebtest in go-clikit: the packages it imports, and the packages that import it.termtextprogressdisplaycliprogressprogresstestmcpprogresseventlogfanoutfanouttestclioutdoctorfsutilcobratreecobratreetestlocalwebsessionsselocalwebtestlaunchassetspagehtmx

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)
	}
}
ExampleLogin in localweb/localwebtest/example_test.go. CI compiles it.