-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistener_test.go
137 lines (128 loc) · 4.06 KB
/
listener_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package minigrush
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
"github.com/crbrox/store/dummy"
)
var methods = []string{"GET", "POST", "PUT", "DELETE", "HEAD"}
var data = [][]byte{{1, 2, 3, 4, 0, 5, 6, 7}}
var urls = []string{"http://0.0.0.0:0"}
var targetHosts = []string{"0.0.0.0:0"}
func do(request *http.Request, listener *Listener) *httptest.ResponseRecorder {
response := httptest.NewRecorder()
listener.ServeHTTP(response, request)
return response
}
func doRequest(request *http.Request, t *testing.T) (*httptest.ResponseRecorder, *Listener) {
store := dummy.Store{}
channel := make(chan *Petition, 1000)
listener := &Listener{
SendTo: channel,
PetitionStore: store,
}
response := do(request, listener)
return response, listener
}
func doAny(listener *Listener, t *testing.T) *httptest.ResponseRecorder {
request, err := http.NewRequest("GET", urls[0], bytes.NewReader(data[0]))
request.Header.Set(RelayerHost, targetHosts[0])
if err != nil {
t.Fatal(err)
}
response := do(request, listener)
return response
}
func TestMissingHeader(t *testing.T) {
for _, method := range methods {
request, err := http.NewRequest(method, urls[0], bytes.NewReader(data[0]))
if err != nil {
t.Fatal(err)
}
response, listener := doRequest(request, t)
if response.Code != 400 {
t.Errorf("missing x-relayer-host should return 400 %d", response.Code)
}
if len(listener.SendTo) > 0 {
t.Errorf("invalid request should not be enqueued, method %q len(listener.SendTo) %d", method, len(listener.SendTo))
}
}
}
func TestOK(t *testing.T) {
for _, method := range methods {
request, err := http.NewRequest(method, urls[0], bytes.NewReader(data[0]))
if err != nil {
t.Fatal(err)
}
request.Header.Set(RelayerHost, targetHosts[0])
response, l := doRequest(request, t)
if response.Code != 200 {
t.Errorf("expected status code 200: %d", response.Code)
}
if len(l.SendTo) != 1 {
t.Errorf("valid request should be enqueued, method %q len(l.SendTo) %d", method, len(l.SendTo))
}
id := strings.TrimSpace(response.Body.String())
d, err := l.PetitionStore.Get(id)
if err != nil {
t.Errorf("petition should be stored with returned ID, method %q id %q err %v", method, id, err)
}
pet := &Petition{}
err = json.Unmarshal(d, pet)
if err != nil {
t.Errorf("stored petition should be valid json, method %q id %q err %v", method, id, err)
}
if pet.Method != method {
t.Errorf("petition's method should be equal to request's one, id %q method %q petition's method %q", id, method, pet.Method)
}
if !reflect.DeepEqual(pet.Body, data[0]) {
t.Errorf("petition's body should be equal to request's one, id %q method %q", id, method)
}
if pet.TargetHost != targetHosts[0] {
t.Errorf("petition's target host should be equal to target host, id %q method %q petition's host %q target host %q", id, method, pet.TargetHost, targetHosts[0])
t.Error(string(d))
}
}
}
func TestStop(t *testing.T) {
listener := &Listener{
SendTo: make(chan *Petition, 1000),
PetitionStore: dummy.Store{},
}
listener.Stop()
response := doAny(listener, t)
if response.Code != 503 {
t.Errorf("Listener stopping should return 503 code, not %d", response.Code)
}
}
func TestFullQueue(t *testing.T) {
listener := &Listener{
SendTo: make(chan *Petition), //not buffered, would block with first petition, simulate full queue
PetitionStore: dummy.Store{},
}
response := doAny(listener, t)
if response.Code != 500 {
t.Errorf("Listener with full queue should return 500 code, not %d", response.Code)
}
}
type BadStore struct {
dummy.Store
}
func (bs *BadStore) Put(id string, data []byte) error {
return fmt.Errorf("error caused for testing bad Put")
}
func TestBadStore(t *testing.T) {
listener := &Listener{
SendTo: make(chan *Petition, 10), //not buffered, would block with first petition, simulate full queue
PetitionStore: &BadStore{},
}
response := doAny(listener, t)
if response.Code != 500 {
t.Errorf("Listener with full queue should return 500 code, not %d", response.Code)
}
}