MCPcopy
hub / github.com/go-kit/kit / TestServerFinalizer

Function TestServerFinalizer

transport/http/server_test.go:182–229  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

180}
181
182func TestServerFinalizer(t *testing.T) {
183 var (
184 headerKey = "X-Henlo-Lizer"
185 headerVal = "Helllo you stinky lizard"
186 statusCode = http.StatusTeapot
187 responseBody = "go eat a fly ugly\n"
188 done = make(chan struct{})
189 )
190 handler := httptransport.NewServer(
191 endpoint.Nop,
192 func(context.Context, *http.Request) (interface{}, error) {
193 return struct{}{}, nil
194 },
195 func(_ context.Context, w http.ResponseWriter, _ interface{}) error {
196 w.Header().Set(headerKey, headerVal)
197 w.WriteHeader(statusCode)
198 w.Write([]byte(responseBody))
199 return nil
200 },
201 httptransport.ServerFinalizer(func(ctx context.Context, code int, _ *http.Request) {
202 if want, have := statusCode, code; want != have {
203 t.Errorf("StatusCode: want %d, have %d", want, have)
204 }
205
206 responseHeader := ctx.Value(httptransport.ContextKeyResponseHeaders).(http.Header)
207 if want, have := headerVal, responseHeader.Get(headerKey); want != have {
208 t.Errorf("%s: want %q, have %q", headerKey, want, have)
209 }
210
211 responseSize := ctx.Value(httptransport.ContextKeyResponseSize).(int64)
212 if want, have := int64(len(responseBody)), responseSize; want != have {
213 t.Errorf("response size: want %d, have %d", want, have)
214 }
215
216 close(done)
217 }),
218 )
219
220 server := httptest.NewServer(handler)
221 defer server.Close()
222 go http.Get(server.URL)
223
224 select {
225 case <-done:
226 case <-time.After(time.Second):
227 t.Fatal("timeout waiting for finalizer")
228 }
229}
230
231type enhancedResponse struct {
232 Foo string `json:"foo"`

Callers

nothing calls this directly

Calls 6

SetMethod · 0.65
WriteMethod · 0.65
WriteHeaderMethod · 0.45
ValueMethod · 0.45
GetMethod · 0.45
CloseMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…