0 | 0 |
package http_test
|
1 | 1 |
|
2 | 2 |
import (
|
|
3 |
"io"
|
3 | 4 |
"net/http"
|
4 | 5 |
"net/http/httptest"
|
5 | 6 |
"net/url"
|
|
11 | 12 |
httptransport "github.com/go-kit/kit/transport/http"
|
12 | 13 |
)
|
13 | 14 |
|
|
15 |
type TestResponse struct {
|
|
16 |
Body io.ReadCloser
|
|
17 |
String string
|
|
18 |
}
|
|
19 |
|
14 | 20 |
func TestHTTPClient(t *testing.T) {
|
15 | 21 |
var (
|
16 | |
encode = func(*http.Request, interface{}) error { return nil }
|
17 | |
decode = func(*http.Response) (interface{}, error) { return struct{}{}, nil }
|
|
22 |
testbody = "testbody"
|
|
23 |
encode = func(*http.Request, interface{}) error { return nil }
|
|
24 |
decode = func(r *http.Response) (interface{}, error) {
|
|
25 |
buffer := make([]byte, len(testbody))
|
|
26 |
r.Body.Read(buffer)
|
|
27 |
return TestResponse{r.Body, string(buffer)}, nil
|
|
28 |
}
|
18 | 29 |
headers = make(chan string, 1)
|
19 | 30 |
headerKey = "X-Foo"
|
20 | 31 |
headerVal = "abcde"
|
|
23 | 34 |
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
24 | 35 |
headers <- r.Header.Get(headerKey)
|
25 | 36 |
w.WriteHeader(http.StatusOK)
|
|
37 |
w.Write([]byte(testbody))
|
26 | 38 |
}))
|
27 | 39 |
|
28 | 40 |
client := httptransport.NewClient(
|
|
33 | 45 |
httptransport.SetClientBefore(httptransport.SetRequestHeader(headerKey, headerVal)),
|
34 | 46 |
)
|
35 | 47 |
|
36 | |
_, err := client.Endpoint()(context.Background(), struct{}{})
|
|
48 |
res, err := client.Endpoint()(context.Background(), struct{}{})
|
37 | 49 |
if err != nil {
|
38 | 50 |
t.Fatal(err)
|
39 | 51 |
}
|
|
44 | 56 |
case <-time.After(time.Millisecond):
|
45 | 57 |
t.Fatalf("timeout waiting for %s", headerKey)
|
46 | 58 |
}
|
|
59 |
// Check that Request Header was successfully received
|
47 | 60 |
if want := headerVal; want != have {
|
|
61 |
t.Errorf("want %q, have %q", want, have)
|
|
62 |
}
|
|
63 |
|
|
64 |
// Check that the response was successfully decoded
|
|
65 |
response, ok := res.(TestResponse)
|
|
66 |
if !ok {
|
|
67 |
t.Fatal("response should be TestResponse")
|
|
68 |
}
|
|
69 |
if want, have := testbody, response.String; want != have {
|
|
70 |
t.Errorf("want %q, have %q", want, have)
|
|
71 |
}
|
|
72 |
|
|
73 |
// Check that response body was closed
|
|
74 |
b := make([]byte, 1)
|
|
75 |
_, err = response.Body.Read(b)
|
|
76 |
if err == nil {
|
|
77 |
t.Fatal("wanted error, got none")
|
|
78 |
}
|
|
79 |
if doNotWant, have := io.EOF, err; doNotWant == have {
|
|
80 |
t.Errorf("do not want %q, have %q", doNotWant, have)
|
|
81 |
}
|
|
82 |
}
|
|
83 |
|
|
84 |
func TestHTTPClientBufferedStream(t *testing.T) {
|
|
85 |
var (
|
|
86 |
testbody = "testbody"
|
|
87 |
encode = func(*http.Request, interface{}) error { return nil }
|
|
88 |
decode = func(r *http.Response) (interface{}, error) {
|
|
89 |
return TestResponse{r.Body, ""}, nil
|
|
90 |
}
|
|
91 |
)
|
|
92 |
|
|
93 |
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
94 |
w.WriteHeader(http.StatusOK)
|
|
95 |
w.Write([]byte(testbody))
|
|
96 |
}))
|
|
97 |
|
|
98 |
client := httptransport.NewClient(
|
|
99 |
"GET",
|
|
100 |
mustParse(server.URL),
|
|
101 |
encode,
|
|
102 |
decode,
|
|
103 |
httptransport.SetBufferedStream(true),
|
|
104 |
)
|
|
105 |
|
|
106 |
res, err := client.Endpoint()(context.Background(), struct{}{})
|
|
107 |
if err != nil {
|
|
108 |
t.Fatal(err)
|
|
109 |
}
|
|
110 |
|
|
111 |
// Check that the response was successfully decoded
|
|
112 |
response, ok := res.(TestResponse)
|
|
113 |
if !ok {
|
|
114 |
t.Fatal("response should be TestResponse")
|
|
115 |
}
|
|
116 |
|
|
117 |
// Check that response body was NOT closed
|
|
118 |
b := make([]byte, len(testbody))
|
|
119 |
_, err = response.Body.Read(b)
|
|
120 |
if want, have := io.EOF, err; have != want {
|
|
121 |
t.Fatal("want %q, have %q", want, have)
|
|
122 |
}
|
|
123 |
if want, have := testbody, string(b); want != have {
|
48 | 124 |
t.Errorf("want %q, have %q", want, have)
|
49 | 125 |
}
|
50 | 126 |
}
|