Skip to content

Instantly share code, notes, and snippets.

@euroelessar
Created August 25, 2018 07:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save euroelessar/6f4535db11b8bf024d85253cdaa1db1f to your computer and use it in GitHub Desktop.
Save euroelessar/6f4535db11b8bf024d85253cdaa1db1f to your computer and use it in GitHub Desktop.
package main
import (
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"time"
"golang.org/x/net/http2"
)
type errReader struct {
err error
}
func (r *errReader) Read(p []byte) (int, error) {
return 0, r.err
}
func main() {
flag.Parse()
handler := func(resp http.ResponseWriter, req *http.Request) {
io.Copy(ioutil.Discard, req.Body)
req.Body.Close()
resp.WriteHeader(200)
}
ts := httptest.NewUnstartedServer(http.HandlerFunc(handler))
err := http2.ConfigureServer(ts.Config, &http2.Server{
MaxConcurrentStreams: 10,
})
if err != nil {
panic(err)
}
ts.TLS = ts.Config.TLSConfig
ts.StartTLS()
client := ts.Client()
err = http2.ConfigureTransport(client.Transport.(*http.Transport))
if err != nil {
panic(err)
}
client.Timeout = time.Second
_, err = client.Get(ts.URL)
if err != nil {
panic(fmt.Sprintf("failed to perform get, err: %v", err))
}
brokenBody := &errReader{errors.New("read error")}
for i := 0; i < 10; i++ {
log.Printf("iteration: %v", i)
_, err = client.Post(ts.URL, "", brokenBody)
if err == nil {
panic(fmt.Sprintf("http request was expected to fail"))
}
}
_, err = client.Get(ts.URL)
if err != nil {
panic(fmt.Sprintf("failed to perform get, err: %v", err))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment