| 90 | } |
| 91 | |
| 92 | func TestMultipleServerBefore(t *testing.T) { |
| 93 | var ( |
| 94 | headerKey = "X-Henlo-Lizer" |
| 95 | headerVal = "Helllo you stinky lizard" |
| 96 | statusCode = http.StatusTeapot |
| 97 | responseBody = "go eat a fly ugly\n" |
| 98 | done = make(chan struct{}) |
| 99 | ) |
| 100 | handler := httptransport.NewServer( |
| 101 | endpoint.Nop, |
| 102 | func(context.Context, *http.Request) (interface{}, error) { |
| 103 | return struct{}{}, nil |
| 104 | }, |
| 105 | func(_ context.Context, w http.ResponseWriter, _ interface{}) error { |
| 106 | w.Header().Set(headerKey, headerVal) |
| 107 | w.WriteHeader(statusCode) |
| 108 | w.Write([]byte(responseBody)) |
| 109 | return nil |
| 110 | }, |
| 111 | httptransport.ServerBefore(func(ctx context.Context, r *http.Request) context.Context { |
| 112 | ctx = context.WithValue(ctx, "one", 1) |
| 113 | |
| 114 | return ctx |
| 115 | }), |
| 116 | httptransport.ServerBefore(func(ctx context.Context, r *http.Request) context.Context { |
| 117 | if _, ok := ctx.Value("one").(int); !ok { |
| 118 | t.Error("Value was not set properly when multiple ServerBefores are used") |
| 119 | } |
| 120 | |
| 121 | close(done) |
| 122 | return ctx |
| 123 | }), |
| 124 | ) |
| 125 | |
| 126 | server := httptest.NewServer(handler) |
| 127 | defer server.Close() |
| 128 | go http.Get(server.URL) |
| 129 | |
| 130 | select { |
| 131 | case <-done: |
| 132 | case <-time.After(time.Second): |
| 133 | t.Fatal("timeout waiting for finalizer") |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | func TestMultipleServerAfter(t *testing.T) { |
| 138 | var ( |