Skip to content

Commit

Permalink
Merge pull request #16 from rs/xhandler/restore_from_ctx
Browse files Browse the repository at this point in the history
Restore various FromContext functions
  • Loading branch information
rs committed Jul 7, 2017
2 parents fe78910 + a4a0e7f commit 1eb70cf
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 17 deletions.
18 changes: 10 additions & 8 deletions chain_test.go
Expand Up @@ -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 {
Expand All @@ -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")
}

Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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")
}

Expand Down Expand Up @@ -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")
}
19 changes: 19 additions & 0 deletions new.go
@@ -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))
})
}
19 changes: 19 additions & 0 deletions new_pre17.go
@@ -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)
})
}
8 changes: 0 additions & 8 deletions xhandler.go
Expand Up @@ -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)
})
}
6 changes: 5 additions & 1 deletion xhandler_test.go
Expand Up @@ -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)
}

0 comments on commit 1eb70cf

Please sign in to comment.