Moved to google.golang.org/genproto/googleapis/api/annotations

Fixes #52
This commit is contained in:
Valerio Gheri
2017-03-31 18:01:58 +02:00
parent 024c5a4e4e
commit c40779224f
2037 changed files with 831329 additions and 1854 deletions

4
vendor/github.com/go-kit/kit/transport/httprp/doc.go generated vendored Normal file
View File

@@ -0,0 +1,4 @@
// Package httprp provides an HTTP reverse-proxy transport. HTTP handlers that
// need to proxy requests to another HTTP service can do so with this package by
// specifying the URL to forward the request to.
package httprp

View File

@@ -0,0 +1,57 @@
package httprp
import (
"context"
"net/http"
"net/http/httputil"
"net/url"
)
// RequestFunc may take information from an HTTP request and put it into a
// request context. BeforeFuncs are executed prior to invoking the
// endpoint.
type RequestFunc func(context.Context, *http.Request) context.Context
// Server is a proxying request handler.
type Server struct {
proxy http.Handler
before []RequestFunc
errorEncoder func(w http.ResponseWriter, err error)
}
// NewServer constructs a new server that implements http.Server and will proxy
// requests to the given base URL using its scheme, host, and base path.
// If the target's path is "/base" and the incoming request was for "/dir",
// the target request will be for /base/dir.
func NewServer(
baseURL *url.URL,
options ...ServerOption,
) *Server {
s := &Server{
proxy: httputil.NewSingleHostReverseProxy(baseURL),
}
for _, option := range options {
option(s)
}
return s
}
// ServerOption sets an optional parameter for servers.
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...) }
}
// ServeHTTP implements http.Handler.
func (s Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
for _, f := range s.before {
ctx = f(ctx, r)
}
s.proxy.ServeHTTP(w, r)
}

View File

@@ -0,0 +1,158 @@
package httprp_test
import (
"context"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"testing"
httptransport "github.com/go-kit/kit/transport/httprp"
)
func TestServerHappyPathSingleServer(t *testing.T) {
originServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("hey"))
}))
defer originServer.Close()
originURL, _ := url.Parse(originServer.URL)
handler := httptransport.NewServer(
originURL,
)
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)
}
}
func TestServerHappyPathSingleServerWithServerOptions(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
}),
)
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)
}
}
func TestServerOriginServerNotFoundResponse(t *testing.T) {
originServer := httptest.NewServer(http.NotFoundHandler())
defer originServer.Close()
originURL, _ := url.Parse(originServer.URL)
handler := httptransport.NewServer(
originURL,
)
proxyServer := httptest.NewServer(handler)
defer proxyServer.Close()
resp, _ := http.Get(proxyServer.URL)
if want, have := http.StatusNotFound, resp.StatusCode; want != have {
t.Errorf("want %d, have %d", want, have)
}
}
func TestServerOriginServerUnreachable(t *testing.T) {
// create a server, then promptly shut it down
originServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
originURL, _ := url.Parse(originServer.URL)
originServer.Close()
handler := httptransport.NewServer(
originURL,
)
proxyServer := httptest.NewServer(handler)
defer proxyServer.Close()
resp, _ := http.Get(proxyServer.URL)
switch resp.StatusCode {
case http.StatusBadGateway: // go1.7 and beyond
break
case http.StatusInternalServerError: // to go1.7
break
default:
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)
}
}