本文整理汇总了Golang中github.com/zalando/skipper/routing.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了New函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestProcessesRequestWithShuntBackend
func TestProcessesRequestWithShuntBackend(t *testing.T) {
u, _ := url.ParseRequestURI("https://www.example.org/hello")
r := &http.Request{
URL: u,
Method: "GET",
Header: http.Header{"X-Test-Header": []string{"test value"}}}
w := httptest.NewRecorder()
fr := make(filters.Registry)
fr.Register(builtin.NewResponseHeader())
doc := `hello: Path("/hello") -> responseHeader("X-Test-Response-Header", "response header value") -> <shunt>`
dc, err := testdataclient.NewDoc(doc)
if err != nil {
t.Error(err)
}
p := New(routing.New(routing.Options{
fr,
routing.MatchingOptionsNone,
sourcePollTimeout,
[]routing.DataClient{dc},
0}), OptionsNone)
delay()
p.ServeHTTP(w, r)
if h, ok := w.Header()["X-Test-Response-Header"]; !ok || h[0] != "response header value" {
t.Error("wrong response header")
}
}
示例2: Example
func Example() {
// create registry
registry := builtin.MakeRegistry()
// create and register the filter specification
spec := &customSpec{name: "customFilter"}
registry.Register(spec)
// create simple data client, with route entries referencing 'customFilter',
// and clipping part of the request path:
dataClient, err := testdataclient.NewDoc(`
ui: Path("/ui/*page") ->
customFilter("ui request") ->
modPath("^/[^/]*", "") ->
"https://ui.example.org";
api: Path("/api/*resource") ->
customFilter("api request") ->
modPath("^/[^/]*", "") ->
"https://api.example.org"`)
if err != nil {
log.Fatal(err)
}
// create http.Handler:
proxy.New(
routing.New(routing.Options{
FilterRegistry: registry,
DataClients: []routing.DataClient{dataClient}}),
proxy.OptionsNone)
}
示例3: TestReceivesDelete
func TestReceivesDelete(t *testing.T) {
dc := testdataclient.New([]*eskip.Route{
{Id: "route1", Path: "/some-path", Backend: "https://www.example.org"},
{Id: "route2", Path: "/some-other", Backend: "https://other.example.org"}})
rt := routing.New(routing.Options{
UpdateBuffer: 0,
DataClients: []routing.DataClient{dc},
PollTimeout: pollTimeout})
req, err := http.NewRequest("GET", "https://www.example.com/some-path", nil)
if err != nil {
t.Error(err)
}
<-waitRoute(rt, req)
<-waitUpdate(dc, nil, []string{"route1"}, false)
time.Sleep(6 * pollTimeout)
req, err = http.NewRequest("GET", "https://www.example.com/some-path", nil)
if err != nil {
t.Error(err)
}
if waitDone(0, waitRoute(rt, req)) {
t.Error("should not have found route")
}
}
示例4: TestStreaming
func TestStreaming(t *testing.T) {
const expectedParts = 3
payload := []byte("some data to stream")
s := startTestServer(payload, expectedParts, voidCheck)
defer s.Close()
doc := fmt.Sprintf(`hello: Path("/hello") -> "%s"`, s.URL)
dc, err := testdataclient.NewDoc(doc)
if err != nil {
t.Error(err)
}
p := New(routing.New(routing.Options{
nil,
routing.MatchingOptionsNone,
sourcePollTimeout,
[]routing.DataClient{dc},
0}), OptionsNone)
delay()
u, _ := url.ParseRequestURI("https://www.example.org/hello")
r := &http.Request{
URL: u,
Method: "GET"}
w := httptest.NewRecorder()
parts := 0
total := 0
done := make(chan int)
go p.ServeHTTP(w, r)
go func() {
for {
buf := w.Body.Bytes()
if len(buf) == 0 {
time.Sleep(streamingDelay)
continue
}
parts++
total += len(buf)
if total >= len(payload) {
close(done)
return
}
}
}()
select {
case <-done:
if parts <= expectedParts {
t.Error("streaming failed", parts)
}
case <-time.After(150 * time.Millisecond):
t.Error("streaming timeout")
}
}
示例5: TestMergesMultipleSources
func TestMergesMultipleSources(t *testing.T) {
if testing.Short() {
t.Skip()
}
dc1 := testdataclient.New([]*eskip.Route{{Id: "route1", Path: "/some-path", Backend: "https://www.example.org"}})
dc2 := testdataclient.New([]*eskip.Route{{Id: "route2", Path: "/some-other", Backend: "https://other.example.org"}})
dc3 := testdataclient.New([]*eskip.Route{{Id: "route3", Path: "/another", Backend: "https://another.example.org"}})
rt := routing.New(routing.Options{
UpdateBuffer: 0,
DataClients: []routing.DataClient{dc1, dc2, dc3},
PollTimeout: pollTimeout})
req1, err := http.NewRequest("GET", "https://www.example.com/some-path", nil)
if err != nil {
t.Error(err)
}
req2, err := http.NewRequest("GET", "https://www.example.com/some-other", nil)
if err != nil {
t.Error(err)
}
req3, err := http.NewRequest("GET", "https://www.example.com/another", nil)
if err != nil {
t.Error(err)
}
if !waitDone(6*pollTimeout,
waitRoute(rt, req1),
waitRoute(rt, req2),
waitRoute(rt, req3)) {
t.Error("test timeout")
}
}
示例6: Example
func Example() {
// create a data client:
dataClient := testdataclient.New([]*eskip.Route{
{Path: "/some/path", Backend: "https://www.example.org"}})
// (only in tests)
tl := loggingtest.New()
defer tl.Close()
// create a router:
r := routing.New(routing.Options{
DataClients: []routing.DataClient{dataClient},
Log: tl})
defer r.Close()
// wait for the route data being propagated:
tl.WaitFor("route settigns applied", time.Second)
// test the router:
route, _ := r.Route(&http.Request{URL: &url.URL{Path: "/some/path"}})
if route == nil {
log.Fatal("failed to route request")
}
fmt.Println(route.Backend)
// Output:
// https://www.example.org
}
示例7: TestMergesUpdatesFromMultipleSources
func TestMergesUpdatesFromMultipleSources(t *testing.T) {
dc1 := testdataclient.New([]*eskip.Route{{Id: "route1", Path: "/some-path", Backend: "https://www.example.org"}})
dc2 := testdataclient.New([]*eskip.Route{{Id: "route2", Path: "/some-other", Backend: "https://other.example.org"}})
dc3 := testdataclient.New([]*eskip.Route{{Id: "route3", Path: "/another", Backend: "https://another.example.org"}})
rt := routing.New(routing.Options{
UpdateBuffer: 0,
DataClients: []routing.DataClient{dc1, dc2, dc3},
PollTimeout: pollTimeout})
req1, err := http.NewRequest("GET", "https://www.example.com/some-path", nil)
if err != nil {
t.Error(err)
}
req2, err := http.NewRequest("GET", "https://www.example.com/some-other", nil)
if err != nil {
t.Error(err)
}
req3, err := http.NewRequest("GET", "https://www.example.com/another", nil)
if err != nil {
t.Error(err)
}
waitRoute(rt, req1)
waitRoute(rt, req2)
waitRoute(rt, req3)
<-waitUpdate(dc1, []*eskip.Route{{Id: "route1", Path: "/some-changed-path", Backend: "https://www.example.org"}}, nil, false)
<-waitUpdate(dc2, []*eskip.Route{{Id: "route2", Path: "/some-other-changed", Backend: "https://www.example.org"}}, nil, false)
<-waitUpdate(dc3, nil, []string{"route3"}, false)
req1, err = http.NewRequest("GET", "https://www.example.com/some-changed-path", nil)
if err != nil {
t.Error(err)
}
req2, err = http.NewRequest("GET", "https://www.example.com/some-other-changed", nil)
if err != nil {
t.Error(err)
}
req3, err = http.NewRequest("GET", "https://www.example.com/another", nil)
if err != nil {
t.Error(err)
}
if !waitDone(6*pollTimeout,
waitRoute(rt, req1),
waitRoute(rt, req2)) {
t.Error("test timeout")
}
time.Sleep(3 * pollTimeout)
if waitDone(0, waitRoute(rt, req3)) {
t.Error("should not have found route")
}
}
示例8: TestProcessesPredicates
func TestProcessesPredicates(t *testing.T) {
dc, err := testdataclient.NewDoc(`
route1: CustomPredicate("custom1") -> "https://route1.example.org";
route2: CustomPredicate("custom2") -> "https://route2.example.org";
catchAll: * -> "https://route.example.org"`)
if err != nil {
t.Error(err)
return
}
cps := []routing.PredicateSpec{&predicate{}, &predicate{}}
rt := routing.New(routing.Options{
DataClients: []routing.DataClient{dc},
PollTimeout: pollTimeout,
Predicates: cps})
req, err := http.NewRequest("GET", "https://www.example.com", nil)
if err != nil {
t.Error(err)
return
}
req.Header.Set(predicateHeader, "custom1")
select {
case r := <-waitRoute(rt, req):
if r.Backend != "https://route1.example.org" {
t.Error("custom predicate matching failed, route1")
return
}
case <-time.After(3 * pollTimeout):
t.Error("test timeout")
}
req.Header.Set(predicateHeader, "custom2")
select {
case r := <-waitRoute(rt, req):
if r.Backend != "https://route2.example.org" {
t.Error("custom predicate matching failed, route2")
return
}
case <-time.After(3 * pollTimeout):
t.Error("test timeout")
}
req.Header.Del(predicateHeader)
select {
case r := <-waitRoute(rt, req):
if r.Backend != "https://route.example.org" {
t.Error("custom predicate matching failed, catch-all")
return
}
case <-time.After(3 * pollTimeout):
t.Error("test timeout")
}
}
示例9: Example
func Example() {
// create etcd data client:
dataClient := etcd.New([]string{"https://etcd.example.org"}, "/skipper")
// create http.Handler:
proxy.New(
routing.New(routing.Options{
FilterRegistry: builtin.MakeRegistry(),
DataClients: []routing.DataClient{dataClient}}),
proxy.OptionsNone)
}
示例10: TestRoute
func TestRoute(t *testing.T) {
payload1 := []byte("host one")
s1 := startTestServer(payload1, 0, voidCheck)
defer s1.Close()
payload2 := []byte("host two")
s2 := startTestServer(payload2, 0, voidCheck)
defer s2.Close()
doc := fmt.Sprintf(`
route1: Path("/host-one/*any") -> "%s";
route2: Path("/host-two/*any") -> "%s"
`, s1.URL, s2.URL)
dc, err := testdataclient.NewDoc(doc)
if err != nil {
t.Error(err)
}
p := New(routing.New(routing.Options{
nil,
routing.MatchingOptionsNone,
sourcePollTimeout,
[]routing.DataClient{dc},
nil,
0}), OptionsNone)
delay()
var (
r *http.Request
w *httptest.ResponseRecorder
u *url.URL
)
u, _ = url.ParseRequestURI("https://www.example.org/host-one/some/path")
r = &http.Request{
URL: u,
Method: "GET"}
w = httptest.NewRecorder()
p.ServeHTTP(w, r)
if w.Code != http.StatusOK || !bytes.Equal(w.Body.Bytes(), payload1) {
t.Error("wrong routing 1")
}
u, _ = url.ParseRequestURI("https://www.example.org/host-two/some/path")
r = &http.Request{
URL: u,
Method: "GET"}
w = httptest.NewRecorder()
p.ServeHTTP(w, r)
if w.Code != http.StatusOK || !bytes.Equal(w.Body.Bytes(), payload2) {
t.Error("wrong routing 2")
}
}
示例11: newTestRoutingWithFiltersPredicates
func newTestRoutingWithFiltersPredicates(fr filters.Registry, cps []routing.PredicateSpec, dc ...routing.DataClient) (*testRouting, error) {
tl := loggingtest.New()
rt := routing.New(routing.Options{
FilterRegistry: fr,
Predicates: cps,
DataClients: dc,
PollTimeout: pollTimeout,
Log: tl})
tr := &testRouting{tl, rt}
return tr, tr.waitForNRouteSettings(len(dc))
}
示例12: Run
// Run skipper.
func Run(o Options) error {
// create authentication for Innkeeper
auth := createInnkeeperAuthentication(o)
// create data client
dataClients, err := createDataClients(o, auth)
if err != nil {
return err
}
if len(dataClients) == 0 {
log.Println("warning: no route source specified")
}
// create a filter registry with the available filter specs registered,
// and register the custom filters
registry := builtin.MakeRegistry()
for _, f := range o.CustomFilters {
registry.Register(f)
}
// create routing
// create the proxy instance
var mo routing.MatchingOptions
if o.IgnoreTrailingSlash {
mo = routing.IgnoreTrailingSlash
}
// ensure a non-zero poll timeout
if o.SourcePollTimeout <= 0 {
o.SourcePollTimeout = defaultSourcePollTimeout
}
// check for dev mode, and set update buffer of the routes
updateBuffer := defaultRoutingUpdateBuffer
if o.DevMode {
updateBuffer = 0
}
// create a routing engine
routing := routing.New(routing.Options{
registry,
mo,
o.SourcePollTimeout,
dataClients,
updateBuffer})
// create the proxy
proxy := proxy.New(routing, o.ProxyOptions, o.PriorityRoutes...)
// start the http server
log.Printf("listening on %v\n", o.Address)
return http.ListenAndServe(o.Address, proxy)
}
示例13: DisabledExample
func DisabledExample() {
// create a target backend server. It will return the value of the 'X-Echo' request header
// as the response body:
targetServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(r.Header.Get("X-Echo")))
}))
defer targetServer.Close()
// create a filter registry, and register the custom filter:
filterRegistry := builtin.MakeRegistry()
filterRegistry.Register(&setEchoHeader{})
// create a data client with a predefined route, referencing the filter and a path condition
// containing a wildcard called 'echo':
routeDoc := fmt.Sprintf(`Path("/return/:echo") -> setEchoHeader() -> "%s"`, targetServer.URL)
dataClient, err := testdataclient.NewDoc(routeDoc)
if err != nil {
log.Fatal(err)
}
// create routing object:
rt := routing.New(routing.Options{
FilterRegistry: filterRegistry,
DataClients: []routing.DataClient{dataClient}})
defer rt.Close()
// create a proxy instance, and start an http server:
proxy := proxy.New(rt, proxy.OptionsNone)
defer proxy.Close()
router := httptest.NewServer(proxy)
defer router.Close()
// make a request to the proxy:
rsp, err := http.Get(fmt.Sprintf("%s/return/Hello,+world!", router.URL))
if err != nil {
log.Fatal(err)
}
defer rsp.Body.Close()
// print out the response:
if _, err := io.Copy(os.Stdout, rsp.Body); err != nil {
log.Fatal(err)
}
// Output:
// Hello, world!
}
示例14: TestFlusherImplementation
func TestFlusherImplementation(t *testing.T) {
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, "))
time.Sleep(15 * time.Millisecond)
w.Write([]byte("world!"))
})
ts := httptest.NewServer(h)
defer ts.Close()
doc := fmt.Sprintf(`* -> "%s"`, ts.URL)
dc, err := testdataclient.NewDoc(doc)
if err != nil {
t.Error(err)
}
p := New(routing.New(routing.Options{
nil,
routing.MatchingOptionsNone,
sourcePollTimeout,
[]routing.DataClient{dc},
nil,
0}), OptionsNone)
delay()
a := fmt.Sprintf(":%d", 1<<16-rand.Intn(1<<15))
ps := &http.Server{Addr: a, Handler: p}
go ps.ListenAndServe()
// let the server start listening
time.Sleep(15 * time.Millisecond)
rsp, err := http.Get("http://127.0.0.1" + a)
if err != nil {
t.Error(err)
return
}
defer rsp.Body.Close()
b, err := ioutil.ReadAll(rsp.Body)
if err != nil {
t.Error(err)
return
}
if string(b) != "Hello, world!" {
t.Error("failed to receive response")
}
}
示例15: TestAppliesFilters
func TestAppliesFilters(t *testing.T) {
payload := []byte("Hello World!")
s := startTestServer(payload, 0, func(r *http.Request) {
if h, ok := r.Header["X-Test-Request-Header"]; !ok ||
h[0] != "request header value" {
t.Error("request header is missing")
}
})
defer s.Close()
u, _ := url.ParseRequestURI("https://www.example.org/hello")
r := &http.Request{
URL: u,
Method: "GET",
Header: http.Header{"X-Test-Header": []string{"test value"}}}
w := httptest.NewRecorder()
fr := make(filters.Registry)
fr.Register(builtin.NewRequestHeader())
fr.Register(builtin.NewResponseHeader())
doc := fmt.Sprintf(`hello:
Path("/hello") ->
requestHeader("X-Test-Request-Header", "request header value") ->
responseHeader("X-Test-Response-Header", "response header value") ->
"%s"`, s.URL)
dc, err := testdataclient.NewDoc(doc)
if err != nil {
t.Error(err)
}
p := New(routing.New(routing.Options{
fr,
routing.MatchingOptionsNone,
sourcePollTimeout,
[]routing.DataClient{dc},
nil,
0}), OptionsNone)
delay()
p.ServeHTTP(w, r)
if h, ok := w.Header()["X-Test-Response-Header"]; !ok || h[0] != "response header value" {
t.Error("missing response header")
}
}