package server import ( "context" "fmt" "net/http" "net/http/httptest" "strings" "testing" ) // A page as the model wants it: title, the main text with headings and // lists, no scripts, styles or menus. func TestPageText(t *testing.T) { page := `Go 1.24 & more

Go 1.24

Generic type aliases are here.

` title, text := pageText(page) if title != "Go 1.24 & more" { t.Fatalf("title %q", title) } for _, want := range []string{"# Go 1.24", "Generic type aliases are here.", "- Swiss tables", "- weak pointers"} { if !strings.Contains(text, want) { t.Fatalf("no %q in:\n%s", want, text) } } for _, not := range []string{"track()", "color:red", "Home", "© Google"} { if strings.Contains(text, not) { t.Fatalf("%q left in:\n%s", not, text) } } } // The server's own network is out of reach: no loopback, private or local // addresses, whatever the name says. func TestFetchRefusesPrivate(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "secret") })) defer srv.Close() for _, u := range []string{srv.URL, "http://localhost:1/", "http://10.0.0.1/", "http://169.254.169.254/latest/meta-data/", "file:///etc/passwd", "ftp://x/"} { if _, err := fetchPage(context.Background(), u); err == nil { t.Errorf("%s: fetched", u) } } } func TestFetchPage(t *testing.T) { old := fetchAllowPrivate fetchAllowPrivate = true defer func() { fetchAllowPrivate = old }() srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/json" { w.Header().Set("Content-Type", "application/json") fmt.Fprint(w, `{"a":1}`) return } w.Header().Set("Content-Type", "text/html; charset=utf-8") fmt.Fprint(w, "T

"+strings.Repeat("слово ", 5000)+"

") })) defer srv.Close() p, err := fetchPage(context.Background(), srv.URL+"/page") if err != nil || p.Title != "T" || !p.Truncated || len([]rune(p.Text)) > fetchMaxChars+10 { t.Fatalf("page: %v %q %v %d", err, p.Title, p.Truncated, len(p.Text)) } p, err = fetchPage(context.Background(), srv.URL+"/json") if err != nil || p.Text != `{"a":1}` { t.Fatalf("json: %+v %v", p, err) } } // Lines broken in the page's source are one paragraph; code keeps its lines. func TestPageTextJoinsWrapped(t *testing.T) { _, text := pageText("

The latest Go release,\n version 1.24,\narrives in February.

a\nb
") if !strings.Contains(text, "The latest Go release, version 1.24, arrives in February.") || !strings.Contains(text, "a\nb") { t.Fatalf("%q", text) } }