11 httptransport "github.com/go-kit/kit/transport/httprp"
14 func TestServerHappyPathSingleServer(t *testing.T) {
15 originServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
16 w.WriteHeader(http.StatusOK)
17 w.Write([]byte("hey"))
19 defer originServer.Close()
20 originURL, _ := url.Parse(originServer.URL)
22 handler := httptransport.NewServer(
25 proxyServer := httptest.NewServer(handler)
26 defer proxyServer.Close()
28 resp, _ := http.Get(proxyServer.URL)
29 if want, have := http.StatusOK, resp.StatusCode; want != have {
30 t.Errorf("want %d, have %d", want, have)
33 responseBody, _ := ioutil.ReadAll(resp.Body)
34 if want, have := "hey", string(responseBody); want != have {
35 t.Errorf("want %q, have %q", want, have)
39 func TestServerHappyPathSingleServerWithServerOptions(t *testing.T) {
41 headerKey = "X-TEST-HEADER"
42 headerVal = "go-kit-proxy"
45 originServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
46 if want, have := headerVal, r.Header.Get(headerKey); want != have {
47 t.Errorf("want %q, have %q", want, have)
50 w.WriteHeader(http.StatusOK)
51 w.Write([]byte("hey"))
53 defer originServer.Close()
54 originURL, _ := url.Parse(originServer.URL)
56 handler := httptransport.NewServer(
58 httptransport.ServerBefore(func(ctx context.Context, r *http.Request) context.Context {
59 r.Header.Add(headerKey, headerVal)
63 proxyServer := httptest.NewServer(handler)
64 defer proxyServer.Close()
66 resp, _ := http.Get(proxyServer.URL)
67 if want, have := http.StatusOK, resp.StatusCode; want != have {
68 t.Errorf("want %d, have %d", want, have)
71 responseBody, _ := ioutil.ReadAll(resp.Body)
72 if want, have := "hey", string(responseBody); want != have {
73 t.Errorf("want %q, have %q", want, have)
77 func TestServerOriginServerNotFoundResponse(t *testing.T) {
78 originServer := httptest.NewServer(http.NotFoundHandler())
79 defer originServer.Close()
80 originURL, _ := url.Parse(originServer.URL)
82 handler := httptransport.NewServer(
85 proxyServer := httptest.NewServer(handler)
86 defer proxyServer.Close()
88 resp, _ := http.Get(proxyServer.URL)
89 if want, have := http.StatusNotFound, resp.StatusCode; want != have {
90 t.Errorf("want %d, have %d", want, have)
94 func TestServerOriginServerUnreachable(t *testing.T) {
95 // create a server, then promptly shut it down
96 originServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
97 w.WriteHeader(http.StatusOK)
99 originURL, _ := url.Parse(originServer.URL)
102 handler := httptransport.NewServer(
105 proxyServer := httptest.NewServer(handler)
106 defer proxyServer.Close()
108 resp, _ := http.Get(proxyServer.URL)
109 switch resp.StatusCode {
110 case http.StatusBadGateway: // go1.7 and beyond
112 case http.StatusInternalServerError: // to go1.7
115 t.Errorf("want %d or %d, have %d", http.StatusBadGateway, http.StatusInternalServerError, resp.StatusCode)
119 func TestMultipleServerBefore(t *testing.T) {
121 headerKey = "X-TEST-HEADER"
122 headerVal = "go-kit-proxy"
125 originServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
126 if want, have := headerVal, r.Header.Get(headerKey); want != have {
127 t.Errorf("want %q, have %q", want, have)
130 w.WriteHeader(http.StatusOK)
131 w.Write([]byte("hey"))
133 defer originServer.Close()
134 originURL, _ := url.Parse(originServer.URL)
136 handler := httptransport.NewServer(
138 httptransport.ServerBefore(func(ctx context.Context, r *http.Request) context.Context {
139 r.Header.Add(headerKey, headerVal)
142 httptransport.ServerBefore(func(ctx context.Context, r *http.Request) context.Context {
146 proxyServer := httptest.NewServer(handler)
147 defer proxyServer.Close()
149 resp, _ := http.Get(proxyServer.URL)
150 if want, have := http.StatusOK, resp.StatusCode; want != have {
151 t.Errorf("want %d, have %d", want, have)
154 responseBody, _ := ioutil.ReadAll(resp.Body)
155 if want, have := "hey", string(responseBody); want != have {
156 t.Errorf("want %q, have %q", want, have)