//go:build ignore package main import ( "context" "fmt" "net/http" "net/http/httptest" "time" ) func main() { s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { time.Sleep(200 * time.Millisecond) fmt.Fprintln(w, "ok") })) defer s.Close() ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond) defer cancel() req, _ := http.NewRequestWithContext(ctx, "GET", s.URL, nil) _, err := http.DefaultClient.Do(req) fmt.Println("expected timeout:", err != nil) }