package busweb import ( "context" "io" "net/http" "net/http/httptest" "strings" "testing" layout "teruna.dev/yetano/yetano-webui-template" "yetano.dev/yetano/verse/web" ) func echo() http.Handler { mux := http.NewServeMux() mux.HandleFunc("GET /hello", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/plain") w.Header().Set("Server", "secret") w.Header().Set("Set-Cookie", "a=b") io.WriteString(w, "prefix="+Prefix(r)+" user="+User(r)+" path="+r.URL.Path+" q="+r.URL.Query().Get("q")+" ua="+r.UserAgent()) }) mux.HandleFunc("POST /form", func(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, Prefix(r)+"/hello?q="+r.FormValue("v"), http.StatusSeeOther) }) mux.HandleFunc("GET /big", func(w http.ResponseWriter, r *http.Request) { w.Write(make([]byte, web.MaxResponseBody+10)) }) mux.HandleFunc("GET /appearance-back", func(w http.ResponseWriter, r *http.Request) { h := layout.PageHeader{BasePath: Prefix(r)} h.ApplyAppearance(r) io.WriteString(w, h.AppearanceBack) }) return mux } func TestServe(t *testing.T) { resp := Serve(context.Background(), echo(), web.Request{Method: "GET", URL: "/hello?q=1", Prefix: "/apps/x", User: "alice", Header: map[string][]string{"User-Agent": {"t"}}}) if resp.Status != 200 || string(resp.Body) != "prefix=/apps/x user=alice path=/hello q=1 ua=t" { t.Fatalf("%d %q", resp.Status, resp.Body) } if resp.Header["Server"] != nil || resp.Header["Set-Cookie"][0] != "a=b" { t.Fatalf("headers %v", resp.Header) } if err := resp.Validate(); err != nil { t.Fatal(err) } } func TestServePostRedirect(t *testing.T) { resp := Serve(context.Background(), echo(), web.Request{Method: "POST", URL: "/form", Prefix: "/p", Header: map[string][]string{"Content-Type": {"application/x-www-form-urlencoded"}}, Body: []byte("v=7")}) if resp.Status != http.StatusSeeOther || resp.Header["Location"][0] != "/p/hello?q=7" { t.Fatalf("%d %v", resp.Status, resp.Header) } } func TestServeTruncatesAndRejects(t *testing.T) { resp := Serve(context.Background(), echo(), web.Request{Method: "GET", URL: "/big"}) if !resp.Truncated || len(resp.Body) != web.MaxResponseBody { t.Fatalf("truncated %v len %d", resp.Truncated, len(resp.Body)) } if resp := Serve(context.Background(), echo(), web.Request{Method: "PUT", URL: "/hello"}); resp.Status != 400 { t.Fatalf("PUT: %d", resp.Status) } if resp := Serve(context.Background(), echo(), web.Request{Method: "HEAD", URL: "/hello"}); resp.Status != 405 && len(resp.Body) != 0 { t.Fatalf("HEAD body: %q", resp.Body) } } // The layout's appearance menu returns to the page below the prefix, not // to the prefix stripped twice. func TestServeAppearanceBack(t *testing.T) { resp := Serve(context.Background(), echo(), web.Request{Method: "GET", URL: "/appearance-back?x=1", Prefix: "/appearance-back"}) if string(resp.Body) != "/appearance-back?x=1" { t.Fatalf("%q", resp.Body) } } func TestWithPrefix(t *testing.T) { srv := httptest.NewServer(WithPrefix(echo(), "/ui/")) defer srv.Close() res, err := http.Get(srv.URL + "/ui/hello?q=2") if err != nil { t.Fatal(err) } b, _ := io.ReadAll(res.Body) res.Body.Close() if !strings.HasPrefix(string(b), "prefix=/ui user= path=/hello q=2") { t.Fatalf("%q", b) } if res, _ := http.Get(srv.URL + "/hello"); res.StatusCode != 404 { t.Fatalf("outside prefix: %d", res.StatusCode) } root := httptest.NewServer(WithPrefix(echo(), "")) defer root.Close() res, _ = http.Get(root.URL + "/hello") b, _ = io.ReadAll(res.Body) res.Body.Close() if !strings.HasPrefix(string(b), "prefix= user= path=/hello") { t.Fatalf("root: %q", b) } }