Skip to content

Commit cbb10cd

Browse files
committed
Experiment with Content-Length
This commit experiments with different encodings for the HTTP request body. See #1526 See #1441 (PR)
1 parent 6b76405 commit cbb10cd

File tree

2 files changed

+98
-2
lines changed

2 files changed

+98
-2
lines changed

request.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ func (r *Request) setBodyReader(body io.Reader) error {
113113
switch v := body.(type) {
114114
case *strings.Reader:
115115
r.ContentLength = int64(v.Len())
116-
case *bytes.Buffer:
116+
// case *bytes.Buffer:
117+
// r.ContentLength = int64(v.Len())
118+
case *bytes.Reader:
117119
r.ContentLength = int64(v.Len())
118120
}
119121
}

request_test.go

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
package elastic
66

7-
import "testing"
7+
import (
8+
"bytes"
9+
"strings"
10+
"testing"
11+
)
812

913
var testReq *Request // used as a temporary variable to avoid compiler optimizations in tests/benchmarks
1014

@@ -25,6 +29,64 @@ func TestRequestSetContentType(t *testing.T) {
2529
}
2630
}
2731

32+
func TestRequestContentLength(t *testing.T) {
33+
tests := []struct {
34+
Body interface{}
35+
Compress bool
36+
ExpectContentLength bool
37+
ContentLength int64
38+
}{
39+
{
40+
Body: `{}`,
41+
Compress: false,
42+
ExpectContentLength: true,
43+
ContentLength: 2,
44+
},
45+
{
46+
Body: strings.NewReader(`{}`),
47+
Compress: false,
48+
ExpectContentLength: true,
49+
ContentLength: 2,
50+
},
51+
{
52+
Body: bytes.NewBuffer([]byte(`{}`)),
53+
Compress: false,
54+
ExpectContentLength: true,
55+
ContentLength: 2,
56+
},
57+
{
58+
Body: bytes.NewReader([]byte(`{}`)),
59+
Compress: false,
60+
ExpectContentLength: true,
61+
ContentLength: 2,
62+
},
63+
}
64+
65+
for i, tt := range tests {
66+
req, err := NewRequest("POST", "/")
67+
if err != nil {
68+
t.Fatal(err)
69+
}
70+
if err := req.SetBody(tt.Body, tt.Compress); err != nil {
71+
t.Fatal(err)
72+
}
73+
if tt.ExpectContentLength {
74+
contentLength := req.ContentLength
75+
if want, have := tt.ContentLength, contentLength; want != have {
76+
t.Fatalf("%d. want Content-Length=%d, have %d", i, want, have)
77+
}
78+
} else {
79+
if want, have := int64(0), req.ContentLength; want != have {
80+
t.Fatalf("%d. want Content-Length=%d, have %d", i, want, have)
81+
}
82+
hdrContentLength := req.Header.Get("Content-Length")
83+
if hdrContentLength != "" {
84+
t.Fatalf("%d. want no Content-Length, have %q", i, hdrContentLength)
85+
}
86+
}
87+
}
88+
}
89+
2890
func BenchmarkRequestSetBodyString(b *testing.B) {
2991
req, err := NewRequest("GET", "/")
3092
if err != nil {
@@ -128,3 +190,35 @@ func BenchmarkRequestSetBodyMapGzip(b *testing.B) {
128190
testReq = req
129191
b.ReportAllocs()
130192
}
193+
194+
func BenchmarkRequestSetBodyStringReader(b *testing.B) {
195+
req, err := NewRequest("GET", "/")
196+
if err != nil {
197+
b.Fatal(err)
198+
}
199+
for i := 0; i < b.N; i++ {
200+
body := strings.NewReader(`{"query":{"match_all":{}}}`)
201+
err = req.SetBody(body, false)
202+
if err != nil {
203+
b.Fatal(err)
204+
}
205+
}
206+
testReq = req
207+
b.ReportAllocs()
208+
}
209+
210+
func BenchmarkRequestSetBodyStringReaderGzip(b *testing.B) {
211+
req, err := NewRequest("GET", "/")
212+
if err != nil {
213+
b.Fatal(err)
214+
}
215+
for i := 0; i < b.N; i++ {
216+
body := strings.NewReader(`{"query":{"match_all":{}}}`)
217+
err = req.SetBody(body, true)
218+
if err != nil {
219+
b.Fatal(err)
220+
}
221+
}
222+
testReq = req
223+
b.ReportAllocs()
224+
}

0 commit comments

Comments
 (0)