Package list golang-github-go-kit-kit / f3a31a9
Add EncodeJSONRequest Will Faught 4 years ago
2 changed file(s) with 88 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
00 package http
11
22 import (
3 "bytes"
4 "encoding/json"
5 "io/ioutil"
36 "net/http"
47 "net/url"
58
112115 return response, nil
113116 }
114117 }
118
119 // EncodeJSONRequest is an EncodeRequestFunc that serializes the request as a
120 // JSON object to the Request body. Many JSON-over-HTTP services can use it as
121 // a sensible default. If the request implements Headerer, the provided headers
122 // will be applied to the request.
123 func EncodeJSONRequest(c context.Context, r *http.Request, request interface{}) error {
124 r.Header.Set("Content-Type", "application/json; charset=utf-8")
125 if headerer, ok := request.(Headerer); ok {
126 for k := range headerer.Headers() {
127 r.Header.Set(k, headerer.Headers().Get(k))
128 }
129 }
130 var b bytes.Buffer
131 r.Body = ioutil.NopCloser(&b)
132 return json.NewEncoder(&b).Encode(request)
133 }
11
22 import (
33 "io"
4 "io/ioutil"
45 "net/http"
56 "net/http/httptest"
67 "net/url"
139140 }
140141 }
141142
143 func TestEncodeJSONRequest(t *testing.T) {
144 var header http.Header
145 var body string
146
147 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
148 b, err := ioutil.ReadAll(r.Body)
149 if err != nil && err != io.EOF {
150 t.Fatal(err)
151 }
152 header = r.Header
153 body = string(b)
154 }))
155
156 defer server.Close()
157
158 serverURL, err := url.Parse(server.URL)
159
160 if err != nil {
161 t.Fatal(err)
162 }
163
164 client := httptransport.NewClient(
165 "POST",
166 serverURL,
167 httptransport.EncodeJSONRequest,
168 func(context.Context, *http.Response) (interface{}, error) { return nil, nil },
169 ).Endpoint()
170
171 for _, test := range []struct {
172 value interface{}
173 body string
174 }{
175 {nil, "null\n"},
176 {12, "12\n"},
177 {1.2, "1.2\n"},
178 {true, "true\n"},
179 {"test", "\"test\"\n"},
180 {enhancedRequest{Foo: "foo"}, "{\"foo\":\"foo\"}\n"},
181 } {
182 if _, err := client(context.Background(), test.value); err != nil {
183 t.Error(err)
184 continue
185 }
186
187 if body != test.body {
188 t.Errorf("%v: actual %#v, expected %#v", test.value, body, test.body)
189 }
190 }
191
192 if _, err := client(context.Background(), enhancedRequest{Foo: "foo"}); err != nil {
193 t.Fatal(err)
194 }
195
196 if _, ok := header["X-Edward"]; !ok {
197 t.Fatalf("X-Edward value: actual %v, expected %v", nil, []string{"Snowden"})
198 }
199
200 if v := header.Get("X-Edward"); v != "Snowden" {
201 t.Errorf("X-Edward string: actual %v, expected %v", v, "Snowden")
202 }
203 }
204
142205 func mustParse(s string) *url.URL {
143206 u, err := url.Parse(s)
144207 if err != nil {
146209 }
147210 return u
148211 }
212
213 type enhancedRequest struct {
214 Foo string `json:"foo"`
215 }
216
217 func (e enhancedRequest) Headers() http.Header { return http.Header{"X-Edward": []string{"Snowden"}} }