-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathstring_body_test.go
89 lines (74 loc) · 2.33 KB
/
string_body_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package falcore
import (
"bytes"
"io/ioutil"
"net/http"
"testing"
"time"
//"io"
)
func TestStringBody(t *testing.T) {
expected := []byte("HOT HOT HOT!!!")
tmp, _ := http.NewRequest("POST", "/hello", bytes.NewReader(expected))
tmp.Header.Set("Content-Type", "text/plain")
tmp.ContentLength = int64(len(expected))
req := newRequest(tmp, nil, time.Now())
req.startPipelineStage("StringBodyTest")
sbf := NewStringBodyFilter()
//sbf := &StringBodyFilter{}
sbf.FilterRequest(req)
if sb, ok := req.HttpRequest.Body.(*StringBody); ok {
readin, _ := ioutil.ReadAll(sb)
sb.Close()
if bytes.Compare(readin, expected) != 0 {
t.Errorf("Body string not read %q expected %q", readin, expected)
}
} else {
t.Errorf("Body not replaced with StringBody")
}
if req.CurrentStage.Status != 0 {
t.Errorf("SBF failed to parse POST with status %d", req.CurrentStage.Status)
}
var body []byte = make([]byte, 100)
l, _ := req.HttpRequest.Body.Read(body)
if bytes.Compare(body[0:l], expected) != 0 {
t.Errorf("Failed to read the right bytes %q expected %q", body, expected)
}
l, _ = req.HttpRequest.Body.Read(body)
if l != 0 {
t.Errorf("Should have read zero!")
}
// Close resets the buffer
req.HttpRequest.Body.Close()
l, _ = req.HttpRequest.Body.Read(body)
if bytes.Compare(body[0:l], expected) != 0 {
t.Errorf("Failed to read the right bytes after calling Close %q expected %q", body, expected)
}
}
func BenchmarkStringBody(b *testing.B) {
b.StopTimer()
expected := []byte("test=123456&test2=987654&test3=somedatanstuff&test4=moredataontheend")
expLen := int64(len(expected))
req := newRequest(nil, nil, time.Now())
req.startPipelineStage("StringBodyTest")
sbf := NewStringBodyFilter()
//sbf := &StringBodyFilter{}
for i := 0; i < b.N; i++ {
tmp, _ := http.NewRequest("POST", "/hello", bytes.NewReader(expected))
tmp.Header.Set("Content-Type", "application/x-www-form-urlencoded")
tmp.ContentLength = expLen
req.HttpRequest = tmp
b.StartTimer()
// replace the body
sbf.FilterRequest(req)
sbf.ReturnBuffer(req)
// read the body twice
/* nah, this isn't so useful
io.CopyN(ioutil.Discard, req.HttpRequest.Body, req.HttpRequest.ContentLength)
req.HttpRequest.Body .Close()
io.CopyN(ioutil.Discard, req.HttpRequest.Body, req.HttpRequest.ContentLength)
req.HttpRequest.Body .Close()
*/
b.StopTimer()
}
}