-
Notifications
You must be signed in to change notification settings - Fork 1
/
consumer_test.go
executable file
·181 lines (164 loc) · 4.71 KB
/
consumer_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// consumer_test.go
package gridas
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"testing"
"time"
"labix.org/v2/mgo/bson"
)
const idPetition = "1"
var testBody = []byte("sent content to target host")
var testBodyResponse = []byte("Hello, client (from target host)\n")
func TestConsumer(t *testing.T) {
setUp(t)
defer tearDown(t)
reqChan := make(chan *Petition, cfgTest.QueueSize)
resultCh := make(chan *http.Request, 1)
errCh := make(chan error, 1)
consumer := newTestConsumer(reqChan)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Log("received ", r)
fmt.Fprint(w, string(testBodyResponse))
rcvdBody, err := ioutil.ReadAll(r.Body)
if err != nil {
errCh <- err
}
if !reflect.DeepEqual(rcvdBody, testBody) {
errCh <- fmt.Errorf("received body in target host is not equal to sent body")
}
resultCh <- r
}))
defer ts.Close()
u, err := url.Parse(ts.URL)
if err != nil {
t.Fatal(err)
}
u.Path = "/x/y/z"
u.RawQuery = "q=a&r=b"
petition := &Petition{
ID: idPetition,
TargetHost: u.Host,
TargetScheme: u.Scheme,
Method: "GET",
URL: u,
Proto: "HTTP/1.1",
Body: testBody,
RemoteAddr: "127.0.0.1",
Host: u.Host,
Created: time.Now(),
}
endCh := consumer.Start(2)
reqChan <- petition
var rcvRequest *http.Request
select {
case rcvRequest = <-resultCh:
case e := <-errCh:
t.Fatal(e)
case <-time.After(5 * time.Second):
t.Fatal("target server waiting too long")
}
if rcvRequest.URL.Path != petition.URL.Path {
t.Errorf("received url path is not equal to sent url path %q %q", rcvRequest.URL, petition.URL)
}
if rcvRequest.URL.RawQuery != petition.URL.RawQuery {
t.Errorf("received query is not equal to sent query %q %q", rcvRequest.URL, petition.URL)
}
if rcvRequest.Method != petition.Method {
t.Errorf("received method is not equal to sent method %q %q", rcvRequest.URL, petition.URL)
}
consumer.Stop()
select {
case <-endCh:
case <-time.After(time.Second):
t.Fatal("time out stopping")
}
reply := Reply{}
db := sessionTest.DB(cfgTest.Database)
respColl := db.C(cfgTest.ResponsesColl)
petColl := db.C(cfgTest.PetitionsColl)
err = respColl.Find(bson.M{"id": idPetition}).One(&reply)
if err != nil {
t.Fatalf("reply should be stored %v", err)
}
if !reflect.DeepEqual(reply.Body, testBodyResponse) {
t.Errorf("target response body does not match %v %v", reply.Body, testBodyResponse)
}
err = petColl.Find(bson.M{"id": idPetition}).One(&Petition{})
if err == nil {
t.Errorf("petition should be deleted %q", idPetition)
}
}
func TestConsumerErrorResponse(t *testing.T) {
setUp(t)
defer tearDown(t)
reqChan := make(chan *Petition, cfgTest.QueueSize)
resultCh := make(chan *http.Request, 1)
consumer := newTestConsumer(reqChan)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Log("received ", r)
w.WriteHeader(http.StatusInternalServerError)
resultCh <- r
}))
defer ts.Close()
u, err := url.Parse(ts.URL)
if err != nil {
t.Fatal(err)
}
u.Path = "/x/y/z"
u.RawQuery = "q=a&r=b"
petition := &Petition{
ID: idPetition,
TargetHost: u.Host,
TargetScheme: u.Scheme,
Method: "GET",
URL: u,
Proto: "HTTP/1.1",
RemoteAddr: "127.0.0.1",
Host: u.Host,
Created: time.Now(),
}
endCh := consumer.Start(2)
reqChan <- petition
select {
case <-resultCh:
case <-time.After(3 * time.Second):
t.Fatal("target server waiting too long")
}
consumer.Stop()
select {
case <-endCh:
case <-time.After(time.Second):
t.Fatal("time out stopping")
}
reply := Reply{}
db := sessionTest.DB(cfgTest.Database)
respColl := db.C(cfgTest.ResponsesColl)
errColl := db.C(cfgTest.ErrorsColl)
petColl := db.C(cfgTest.PetitionsColl)
err = respColl.Find(bson.M{"id": idPetition}).One(&reply)
if err != nil {
t.Fatalf("reply should be stored in petition store %v", err)
}
if reply.StatusCode != http.StatusInternalServerError {
t.Error("reply status code distinct to response status code %d != %d", reply.StatusCode, http.StatusInternalServerError)
}
err = errColl.Find(bson.M{"id": idPetition}).One(&reply)
if err != nil {
t.Fatalf("reply should be stored in errors collections %v", err)
}
if reply.StatusCode != http.StatusInternalServerError {
t.Error("reply status code distinct to response status code %d != %d", reply.StatusCode, http.StatusInternalServerError)
}
err = petColl.Find(bson.M{"id": idPetition}).One(&Petition{})
if err == nil {
t.Errorf("petition should be deleted %q", idPetition)
}
}
func newTestConsumer(petCh chan *Petition) *Consumer {
return &Consumer{GetFrom: petCh, Cfg: cfgTest, SessionSeed: sessionTest}
}