diff options
| author | Felix Hanley <felix@userspace.com.au> | 2016-05-11 07:43:19 +0000 |
|---|---|---|
| committer | Felix Hanley <felix@userspace.com.au> | 2016-05-11 07:43:19 +0000 |
| commit | 01d4169b551675aa04e9075915113d2e94fdb72d (patch) | |
| tree | 0eaad50b248928795ba6c5c24873b7b84b0bca09 /src/dict2rest/vendor/github.com/rs/xhandler | |
| parent | a5be2d0a2f450dd2e2c699e45773016528d43876 (diff) | |
| download | go-dict2rest-01d4169b551675aa04e9075915113d2e94fdb72d.tar.gz go-dict2rest-01d4169b551675aa04e9075915113d2e94fdb72d.tar.bz2 | |
Use Golang 1.6 vendor structure
Diffstat (limited to 'src/dict2rest/vendor/github.com/rs/xhandler')
12 files changed, 735 insertions, 0 deletions
diff --git a/src/dict2rest/vendor/github.com/rs/xhandler/.travis.yml b/src/dict2rest/vendor/github.com/rs/xhandler/.travis.yml new file mode 100644 index 0000000..b65c7a9 --- /dev/null +++ b/src/dict2rest/vendor/github.com/rs/xhandler/.travis.yml @@ -0,0 +1,7 @@ +language: go +go: +- 1.5 +- tip +matrix: + allow_failures: + - go: tip diff --git a/src/dict2rest/vendor/github.com/rs/xhandler/LICENSE b/src/dict2rest/vendor/github.com/rs/xhandler/LICENSE new file mode 100644 index 0000000..47c5e9d --- /dev/null +++ b/src/dict2rest/vendor/github.com/rs/xhandler/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2015 Olivier Poitrey <rs@dailymotion.com> + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is furnished +to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/src/dict2rest/vendor/github.com/rs/xhandler/README.md b/src/dict2rest/vendor/github.com/rs/xhandler/README.md new file mode 100644 index 0000000..91c594b --- /dev/null +++ b/src/dict2rest/vendor/github.com/rs/xhandler/README.md @@ -0,0 +1,134 @@ +# XHandler + +[](https://godoc.org/github.com/rs/xhandler) [](https://raw.githubusercontent.com/rs/xhandler/master/LICENSE) [](https://travis-ci.org/rs/xhandler) [](http://gocover.io/github.com/rs/xhandler) + +XHandler is a bridge between [net/context](https://godoc.org/golang.org/x/net/context) and `http.Handler`. + +It lets you enforce `net/context` in your handlers without sacrificing compatibility with existing `http.Handlers` nor imposing a specific router. + +Thanks to `net/context` deadline management, `xhandler` is able to enforce a per request deadline and will cancel the context when the client closes the connection unexpectedly. + +You may create your own `net/context` aware handler pretty much the same way as you would do with http.Handler. + +Read more about xhandler on [Dailymotion engineering blog](http://engineering.dailymotion.com/our-way-to-go/). + +## Installing + + go get -u github.com/rs/xhandler + +## Usage + +```go +package main + +import ( + "log" + "net/http" + "time" + + "github.com/rs/cors" + "github.com/rs/xhandler" + "golang.org/x/net/context" +) + +type myMiddleware struct { + next xhandler.HandlerC +} + +func (h myMiddleware) ServeHTTPC(ctx context.Context, w http.ResponseWriter, r *http.Request) { + ctx = context.WithValue(ctx, "test", "World") + h.next.ServeHTTPC(ctx, w, r) +} + +func main() { + c := xhandler.Chain{} + + // Add close notifier handler so context is cancelled when the client closes + // the connection + c.UseC(xhandler.CloseHandler) + + // Add timeout handler + c.UseC(xhandler.TimeoutHandler(2 * time.Second)) + + // Middleware putting something in the context + c.UseC(func(next xhandler.HandlerC) xhandler.HandlerC { + return myMiddleware{next: next} + }) + + // Mix it with a non-context-aware middleware handler + c.Use(cors.Default().Handler) + + // Final handler (using handlerFuncC), reading from the context + xh := xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { + value := ctx.Value("test").(string) + w.Write([]byte("Hello " + value)) + }) + + // Bridge context aware handlers with http.Handler using xhandler.Handle() + http.Handle("/test", c.Handler(xh)) + + if err := http.ListenAndServe(":8080", nil); err != nil { + log.Fatal(err) + } +} +``` + +### Using xmux + +Xhandler comes with an optional context aware [muxer](https://github.com/rs/xmux) forked from [httprouter](https://github.com/julienschmidt/httprouter): + +```go +package main + +import ( + "fmt" + "log" + "net/http" + "time" + + "github.com/rs/xhandler" + "github.com/rs/xmux" + "golang.org/x/net/context" +) + +func main() { + c := xhandler.Chain{} + + // Append a context-aware middleware handler + c.UseC(xhandler.CloseHandler) + + // Another context-aware middleware handler + c.UseC(xhandler.TimeoutHandler(2 * time.Second)) + + mux := xmux.New() + + // Use c.Handler to terminate the chain with your final handler + mux.GET("/welcome/:name", xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, req *http.Request) { + fmt.Fprintf(w, "Welcome %s!", xmux.Params(ctx).Get("name")) + })) + + if err := http.ListenAndServe(":8080", c.Handler(mux)); err != nil { + log.Fatal(err) + } +} +``` + +See [xmux](https://github.com/rs/xmux) for more examples. + +## Context Aware Middleware + +Here is a list of `net/context` aware middleware handlers implementing `xhandler.HandlerC` interface. + +Feel free to put up a PR linking your middleware if you have built one: + +| Middleware | Author | Description | +| ---------- | ------ | ----------- | +| [xmux](https://github.com/rs/xmux) | [Olivier Poitrey](https://github.com/rs) | HTTP request muxer | +| [xlog](https://github.com/rs/xlog) | [Olivier Poitrey](https://github.com/rs) | HTTP handler logger | +| [xstats](https://github.com/rs/xstats) | [Olivier Poitrey](https://github.com/rs) | A generic client for service instrumentation | +| [xaccess](https://github.com/rs/xaccess) | [Olivier Poitrey](https://github.com/rs) | HTTP handler access logger with [xlog](https://github.com/rs/xlog) and [xstats](https://github.com/rs/xstats) | +| [cors](https://github.com/rs/cors) | [Olivier Poitrey](https://github.com/rs) | [Cross Origin Resource Sharing](http://www.w3.org/TR/cors/) (CORS) support | + +## Licenses + +All source code is licensed under the [MIT License](https://raw.github.com/rs/xhandler/master/LICENSE). diff --git a/src/dict2rest/vendor/github.com/rs/xhandler/chain.go b/src/dict2rest/vendor/github.com/rs/xhandler/chain.go new file mode 100644 index 0000000..042274d --- /dev/null +++ b/src/dict2rest/vendor/github.com/rs/xhandler/chain.go @@ -0,0 +1,93 @@ +package xhandler + +import ( + "net/http" + + "golang.org/x/net/context" +) + +// Chain is an helper to chain middleware handlers together for an easier +// management. +type Chain []func(next HandlerC) HandlerC + +// UseC appends a context-aware handler to the middleware chain. +func (c *Chain) UseC(f func(next HandlerC) HandlerC) { + *c = append(*c, f) +} + +// Use appends a standard http.Handler to the middleware chain without +// lossing track of the context when inserted between two context aware handlers. +// +// Caveat: the f function will be called on each request so you are better to put +// any initialization sequence outside of this function. +func (c *Chain) Use(f func(next http.Handler) http.Handler) { + xf := func(next HandlerC) HandlerC { + return HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { + n := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + next.ServeHTTPC(ctx, w, r) + }) + f(n).ServeHTTP(w, r) + }) + } + *c = append(*c, xf) +} + +// Handler wraps the provided final handler with all the middleware appended to +// the chain and return a new standard http.Handler instance. +// The context.Background() context is injected automatically. +func (c Chain) Handler(xh HandlerC) http.Handler { + ctx := context.Background() + return c.HandlerCtx(ctx, xh) +} + +// HandlerFC is an helper to provide a function (HandlerFuncC) to Handler(). +// +// HandlerFC is equivalent to: +// c.Handler(xhandler.HandlerFuncC(xhc)) +func (c Chain) HandlerFC(xhf HandlerFuncC) http.Handler { + ctx := context.Background() + return c.HandlerCtx(ctx, HandlerFuncC(xhf)) +} + +// HandlerH is an helper to provide a standard http handler (http.HandlerFunc) +// to Handler(). Your final handler won't have access the context though. +func (c Chain) HandlerH(h http.Handler) http.Handler { + ctx := context.Background() + return c.HandlerCtx(ctx, HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { + h.ServeHTTP(w, r) + })) +} + +// HandlerF is an helper to provide a standard http handler function +// (http.HandlerFunc) to Handler(). Your final handler won't have access +// the context though. +func (c Chain) HandlerF(hf http.HandlerFunc) http.Handler { + ctx := context.Background() + return c.HandlerCtx(ctx, HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { + hf(w, r) + })) +} + +// HandlerCtx wraps the provided final handler with all the middleware appended to +// the chain and return a new standard http.Handler instance. +func (c Chain) HandlerCtx(ctx context.Context, xh HandlerC) http.Handler { + return New(ctx, c.HandlerC(xh)) +} + +// HandlerC wraps the provided final handler with all the middleware appended to +// the chain and returns a HandlerC instance. +func (c Chain) HandlerC(xh HandlerC) HandlerC { + for i := len(c) - 1; i >= 0; i-- { + xh = c[i](xh) + } + return xh +} + +// HandlerCF wraps the provided final handler func with all the middleware appended to +// the chain and returns a HandlerC instance. +// +// HandlerCF is equivalent to: +// c.HandlerC(xhandler.HandlerFuncC(xhc)) +func (c Chain) HandlerCF(xhc HandlerFuncC) HandlerC { + return c.HandlerC(HandlerFuncC(xhc)) +} diff --git a/src/dict2rest/vendor/github.com/rs/xhandler/chain_example_test.go b/src/dict2rest/vendor/github.com/rs/xhandler/chain_example_test.go new file mode 100644 index 0000000..005b5b2 --- /dev/null +++ b/src/dict2rest/vendor/github.com/rs/xhandler/chain_example_test.go @@ -0,0 +1,52 @@ +package xhandler_test + +import ( + "fmt" + "net/http" + "strings" + "time" + + "github.com/rs/cors" + "github.com/rs/xhandler" + "golang.org/x/net/context" +) + +func ExampleChain() { + c := xhandler.Chain{} + // Append a context-aware middleware handler + c.UseC(xhandler.CloseHandler) + + // Mix it with a non-context-aware middleware handler + c.Use(cors.Default().Handler) + + // Another context-aware middleware handler + c.UseC(xhandler.TimeoutHandler(2 * time.Second)) + + mux := http.NewServeMux() + + // Use c.Handler to terminate the chain with your final handler + mux.Handle("/", c.Handler(xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, req *http.Request) { + fmt.Fprintf(w, "Welcome to the home page!") + }))) + + // You can reuse the same chain for other handlers + mux.Handle("/api", c.Handler(xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, req *http.Request) { + fmt.Fprintf(w, "Welcome to the API!") + }))) +} + +func ExampleIf() { + c := xhandler.Chain{} + + // Add timeout handler only if the path match a prefix + c.UseC(xhandler.If( + func(ctx context.Context, w http.ResponseWriter, r *http.Request) bool { + return strings.HasPrefix(r.URL.Path, "/with-timeout/") + }, + xhandler.TimeoutHandler(2*time.Second), + )) + + http.Handle("/", c.Handler(xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, req *http.Request) { + fmt.Fprintf(w, "Welcome to the home page!") + }))) +} diff --git a/src/dict2rest/vendor/github.com/rs/xhandler/chain_test.go b/src/dict2rest/vendor/github.com/rs/xhandler/chain_test.go new file mode 100644 index 0000000..0ce9410 --- /dev/null +++ b/src/dict2rest/vendor/github.com/rs/xhandler/chain_test.go @@ -0,0 +1,112 @@ +package xhandler + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/assert" + "golang.org/x/net/context" +) + +func TestAppendHandlerC(t *testing.T) { + init := 0 + h1 := func(next HandlerC) HandlerC { + init++ + return HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { + ctx = context.WithValue(ctx, "test", 1) + next.ServeHTTPC(ctx, w, r) + }) + } + h2 := func(next HandlerC) HandlerC { + return HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { + ctx = context.WithValue(ctx, "test", 2) + next.ServeHTTPC(ctx, w, r) + }) + } + c := Chain{} + c.UseC(h1) + c.UseC(h2) + assert.Len(t, c, 2) + + h := c.Handler(HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { + // Test ordering + assert.Equal(t, 2, ctx.Value("test"), "second handler should overwrite first handler's context value") + })) + + h.ServeHTTP(nil, nil) + h.ServeHTTP(nil, nil) + assert.Equal(t, 1, init, "handler init called once") +} + +func TestAppendHandler(t *testing.T) { + init := 0 + h1 := func(next HandlerC) HandlerC { + return HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { + ctx = context.WithValue(ctx, "test", 1) + next.ServeHTTPC(ctx, w, r) + }) + } + h2 := func(next http.Handler) http.Handler { + init++ + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // Change r and w values + w = httptest.NewRecorder() + r = &http.Request{} + next.ServeHTTP(w, r) + }) + } + c := Chain{} + c.UseC(h1) + c.Use(h2) + assert.Len(t, c, 2) + + h := c.Handler(HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { + // Test ordering + assert.Equal(t, 1, ctx.Value("test"), + "the first handler value should be pass through the second (non-aware) one") + // Test r and w overwrite + assert.NotNil(t, w) + assert.NotNil(t, r) + })) + + h.ServeHTTP(nil, nil) + h.ServeHTTP(nil, nil) + // There's no safe way to not initialize non ctx aware handlers on each request :/ + //assert.Equal(t, 1, init, "handler init called once") +} + +func TestChainHandlerC(t *testing.T) { + handlerCalls := 0 + h1 := func(next HandlerC) HandlerC { + return HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { + handlerCalls++ + ctx = context.WithValue(ctx, "test", 1) + next.ServeHTTPC(ctx, w, r) + }) + } + h2 := func(next HandlerC) HandlerC { + return HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { + handlerCalls++ + ctx = context.WithValue(ctx, "test", 2) + next.ServeHTTPC(ctx, w, r) + }) + } + + c := Chain{} + c.UseC(h1) + c.UseC(h2) + h := c.HandlerC(HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { + handlerCalls++ + + assert.Equal(t, 2, ctx.Value("test"), + "second handler should overwrite first handler's context value") + assert.Equal(t, 1, ctx.Value("mainCtx"), + "the mainCtx value should be pass through") + })) + + mainCtx := context.WithValue(context.Background(), "mainCtx", 1) + h.ServeHTTPC(mainCtx, nil, nil) + + assert.Equal(t, 3, handlerCalls, "all handler called once") +} diff --git a/src/dict2rest/vendor/github.com/rs/xhandler/middleware.go b/src/dict2rest/vendor/github.com/rs/xhandler/middleware.go new file mode 100644 index 0000000..5de1364 --- /dev/null +++ b/src/dict2rest/vendor/github.com/rs/xhandler/middleware.go @@ -0,0 +1,59 @@ +package xhandler + +import ( + "net/http" + "time" + + "golang.org/x/net/context" +) + +// CloseHandler returns a Handler cancelling the context when the client +// connection close unexpectedly. +func CloseHandler(next HandlerC) HandlerC { + return HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { + // Cancel the context if the client closes the connection + if wcn, ok := w.(http.CloseNotifier); ok { + var cancel context.CancelFunc + ctx, cancel = context.WithCancel(ctx) + defer cancel() + + notify := wcn.CloseNotify() + go func() { + select { + case <-notify: + cancel() + case <-ctx.Done(): + } + }() + } + + next.ServeHTTPC(ctx, w, r) + }) +} + +// TimeoutHandler returns a Handler which adds a timeout to the context. +// +// Child handlers have the responsability to obey the context deadline and to return +// an appropriate error (or not) response in case of timeout. +func TimeoutHandler(timeout time.Duration) func(next HandlerC) HandlerC { + return func(next HandlerC) HandlerC { + return HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { + ctx, _ = context.WithTimeout(ctx, timeout) + next.ServeHTTPC(ctx, w, r) + }) + } +} + +// If is a special handler that will skip insert the condNext handler only if a condition +// applies at runtime. +func If(cond func(ctx context.Context, w http.ResponseWriter, r *http.Request) bool, condNext func(next HandlerC) HandlerC) func(next HandlerC) HandlerC { + return func(next HandlerC) HandlerC { + return HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { + if cond(ctx, w, r) { + condNext(next).ServeHTTPC(ctx, w, r) + } else { + next.ServeHTTPC(ctx, w, r) + } + }) + } +} diff --git a/src/dict2rest/vendor/github.com/rs/xhandler/middleware_test.go b/src/dict2rest/vendor/github.com/rs/xhandler/middleware_test.go new file mode 100644 index 0000000..51306e3 --- /dev/null +++ b/src/dict2rest/vendor/github.com/rs/xhandler/middleware_test.go @@ -0,0 +1,88 @@ +package xhandler + +import ( + "log" + "net/http" + "net/http/httptest" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "golang.org/x/net/context" +) + +func TestTimeoutHandler(t *testing.T) { + ctx := context.WithValue(context.Background(), contextKey, "value") + xh := TimeoutHandler(time.Second)(&handler{}) + h := New(ctx, xh) + w := httptest.NewRecorder() + r, err := http.NewRequest("GET", "http://example.com/foo", nil) + if err != nil { + log.Fatal(err) + } + h.ServeHTTP(w, r) + assert.Equal(t, "value with deadline", w.Body.String()) +} + +type closeNotifyWriter struct { + *httptest.ResponseRecorder + closed bool +} + +func (w *closeNotifyWriter) CloseNotify() <-chan bool { + notify := make(chan bool, 1) + if w.closed { + // return an already "closed" notifier + notify <- true + } + return notify +} + +func TestCloseHandlerClientClose(t *testing.T) { + ctx := context.WithValue(context.Background(), contextKey, "value") + xh := CloseHandler(&handler{}) + h := New(ctx, xh) + w := &closeNotifyWriter{ResponseRecorder: httptest.NewRecorder(), closed: true} + r, err := http.NewRequest("GET", "http://example.com/foo", nil) + if err != nil { + log.Fatal(err) + } + h.ServeHTTP(w, r) + assert.Equal(t, "value canceled", w.Body.String()) +} + +func TestCloseHandlerRequestEnds(t *testing.T) { + ctx := context.WithValue(context.Background(), contextKey, "value") + xh := CloseHandler(&handler{}) + h := New(ctx, xh) + w := &closeNotifyWriter{ResponseRecorder: httptest.NewRecorder(), closed: false} + r, err := http.NewRequest("GET", "http://example.com/foo", nil) + if err != nil { + log.Fatal(err) + } + h.ServeHTTP(w, r) + assert.Equal(t, "value", w.Body.String()) +} + +func TestIf(t *testing.T) { + trueHandler := HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/true", r.URL.Path) + }) + falseHandler := HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { + assert.NotEqual(t, "/true", r.URL.Path) + }) + ctx := context.WithValue(context.Background(), contextKey, "value") + xh := If( + func(ctx context.Context, w http.ResponseWriter, r *http.Request) bool { + return r.URL.Path == "/true" + }, + func(next HandlerC) HandlerC { + return trueHandler + }, + )(falseHandler) + h := New(ctx, xh) + r, _ := http.NewRequest("GET", "http://example.com/true", nil) + h.ServeHTTP(nil, r) + r, _ = http.NewRequest("GET", "http://example.com/false", nil) + h.ServeHTTP(nil, r) +} diff --git a/src/dict2rest/vendor/github.com/rs/xhandler/xhandler.go b/src/dict2rest/vendor/github.com/rs/xhandler/xhandler.go new file mode 100644 index 0000000..0dfcddf --- /dev/null +++ b/src/dict2rest/vendor/github.com/rs/xhandler/xhandler.go @@ -0,0 +1,42 @@ +// Package xhandler provides a bridge between http.Handler and net/context. +// +// xhandler enforces net/context in your handlers without sacrificing +// compatibility with existing http.Handlers nor imposing a specific router. +// +// Thanks to net/context deadline management, xhandler is able to enforce +// a per request deadline and will cancel the context in when the client close +// the connection unexpectedly. +// +// You may create net/context aware middlewares pretty much the same way as +// you would do with http.Handler. +package xhandler // import "github.com/rs/xhandler" + +import ( + "net/http" + + "golang.org/x/net/context" +) + +// HandlerC is a net/context aware http.Handler +type HandlerC interface { + ServeHTTPC(context.Context, http.ResponseWriter, *http.Request) +} + +// HandlerFuncC type is an adapter to allow the use of ordinary functions +// as a xhandler.Handler. If f is a function with the appropriate signature, +// xhandler.HandlerFuncC(f) is a xhandler.Handler object that calls f. +type HandlerFuncC func(context.Context, http.ResponseWriter, *http.Request) + +// ServeHTTPC calls f(ctx, w, r). +func (f HandlerFuncC) ServeHTTPC(ctx context.Context, w http.ResponseWriter, r *http.Request) { + 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) + }) +} diff --git a/src/dict2rest/vendor/github.com/rs/xhandler/xhandler_example_test.go b/src/dict2rest/vendor/github.com/rs/xhandler/xhandler_example_test.go new file mode 100644 index 0000000..9f6f8a5 --- /dev/null +++ b/src/dict2rest/vendor/github.com/rs/xhandler/xhandler_example_test.go @@ -0,0 +1,67 @@ +package xhandler_test + +import ( + "log" + "net/http" + "time" + + "github.com/rs/xhandler" + "golang.org/x/net/context" +) + +type key int + +const contextKey key = 0 + +func newContext(ctx context.Context, value string) context.Context { + return context.WithValue(ctx, contextKey, value) +} + +func fromContext(ctx context.Context) (string, bool) { + value, ok := ctx.Value(contextKey).(string) + return value, ok +} + +func ExampleHandle() { + var xh xhandler.HandlerC + xh = xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { + value, _ := fromContext(ctx) + w.Write([]byte("Hello " + value)) + }) + + xh = (func(next xhandler.HandlerC) xhandler.HandlerC { + return xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { + ctx = newContext(ctx, "World") + next.ServeHTTPC(ctx, w, r) + }) + })(xh) + + ctx := context.Background() + // Bridge context aware handlers with http.Handler using xhandler.Handle() + http.Handle("/", xhandler.New(ctx, xh)) + + if err := http.ListenAndServe(":8080", nil); err != nil { + log.Fatal(err) + } +} + +func ExampleHandleTimeout() { + var xh xhandler.HandlerC + xh = xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { + w.Write([]byte("Hello World")) + if _, ok := ctx.Deadline(); ok { + w.Write([]byte(" with deadline")) + } + }) + + // This handler adds a timeout to the handler + xh = xhandler.TimeoutHandler(5 * time.Second)(xh) + + ctx := context.Background() + // Bridge context aware handlers with http.Handler using xhandler.Handle() + http.Handle("/", xhandler.New(ctx, xh)) + + if err := http.ListenAndServe(":8080", nil); err != nil { + log.Fatal(err) + } +} diff --git a/src/dict2rest/vendor/github.com/rs/xhandler/xhandler_test.go b/src/dict2rest/vendor/github.com/rs/xhandler/xhandler_test.go new file mode 100644 index 0000000..3f5021f --- /dev/null +++ b/src/dict2rest/vendor/github.com/rs/xhandler/xhandler_test.go @@ -0,0 +1,61 @@ +package xhandler + +import ( + "log" + "net/http" + "net/http/httptest" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "golang.org/x/net/context" +) + +type handler struct{} + +type key int + +const contextKey key = 0 + +func newContext(ctx context.Context, value string) context.Context { + return context.WithValue(ctx, contextKey, value) +} + +func fromContext(ctx context.Context) (string, bool) { + value, ok := ctx.Value(contextKey).(string) + return value, ok +} + +func (h handler) ServeHTTPC(ctx context.Context, w http.ResponseWriter, r *http.Request) { + // Leave other go routines a chance to run + time.Sleep(time.Nanosecond) + value, _ := fromContext(ctx) + if _, ok := ctx.Deadline(); ok { + value += " with deadline" + } + if ctx.Err() == context.Canceled { + value += " canceled" + } + w.Write([]byte(value)) +} + +func TestHandle(t *testing.T) { + ctx := context.WithValue(context.Background(), contextKey, "value") + h := New(ctx, &handler{}) + w := httptest.NewRecorder() + r, err := http.NewRequest("GET", "http://example.com/foo", nil) + if err != nil { + log.Fatal(err) + } + h.ServeHTTP(w, r) + assert.Equal(t, "value", w.Body.String()) +} + +func TestHandlerFunc(t *testing.T) { + ok := false + xh := HandlerFuncC(func(context.Context, http.ResponseWriter, *http.Request) { + ok = true + }) + xh.ServeHTTPC(nil, nil, nil) + assert.True(t, ok) +} diff --git a/src/dict2rest/vendor/github.com/rs/xhandler/xmux/README.md b/src/dict2rest/vendor/github.com/rs/xhandler/xmux/README.md new file mode 100644 index 0000000..6ea3c0f --- /dev/null +++ b/src/dict2rest/vendor/github.com/rs/xhandler/xmux/README.md @@ -0,0 +1 @@ +Xmux did move to it's [own repository](https://github.com/rs/xmux/blob/master/README.md) |
