Created
May 20, 2016 18:22
-
-
Save nemosupremo/be66f5b4521fbe48cf775a18783ea7d7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"crypto/tls" | |
"fmt" | |
"golang.org/x/net/http2" | |
"io" | |
"io/ioutil" | |
"log" | |
"time" | |
"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() | |
w.WriteHeader(http.StatusRequestEntityTooLarge) | |
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", 65537) | |
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 < 50; i++ { // racy? | |
log.Printf("client.Do next\n") | |
res, err := client.Do(req) | |
log.Printf("client.Do done\n") | |
if err != nil || res.StatusCode != 413 { | |
scode := 0 | |
if res != nil { | |
scode = res.StatusCode | |
} | |
log.Fatalf("Expected Status Code 413, Got status code: %d, error: %v", scode, 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() | |
time.Sleep(1 * time.Millisecond) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment