-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_test.go
111 lines (94 loc) · 2.47 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
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
package congruent
import (
"fmt"
"net/http"
"net/http/httptest"
"sort"
"testing"
)
func TestPrepareBodyString(t *testing.T) {
expected := "heyooo"
req := &Request{Body: expected}
buf, err := req.PrepareBody()
if err != nil {
t.Error(err)
t.Fail()
}
if string(buf.Bytes()) != expected {
t.Errorf("expected %s, got %s", expected, buf.Bytes())
}
}
func TestPrepareBodyJSON(t *testing.T) {
expected := `{"ok":true,"items":["a","b","c"]}`
type testData struct {
Ok bool `json:"ok"`
Items []string `json:"items"`
}
req := &Request{Body: &testData{true, []string{"a", "b", "c"}}}
buf, err := req.PrepareBody()
if err != nil {
t.Error(err)
t.Fail()
}
if string(buf.Bytes()) != expected {
t.Errorf("expected %s, got %s", expected, buf.Bytes())
}
}
func TestRequest(t *testing.T) {
// channel for storing "has been called" ints from test servers
c := make(chan int, 2)
ts0 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "a")
// check header
if h := r.Header.Get("X-Test"); h != "zero" {
t.Errorf(`Expected header value "zero", got %s`, h)
}
// write to channel to track request
c <- 0
}))
defer ts0.Close()
ts1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "b")
// check header
if h := r.Header.Get("X-Test"); h != "one" {
t.Errorf(`Expected header value "one", got %s`, h)
}
// write to channel to track request
c <- 1
}))
defer ts1.Close()
servers := Servers([]*Server{
NewServer(ts0.URL, &http.Header{"X-Test": []string{"zero"}}),
NewServer(ts1.URL, &http.Header{"X-Test": []string{"one"}})})
request := NewRequest("GET", "/", nil, nil)
responses, err := servers.Request(request)
if err != nil {
t.Error(err)
t.Fail()
}
// block until both requests finish, and save results
var seen []int
for i := 0; i < 2; i++ {
seen = append(seen, <-c)
}
// verify that we saw responses from both servers
sort.Sort(sort.IntSlice(seen))
for i, n := range seen {
if i != n {
t.Errorf("Expected server %d to be called, saw %d", i, n)
}
}
// verify we saw responses from each test server
var resp []string
for _, response := range responses {
resp = append(resp, string(response.Body))
}
// verify that we saw responses from both servers
sort.Sort(sort.StringSlice(resp))
if resp[0] != "a" {
t.Errorf(`Expected "a", got %s`, resp[0])
}
if resp[1] != "b" {
t.Errorf(`Expected "b", got %s`, resp[1])
}
}