Codebase list golang-github-go-kit-kit / fd2bb1e
update based on comments update based on comments to previous commit. TRAVIS ALLEN SALAS COX 6 years ago
2 changed file(s) with 12 addition(s) and 17 deletion(s). Raw diff Collapse all Expand all
9090 ctx, cancel := context.WithCancel(ctx)
9191 defer cancel()
9292
93 // Vars used for client finalizer to ensure there are no nil values
9493 var (
95 req *http.Request = &http.Request{}
96 resp *http.Response = &http.Response{}
94 resp *http.Response
9795 err error
9896 )
9997 if c.finalizer != nil {
10098 defer func() {
101 ctx = context.WithValue(ctx, ContextKeyResponseHeaders, resp.Header)
102 ctx = context.WithValue(ctx, ContextKeyResponseSize, resp.ContentLength)
103 c.finalizer(ctx, resp.StatusCode, req)
99 if resp != nil {
100 ctx = context.WithValue(ctx, ContextKeyResponseHeaders, resp.Header)
101 ctx = context.WithValue(ctx, ContextKeyResponseSize, resp.ContentLength)
102 }
103 c.finalizer(ctx, err)
104104 }()
105105 }
106106
107 req, err = http.NewRequest(c.method, c.tgt.String(), nil)
107 req, err := http.NewRequest(c.method, c.tgt.String(), nil)
108108 if err != nil {
109109 return nil, err
110110 }
141141
142142 // ClientFinalizerFunc can be used to perform work at the end of a client HTTP
143143 // request, after the response is returned. The principal
144 // intended use is for request logging. In addition to the response code
145 // provided in the function signature, additional response parameters are
144 // intended use is for error logging. Additional response parameters are
146145 // provided in the context under keys with the ContextKeyResponse prefix.
147 type ClientFinalizerFunc func(ctx context.Context, code int, r *http.Request)
146 // Note: err may be nil. There maybe also no additional response parameters depending on
147 // when an error occurs.
148 type ClientFinalizerFunc func(ctx context.Context, err error)
148149
149150 // EncodeJSONRequest is an EncodeRequestFunc that serializes the request as a
150151 // JSON object to the Request body. Many JSON-over-HTTP services can use it as
143143 var (
144144 headerKey = "X-Henlo-Lizer"
145145 headerVal = "Helllo you stinky lizard"
146 statusCode = http.StatusTeapot
147146 responseBody = "go eat a fly ugly\n"
148147 done = make(chan struct{})
149148 encode = func(context.Context, *http.Request, interface{}) error { return nil }
154153
155154 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
156155 w.Header().Set(headerKey, headerVal)
157 w.WriteHeader(statusCode)
158156 w.Write([]byte(responseBody))
159157 }))
160158 defer server.Close()
164162 mustParse(server.URL),
165163 encode,
166164 decode,
167 httptransport.ClientFinalizer(func(ctx context.Context, code int, _ *http.Request) {
168 if want, have := statusCode, code; want != have {
169 t.Errorf("StatusCode: want %d, have %d", want, have)
170 }
171
165 httptransport.ClientFinalizer(func(ctx context.Context, err error) {
172166 responseHeader := ctx.Value(httptransport.ContextKeyResponseHeaders).(http.Header)
173167 if want, have := headerVal, responseHeader.Get(headerKey); want != have {
174168 t.Errorf("%s: want %q, have %q", headerKey, want, have)