Merge pull request #16 from rs/xhandler/restore_from_ctx

Restore various FromContext functions
master
Olivier Poitrey 6 years ago committed by GitHub
commit 1eb70cf152

@ -10,6 +10,8 @@ import (
"github.com/stretchr/testify/assert"
)
var testRequest, _ = http.NewRequest("GET", "/", nil)
func TestAppendHandlerC(t *testing.T) {
init := 0
h1 := func(next HandlerC) HandlerC {
@ -35,8 +37,8 @@ func TestAppendHandlerC(t *testing.T) {
assert.Equal(t, 2, ctx.Value("test"), "second handler should overwrite first handler's context value")
}))
h.ServeHTTP(nil, nil)
h.ServeHTTP(nil, nil)
h.ServeHTTP(nil, testRequest)
h.ServeHTTP(nil, testRequest)
assert.Equal(t, 1, init, "handler init called once")
}
@ -71,8 +73,8 @@ func TestAppendHandler(t *testing.T) {
assert.NotNil(t, r)
}))
h.ServeHTTP(nil, nil)
h.ServeHTTP(nil, nil)
h.ServeHTTP(nil, testRequest)
h.ServeHTTP(nil, testRequest)
// There's no safe way to not initialize non ctx aware handlers on each request :/
//assert.Equal(t, 1, init, "handler init called once")
}
@ -107,7 +109,7 @@ func TestChainHandlerC(t *testing.T) {
}))
mainCtx := context.WithValue(context.Background(), "mainCtx", 1)
h.ServeHTTPC(mainCtx, nil, nil)
h.ServeHTTPC(mainCtx, nil, testRequest)
assert.Equal(t, 3, handlerCalls, "all handler called once")
}
@ -150,7 +152,7 @@ func TestAdd(t *testing.T) {
}))
mainCtx := context.WithValue(context.Background(), "mainCtx", 1)
h.ServeHTTPC(mainCtx, nil, nil)
h.ServeHTTPC(mainCtx, nil, testRequest)
assert.Equal(t, 4, handlerCalls, "all handler called once")
}
@ -202,9 +204,9 @@ func TestWith(t *testing.T) {
}))
mainCtx := context.WithValue(context.Background(), "mainCtx", 1)
h.ServeHTTPC(mainCtx, nil, nil)
h.ServeHTTPC(mainCtx, nil, testRequest)
assert.Equal(t, 2, handlerCalls, "all handlers called once")
handlerCalls = 0
i.ServeHTTPC(mainCtx, nil, nil)
i.ServeHTTPC(mainCtx, nil, testRequest)
assert.Equal(t, 4, handlerCalls, "all handler called once")
}

@ -0,0 +1,19 @@
// +build go1.7
package xhandler
import (
"net/http"
"context"
)
// New creates a conventional http.Handler injecting the provided root
// context to sub handlers. This handler is used as a bridge between conventional
// http.Handler and context aware handlers.
func New(ctx context.Context, h HandlerC) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h.ServeHTTPC(ctx, w, r.WithContext(ctx))
})
}

@ -0,0 +1,19 @@
// +build !go1.7
package xhandler
import (
"net/http"
"golang.org/x/net/context"
)
// New creates a conventional http.Handler injecting the provided root
// context to sub handlers. This handler is used as a bridge between conventional
// http.Handler and context aware handlers.
func New(ctx context.Context, h HandlerC) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h.ServeHTTPC(ctx, w, r)
})
}

@ -32,11 +32,3 @@ func (f HandlerFuncC) ServeHTTPC(ctx context.Context, w http.ResponseWriter, r *
f(ctx, w, r)
}
// New creates a conventional http.Handler injecting the provided root
// context to sub handlers. This handler is used as a bridge between conventional
// http.Handler and context aware handlers.
func New(ctx context.Context, h HandlerC) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h.ServeHTTPC(ctx, w, r)
})
}

@ -57,6 +57,10 @@ func TestHandlerFunc(t *testing.T) {
xh := HandlerFuncC(func(context.Context, http.ResponseWriter, *http.Request) {
ok = true
})
xh.ServeHTTPC(nil, nil, nil)
r, err := http.NewRequest("GET", "http://example.com/foo", nil)
if err != nil {
log.Fatal(err)
}
xh.ServeHTTPC(context.Background(), nil, r)
assert.True(t, ok)
}

Loading…
Cancel
Save