forked from hudl/fargo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc_test.go
45 lines (36 loc) · 994 Bytes
/
rpc_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
package fargo
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
type roundtripper struct {
TripCount int
}
func (r *roundtripper) RoundTrip(req *http.Request) (*http.Response, error) {
r.TripCount++
return http.DefaultTransport.RoundTrip(req)
}
func TestHttpClient(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello World")
}))
defer server.Close()
Convey("Given fargo.HttpClient is set to a custom client", t, func() {
rt := new(roundtripper)
HttpClient = &http.Client{
Transport: rt,
}
Convey("netReq uses that client to handle requests", func() {
req, err := http.NewRequest("GET", server.URL, nil)
So(err, ShouldBeNil)
respBody, respCode, err := netReq(req)
So(err, ShouldBeNil)
So(respCode, ShouldEqual, 200)
So(string(respBody), ShouldEqual, "Hello World")
So(rt.TripCount, ShouldEqual, 1)
})
})
}