Documentation ¶
Overview ¶
Package testdataclient provides a test implementation for the DataClient interface of the skipper/routing package.
It uses in-memory route definitions that are passed in on construction, and can upserted/deleted programmatically.
Example ¶
package main import ( "fmt" "log" "net/http" "net/url" "time" "github.com/zalando/skipper/eskip" "github.com/zalando/skipper/logging/loggingtest" "github.com/zalando/skipper/routing" "github.com/zalando/skipper/routing/testdataclient" ) func main() { // 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 settings 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
Index ¶
- type Client
- func (c *Client) Close()
- func (c *Client) FailNext()
- func (c *Client) LoadAll() ([]*eskip.Route, error)
- func (c *Client) LoadUpdate() ([]*eskip.Route, []string, error)
- func (c *Client) Update(upsert []*eskip.Route, deletedIDs []string)
- func (c *Client) UpdateDoc(upsertDoc string, deletedIDs []string) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
DataClient implementation.
func New ¶
Creates a Client with an initial set of route definitions.
Example ¶
package main import ( "github.com/zalando/skipper/eskip" "github.com/zalando/skipper/routing/testdataclient" ) func main() { testdataclient.New([]*eskip.Route{ {Path: "/some/path", Backend: "https://www.example.org"}}) }
Output:
func NewDoc ¶
Creates a Client with an initial set of route definitions in eskip format. If parsing the eskip document fails, returns an error.
Example ¶
package main import ( "log" "github.com/zalando/skipper/routing/testdataclient" ) func main() { dc, err := testdataclient.NewDoc(`Path("/some/path") -> "https://www.example.org"`) if err != nil || dc == nil { log.Fatal(err, dc == nil) } }
Output:
func (*Client) FailNext ¶
func (c *Client) FailNext()
Sets the Client to fail on the next call to LoadAll or LoadUpdate. Repeated call to FailNext will result the Client to fail as many times as it was called.
Example ¶
package main import ( "fmt" "log" "github.com/zalando/skipper/routing/testdataclient" ) func main() { // create a data client: dc, err := testdataclient.NewDoc(`Path("/some/path") -> "https://www.example.org"`) if err != nil || dc == nil { log.Fatal(err, dc == nil) } // set the the next two requests to fail: dc.FailNext() dc.FailNext() // wait for the third request to succeed: _, err = dc.LoadAll() fmt.Println(err) _, err = dc.LoadAll() fmt.Println(err) routes, err := dc.LoadAll() if err != nil || len(routes) != 1 { log.Fatal(err, len(routes)) } fmt.Println(routes[0].Backend) }
Output: failed to get routes failed to get routes https://www.example.org
func (*Client) LoadUpdate ¶
Returns the route definitions upserted/deleted since the last call to LoadAll.