-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer_test.go
93 lines (88 loc) · 2.39 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
// consumer_test.go
package minigrush
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"testing"
"time"
"github.com/crbrox/store/dummy"
)
const idPetition = "1"
var testBody = []byte("sent content to target host")
var testBodyResponse = []byte("Hello, client (from target host)\n")
func TestAll(t *testing.T) {
storeP := dummy.Store{}
storeR := dummy.Store{}
channel := make(chan *Petition, 1000)
consumer := &Consumer{GetFrom: channel, PetitionStore: storeP, ReplyStore: storeR}
resultCh := make(chan *http.Request, 1)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, string(testBodyResponse))
rcvdBody, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(rcvdBody, testBody) {
t.Error("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)
channel <- petition
rcvRequest := <-resultCh
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.Error("time out stopping")
}
br, err := storeR.Get(idPetition)
if err != nil {
t.Errorf("reply should be stored %v", err)
}
reply := &Reply{}
err = json.Unmarshal(br, reply)
if err != nil {
t.Errorf("reply should be json-encoded %v", err)
}
if !reflect.DeepEqual(reply.Body, testBodyResponse) {
t.Errorf("target response body does not match %v %v", reply.Body, testBodyResponse)
}
_, err = storeP.Get(idPetition)
if err == nil {
t.Error("petition should be deleted %q", idPetition)
}
}