Skip to content

Commit

Permalink
Merge pull request #97 from remind101/http-handler-compat
Browse files Browse the repository at this point in the history
Ensure Context arg is the same as request Context
  • Loading branch information
bmarini authored Apr 4, 2018
2 parents a2aea2a + 8f46cf0 commit e2ec3e8
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 1 deletion.
2 changes: 2 additions & 0 deletions httpx/middleware/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func (h *Header) ServeHTTPContext(ctx context.Context, w http.ResponseWriter, r
value := e(r)

ctx = httpx.WithHeader(ctx, h.key, value)
r = r.WithContext(ctx)

return h.handler.ServeHTTPContext(ctx, w, r)
}

Expand Down
5 changes: 5 additions & 0 deletions httpx/middleware/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"context"

"github.com/remind101/pkg/httpx"
)

Expand All @@ -24,7 +25,11 @@ func TestHeader(t *testing.T) {
m := ExtractHeader(
httpx.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
data := httpx.Header(ctx, tt.key)
if got, want := data, tt.val; got != want {
t.Fatalf("%s => %s; want %s", tt.key, got, want)
}

data = httpx.Header(r.Context(), tt.key)
if got, want := data, tt.val; got != want {
t.Fatalf("%s => %s; want %s", tt.key, got, want)
}
Expand Down
3 changes: 3 additions & 0 deletions httpx/middleware/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ func LogTo(h httpx.Handler, g loggerGenerator) httpx.Handler {
func InsertLogger(h httpx.Handler, g loggerGenerator) httpx.Handler {
return httpx.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
l := g(ctx, r)

ctx = logger.WithLogger(ctx, l)
r = r.WithContext(ctx)

return h.ServeHTTPContext(ctx, w, r)
})
}
Expand Down
1 change: 1 addition & 0 deletions httpx/middleware/opentracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (h *OpentracingTracer) ServeHTTPContext(ctx context.Context, w http.Respons

defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
r = r.WithContext(ctx)

rw := NewResponseWriter(w)
reqErr := h.handler.ServeHTTPContext(ctx, rw, r)
Expand Down
2 changes: 2 additions & 0 deletions httpx/middleware/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func (m *Reporter) ServeHTTPContext(ctx context.Context, w http.ResponseWriter,
// Add the request id to reporter context.
ctx = errors.WithInfo(ctx, "request_id", httpx.RequestID(ctx))

r = r.WithContext(ctx)

return m.handler.ServeHTTPContext(ctx, w, r)
}

Expand Down
3 changes: 3 additions & 0 deletions httpx/middleware/request_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"

"context"

"github.com/remind101/pkg/httpx"
)

Expand Down Expand Up @@ -39,5 +40,7 @@ func (h *RequestID) ServeHTTPContext(ctx context.Context, w http.ResponseWriter,
requestID := e(r)

ctx = httpx.WithRequestID(ctx, requestID)
r = r.WithContext(ctx)

return h.handler.ServeHTTPContext(ctx, w, r)
}
8 changes: 7 additions & 1 deletion httpx/middleware/request_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"net/http/httptest"
"testing"

"github.com/remind101/pkg/httpx"
"context"

"github.com/remind101/pkg/httpx"
)

func TestRequestID(t *testing.T) {
Expand All @@ -23,7 +24,12 @@ func TestRequestID(t *testing.T) {
m := &RequestID{
handler: httpx.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
requestID := httpx.RequestID(ctx)
if got, want := requestID, tt.id; got != want {
t.Fatalf("RequestID => %s; want %s", got, want)
}

// From request.Context()
requestID = httpx.RequestID(r.Context())
if got, want := requestID, tt.id; got != want {
t.Fatalf("RequestID => %s; want %s", got, want)
}
Expand Down

0 comments on commit e2ec3e8

Please sign in to comment.