forked from sohlich/nats-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_test.go
75 lines (65 loc) · 1.38 KB
/
request_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
package natsproxy
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"testing"
"github.com/gogo/protobuf/proto"
)
func TestUnmarshallFrom(t *testing.T) {
URL := "/api/method"
original := &Request{
URL: URL,
Body: []byte{0xFF, 0xFC},
}
payload, _ := proto.Marshal(original)
copyObj := &Request{}
if err := copyObj.UnmarshallFrom(payload); err != nil {
t.Error(err)
}
if original.URL != copyObj.URL {
fmt.Println()
t.Error("URL not equals")
}
// TODO more precise equality test
if len(original.Body) != len(copyObj.Body) {
t.Error("Body not equals")
}
}
func TestNewRequestFromHttp(t *testing.T) {
url, _ := url.Parse("http://test.com/test")
httpReq := &http.Request{
Method: "GET",
URL: url,
Body: ioutil.NopCloser(bytes.NewReader([]byte{0xFF, 0xFC})),
}
req := NewRequest()
err := req.FromHTTP(httpReq)
if err != nil {
t.Error(err)
}
if req.URL != "http://test.com/test" {
t.Error("Url not equals")
}
// TODO better test for Body
if len(req.Body) != 2 {
t.Error("Body length not equals")
}
}
func BenchmarkMarshallRequest(b *testing.B) {
b.StopTimer()
url, _ := url.Parse("http://test.com/test")
httpReq := &http.Request{
Method: "GET",
URL: url,
Body: ioutil.NopCloser(bytes.NewReader([]byte{0xFF, 0xFC})),
}
req := NewRequest()
req.FromHTTP(httpReq)
b.StartTimer()
for i := 0; i < b.N; i++ {
proto.Marshal(req)
}
}