...
1 package http
2
3 import (
4 "context"
5 "fmt"
6 "net/http"
7 "net/http/httptest"
8 )
9
10 func ExamplePopulateRequestContext() {
11 handler := NewServer(
12 func(ctx context.Context, request interface{}) (response interface{}, err error) {
13 fmt.Println("Method", ctx.Value(ContextKeyRequestMethod).(string))
14 fmt.Println("RequestPath", ctx.Value(ContextKeyRequestPath).(string))
15 fmt.Println("RequestURI", ctx.Value(ContextKeyRequestURI).(string))
16 fmt.Println("X-Request-ID", ctx.Value(ContextKeyRequestXRequestID).(string))
17 return struct{}{}, nil
18 },
19 func(context.Context, *http.Request) (interface{}, error) { return struct{}{}, nil },
20 func(context.Context, http.ResponseWriter, interface{}) error { return nil },
21 ServerBefore(PopulateRequestContext),
22 )
23
24 server := httptest.NewServer(handler)
25 defer server.Close()
26
27 req, _ := http.NewRequest("PATCH", fmt.Sprintf("%s/search?q=sympatico", server.URL), nil)
28 req.Header.Set("X-Request-Id", "a1b2c3d4e5")
29 http.DefaultClient.Do(req)
30
31
32
33
34
35
36 }
37
View as plain text