Skip to content

Instantly share code, notes, and snippets.

@nemosupremo
Created May 20, 2016 18:03
Show Gist options
  • Save nemosupremo/fad888179a061a0e3cd80d191a407b9f to your computer and use it in GitHub Desktop.
Save nemosupremo/fad888179a061a0e3cd80d191a407b9f to your computer and use it in GitHub Desktop.
package main
import (
"crypto/tls"
"fmt"
"golang.org/x/net/http2"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"strings"
)
type ChunkyReader struct {
R io.Reader // underlying reader
N int64 // max bytes to read at a time
}
func (l *ChunkyReader) Read(p []byte) (n int, err error) {
if int64(len(p)) > l.N {
p = p[0:l.N]
}
n, err = l.R.Read(p)
//l.N -= int64(n)
return
}
func main() {
ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Body.Close()
log.Printf("Http: %s", r.Proto)
fmt.Fprintln(w, "abc")
}))
http2.ConfigureServer(ts.Config, &http2.Server{})
ts.TLS = ts.Config.TLSConfig
ts.StartTLS()
defer ts.Close()
tr := &http.Transport{TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
}}
http2.ConfigureTransport(tr)
client := &http.Client{
Transport: tr,
}
// if its too small the script will work.
raw := strings.Repeat("A", 256)
log.Printf("len(data)=%v\n", len(raw))
sr := &ChunkyReader{strings.NewReader(raw), 128}
req, err := http.NewRequest("POST", ts.URL, sr)
if err != nil {
log.Fatal(err)
}
for i := 0; i < 2; i++ { // racy?
log.Printf("client.Do next\n")
res, err := client.Do(req)
log.Printf("client.Do done\n")
if err != nil {
log.Fatal(err)
}
n, err := io.Copy(ioutil.Discard, res.Body)
log.Printf("done Reading the server response's body n=(%v) err=%v\nres.headers: %v\n", n, err, res.Header)
_ = res.Body.Close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment