4
4
5
5
package elastic
6
6
7
- import "testing"
7
+ import (
8
+ "bytes"
9
+ "strings"
10
+ "testing"
11
+ )
8
12
9
13
var testReq * Request // used as a temporary variable to avoid compiler optimizations in tests/benchmarks
10
14
@@ -25,6 +29,64 @@ func TestRequestSetContentType(t *testing.T) {
25
29
}
26
30
}
27
31
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
+
28
90
func BenchmarkRequestSetBodyString (b * testing.B ) {
29
91
req , err := NewRequest ("GET" , "/" )
30
92
if err != nil {
@@ -128,3 +190,35 @@ func BenchmarkRequestSetBodyMapGzip(b *testing.B) {
128
190
testReq = req
129
191
b .ReportAllocs ()
130
192
}
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