github.com/avenga/couper@v1.12.2/server/http_proxy_test.go (about) 1 package server_test 2 3 import ( 4 "bytes" 5 "crypto/rand" 6 "io" 7 "net/http" 8 "testing" 9 "time" 10 11 "github.com/avenga/couper/internal/test" 12 ) 13 14 func TestHTTPProxy_Stream(t *testing.T) { 15 helper := test.New(t) 16 17 shutdown, hook := newCouper("testdata/integration/proxy/01_couper.hcl", helper) 18 defer shutdown() 19 20 randomBytes := make([]byte, 64*1024) // doubled amount of the proxy byte buffer (32k) 21 _, err := rand.Read(randomBytes) 22 helper.Must(err) 23 24 outreq, err := http.NewRequest(http.MethodPost, "http://stream.me:8080/", bytes.NewBuffer(randomBytes)) 25 helper.Must(err) 26 27 client := newClient() 28 time.Sleep(time.Second) 29 for _, e := range hook.AllEntries() { 30 t.Log(e.String()) 31 } 32 33 res, err := client.Do(outreq) 34 helper.Must(err) 35 36 if res.StatusCode != http.StatusOK { 37 t.Errorf("expected status OK, got %d", res.StatusCode) 38 } 39 40 lastRead := time.Now() 41 lastReadAv := time.Duration(0) 42 totalBytes := 0 43 readCount := 0 44 for { 45 lrd := time.Since(lastRead) 46 lastReadAv += lrd 47 bf := make([]byte, 1024) 48 n, rerr := res.Body.Read(bf) 49 totalBytes += n 50 if rerr == io.EOF { 51 break 52 } 53 54 lastRead = time.Now() 55 readCount += 1 56 } 57 helper.Must(res.Body.Close()) 58 59 if totalBytes != 65536 { 60 t.Errorf("expected 64k bytes, got: %d", totalBytes) 61 } 62 63 // lastReadAv is within nanosecond range (<100ns), 64 // should be greater than 0.3 millisecond range while streaming since the backend delays the response chunks. 65 if lastReadAv/time.Duration(readCount) < time.Nanosecond*300 { 66 t.Errorf("expected slower read times with delayed streaming, got an average of: %s", lastReadAv/time.Duration(readCount)) 67 } 68 t.Log(lastReadAv / time.Duration(readCount)) 69 }