OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / go-kit / kit / transport / httprp / server_test.go
1 package httprp_test
2
3 import (
4         "context"
5         "io/ioutil"
6         "net/http"
7         "net/http/httptest"
8         "net/url"
9         "testing"
10
11         httptransport "github.com/go-kit/kit/transport/httprp"
12 )
13
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"))
18         }))
19         defer originServer.Close()
20         originURL, _ := url.Parse(originServer.URL)
21
22         handler := httptransport.NewServer(
23                 originURL,
24         )
25         proxyServer := httptest.NewServer(handler)
26         defer proxyServer.Close()
27
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)
31         }
32
33         responseBody, _ := ioutil.ReadAll(resp.Body)
34         if want, have := "hey", string(responseBody); want != have {
35                 t.Errorf("want %q, have %q", want, have)
36         }
37 }
38
39 func TestServerHappyPathSingleServerWithServerOptions(t *testing.T) {
40         const (
41                 headerKey = "X-TEST-HEADER"
42                 headerVal = "go-kit-proxy"
43         )
44
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)
48                 }
49
50                 w.WriteHeader(http.StatusOK)
51                 w.Write([]byte("hey"))
52         }))
53         defer originServer.Close()
54         originURL, _ := url.Parse(originServer.URL)
55
56         handler := httptransport.NewServer(
57                 originURL,
58                 httptransport.ServerBefore(func(ctx context.Context, r *http.Request) context.Context {
59                         r.Header.Add(headerKey, headerVal)
60                         return ctx
61                 }),
62         )
63         proxyServer := httptest.NewServer(handler)
64         defer proxyServer.Close()
65
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)
69         }
70
71         responseBody, _ := ioutil.ReadAll(resp.Body)
72         if want, have := "hey", string(responseBody); want != have {
73                 t.Errorf("want %q, have %q", want, have)
74         }
75 }
76
77 func TestServerOriginServerNotFoundResponse(t *testing.T) {
78         originServer := httptest.NewServer(http.NotFoundHandler())
79         defer originServer.Close()
80         originURL, _ := url.Parse(originServer.URL)
81
82         handler := httptransport.NewServer(
83                 originURL,
84         )
85         proxyServer := httptest.NewServer(handler)
86         defer proxyServer.Close()
87
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)
91         }
92 }
93
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)
98         }))
99         originURL, _ := url.Parse(originServer.URL)
100         originServer.Close()
101
102         handler := httptransport.NewServer(
103                 originURL,
104         )
105         proxyServer := httptest.NewServer(handler)
106         defer proxyServer.Close()
107
108         resp, _ := http.Get(proxyServer.URL)
109         switch resp.StatusCode {
110         case http.StatusBadGateway: // go1.7 and beyond
111                 break
112         case http.StatusInternalServerError: // to go1.7
113                 break
114         default:
115                 t.Errorf("want %d or %d, have %d", http.StatusBadGateway, http.StatusInternalServerError, resp.StatusCode)
116         }
117 }
118
119 func TestMultipleServerBefore(t *testing.T) {
120         const (
121                 headerKey = "X-TEST-HEADER"
122                 headerVal = "go-kit-proxy"
123         )
124
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)
128                 }
129
130                 w.WriteHeader(http.StatusOK)
131                 w.Write([]byte("hey"))
132         }))
133         defer originServer.Close()
134         originURL, _ := url.Parse(originServer.URL)
135
136         handler := httptransport.NewServer(
137                 originURL,
138                 httptransport.ServerBefore(func(ctx context.Context, r *http.Request) context.Context {
139                         r.Header.Add(headerKey, headerVal)
140                         return ctx
141                 }),
142                 httptransport.ServerBefore(func(ctx context.Context, r *http.Request) context.Context {
143                         return ctx
144                 }),
145         )
146         proxyServer := httptest.NewServer(handler)
147         defer proxyServer.Close()
148
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)
152         }
153
154         responseBody, _ := ioutil.ReadAll(resp.Body)
155         if want, have := "hey", string(responseBody); want != have {
156                 t.Errorf("want %q, have %q", want, have)
157         }
158 }