glide up
This commit is contained in:
10
vendor/github.com/go-kit/kit/transport/httprp/server.go
generated
vendored
10
vendor/github.com/go-kit/kit/transport/httprp/server.go
generated
vendored
@@ -1,10 +1,11 @@
|
||||
package httprp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// RequestFunc may take information from an HTTP request and put it into a
|
||||
@@ -14,6 +15,7 @@ type RequestFunc func(context.Context, *http.Request) context.Context
|
||||
|
||||
// Server is a proxying request handler.
|
||||
type Server struct {
|
||||
ctx context.Context
|
||||
proxy http.Handler
|
||||
before []RequestFunc
|
||||
errorEncoder func(w http.ResponseWriter, err error)
|
||||
@@ -24,10 +26,12 @@ type Server struct {
|
||||
// If the target's path is "/base" and the incoming request was for "/dir",
|
||||
// the target request will be for /base/dir.
|
||||
func NewServer(
|
||||
ctx context.Context,
|
||||
baseURL *url.URL,
|
||||
options ...ServerOption,
|
||||
) *Server {
|
||||
s := &Server{
|
||||
ctx: ctx,
|
||||
proxy: httputil.NewSingleHostReverseProxy(baseURL),
|
||||
}
|
||||
for _, option := range options {
|
||||
@@ -42,12 +46,12 @@ type ServerOption func(*Server)
|
||||
// ServerBefore functions are executed on the HTTP request object before the
|
||||
// request is decoded.
|
||||
func ServerBefore(before ...RequestFunc) ServerOption {
|
||||
return func(s *Server) { s.before = append(s.before, before...) }
|
||||
return func(s *Server) { s.before = before }
|
||||
}
|
||||
|
||||
// ServeHTTP implements http.Handler.
|
||||
func (s Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
ctx := s.ctx
|
||||
|
||||
for _, f := range s.before {
|
||||
ctx = f(ctx, r)
|
||||
|
48
vendor/github.com/go-kit/kit/transport/httprp/server_test.go
generated
vendored
48
vendor/github.com/go-kit/kit/transport/httprp/server_test.go
generated
vendored
@@ -1,13 +1,14 @@
|
||||
package httprp_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
httptransport "github.com/go-kit/kit/transport/httprp"
|
||||
)
|
||||
|
||||
@@ -20,6 +21,7 @@ func TestServerHappyPathSingleServer(t *testing.T) {
|
||||
originURL, _ := url.Parse(originServer.URL)
|
||||
|
||||
handler := httptransport.NewServer(
|
||||
context.Background(),
|
||||
originURL,
|
||||
)
|
||||
proxyServer := httptest.NewServer(handler)
|
||||
@@ -54,6 +56,7 @@ func TestServerHappyPathSingleServerWithServerOptions(t *testing.T) {
|
||||
originURL, _ := url.Parse(originServer.URL)
|
||||
|
||||
handler := httptransport.NewServer(
|
||||
context.Background(),
|
||||
originURL,
|
||||
httptransport.ServerBefore(func(ctx context.Context, r *http.Request) context.Context {
|
||||
r.Header.Add(headerKey, headerVal)
|
||||
@@ -80,6 +83,7 @@ func TestServerOriginServerNotFoundResponse(t *testing.T) {
|
||||
originURL, _ := url.Parse(originServer.URL)
|
||||
|
||||
handler := httptransport.NewServer(
|
||||
context.Background(),
|
||||
originURL,
|
||||
)
|
||||
proxyServer := httptest.NewServer(handler)
|
||||
@@ -100,6 +104,7 @@ func TestServerOriginServerUnreachable(t *testing.T) {
|
||||
originServer.Close()
|
||||
|
||||
handler := httptransport.NewServer(
|
||||
context.Background(),
|
||||
originURL,
|
||||
)
|
||||
proxyServer := httptest.NewServer(handler)
|
||||
@@ -115,44 +120,3 @@ func TestServerOriginServerUnreachable(t *testing.T) {
|
||||
t.Errorf("want %d or %d, have %d", http.StatusBadGateway, http.StatusInternalServerError, resp.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMultipleServerBefore(t *testing.T) {
|
||||
const (
|
||||
headerKey = "X-TEST-HEADER"
|
||||
headerVal = "go-kit-proxy"
|
||||
)
|
||||
|
||||
originServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if want, have := headerVal, r.Header.Get(headerKey); want != have {
|
||||
t.Errorf("want %q, have %q", want, have)
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("hey"))
|
||||
}))
|
||||
defer originServer.Close()
|
||||
originURL, _ := url.Parse(originServer.URL)
|
||||
|
||||
handler := httptransport.NewServer(
|
||||
originURL,
|
||||
httptransport.ServerBefore(func(ctx context.Context, r *http.Request) context.Context {
|
||||
r.Header.Add(headerKey, headerVal)
|
||||
return ctx
|
||||
}),
|
||||
httptransport.ServerBefore(func(ctx context.Context, r *http.Request) context.Context {
|
||||
return ctx
|
||||
}),
|
||||
)
|
||||
proxyServer := httptest.NewServer(handler)
|
||||
defer proxyServer.Close()
|
||||
|
||||
resp, _ := http.Get(proxyServer.URL)
|
||||
if want, have := http.StatusOK, resp.StatusCode; want != have {
|
||||
t.Errorf("want %d, have %d", want, have)
|
||||
}
|
||||
|
||||
responseBody, _ := ioutil.ReadAll(resp.Body)
|
||||
if want, have := "hey", string(responseBody); want != have {
|
||||
t.Errorf("want %q, have %q", want, have)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user