mirror of
https://github.com/restic/rest-server.git
synced 2025-12-07 09:36:13 -08:00
Vendor dependencies
This commit is contained in:
committed by
Zlatko Čalušić
parent
2f0a16d8b7
commit
6054876201
60
vendor/goji.io/middleware/middleware.go
generated
vendored
Normal file
60
vendor/goji.io/middleware/middleware.go
generated
vendored
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
Package middleware contains utilities for Goji Middleware authors.
|
||||
|
||||
Unless you are writing middleware for your application, you should avoid
|
||||
importing this package. Instead, use the abstractions provided by your
|
||||
middleware package.
|
||||
*/
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"goji.io"
|
||||
"goji.io/internal"
|
||||
)
|
||||
|
||||
/*
|
||||
Pattern returns the most recently matched Pattern, or nil if no pattern was
|
||||
matched.
|
||||
*/
|
||||
func Pattern(ctx context.Context) goji.Pattern {
|
||||
p := ctx.Value(internal.Pattern)
|
||||
if p == nil {
|
||||
return nil
|
||||
}
|
||||
return p.(goji.Pattern)
|
||||
}
|
||||
|
||||
/*
|
||||
SetPattern returns a new context in which the given Pattern is used as the most
|
||||
recently matched pattern.
|
||||
*/
|
||||
func SetPattern(ctx context.Context, p goji.Pattern) context.Context {
|
||||
return context.WithValue(ctx, internal.Pattern, p)
|
||||
}
|
||||
|
||||
/*
|
||||
Handler returns the handler corresponding to the most recently matched Pattern,
|
||||
or nil if no pattern was matched.
|
||||
|
||||
The handler returned by this function is the one that will be dispatched to at
|
||||
the end of the middleware stack. If the returned Handler is nil, http.NotFound
|
||||
will be used instead.
|
||||
*/
|
||||
func Handler(ctx context.Context) http.Handler {
|
||||
h := ctx.Value(internal.Handler)
|
||||
if h == nil {
|
||||
return nil
|
||||
}
|
||||
return h.(http.Handler)
|
||||
}
|
||||
|
||||
/*
|
||||
SetHandler returns a new context in which the given Handler was most recently
|
||||
matched and which consequently will be dispatched to.
|
||||
*/
|
||||
func SetHandler(ctx context.Context, h http.Handler) context.Context {
|
||||
return context.WithValue(ctx, internal.Handler, h)
|
||||
}
|
||||
48
vendor/goji.io/middleware/middleware_test.go
generated
vendored
Normal file
48
vendor/goji.io/middleware/middleware_test.go
generated
vendored
Normal file
@@ -0,0 +1,48 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type testPattern bool
|
||||
|
||||
func (t testPattern) Match(r *http.Request) *http.Request {
|
||||
if t {
|
||||
return r
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type testHandler struct{}
|
||||
|
||||
func (t testHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {}
|
||||
|
||||
func TestPattern(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
pat := testPattern(true)
|
||||
ctx := SetPattern(context.Background(), pat)
|
||||
if pat2 := Pattern(ctx); pat2 != pat {
|
||||
t.Errorf("got ctx=%v, expected %v", pat2, pat)
|
||||
}
|
||||
|
||||
if pat2 := Pattern(context.Background()); pat2 != nil {
|
||||
t.Errorf("got ctx=%v, expecte nil", pat2)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandler(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
h := testHandler{}
|
||||
ctx := SetHandler(context.Background(), h)
|
||||
if h2 := Handler(ctx); h2 != h {
|
||||
t.Errorf("got handler=%v, expected %v", h2, h)
|
||||
}
|
||||
|
||||
if h2 := Handler(context.Background()); h2 != nil {
|
||||
t.Errorf("got handler=%v, expected nil", h2)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user