From 604569c7a39c54fc927b31fac951c18b93b01081 Mon Sep 17 00:00:00 2001 From: pancho horrillo Date: Thu, 3 Oct 2019 22:29:47 +0200 Subject: [PATCH] Add internal/client/route_list{,_test}.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Héctor Hurtado --- internal/client/route_list.go | 13 ++++++++ internal/client/route_list_test.go | 49 ++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 internal/client/route_list.go create mode 100644 internal/client/route_list_test.go diff --git a/internal/client/route_list.go b/internal/client/route_list.go new file mode 100644 index 0000000..629966b --- /dev/null +++ b/internal/client/route_list.go @@ -0,0 +1,13 @@ +package client + +import ( + "io" + + "github.com/BBVA/kapow/internal/http" +) + +// ListRoutes list the routes registered on the kapow! instance +func ListRoutes(host string, w io.Writer) error { + url := host + "/routes/" + return http.Get(url, "application/json", nil, w) +} diff --git a/internal/client/route_list_test.go b/internal/client/route_list_test.go new file mode 100644 index 0000000..f798bac --- /dev/null +++ b/internal/client/route_list_test.go @@ -0,0 +1,49 @@ +package client + +import ( + "bytes" + "fmt" + "net/http" + "testing" + + gock "gopkg.in/h2non/gock.v1" +) + +const ( + host = "http://localhost:8080" +) + +func TestListRoutesEmpty(t *testing.T) { + descr := fmt.Sprintf("ListRoutes(%q, nil)", host) + + defer gock.Off() + gock.New(host).Get("/routes").MatchType("json").Reply(http.StatusOK) + + err := ListRoutes(host, nil) + + if err != nil { + t.Errorf("%s: unexpected error %q", descr, err) + } +} + +func TestListRoutesSome(t *testing.T) { + descr := fmt.Sprintf("ListRoutes(%q, buf)", host) + + const want = "JSON array of some routes..." + + defer gock.Off() + gock.New(host). + Get("/routes"). + MatchType("json"). + Reply(http.StatusOK). + BodyString(want) + + buf := new(bytes.Buffer) + err := ListRoutes(host, buf) + + if err != nil { + t.Errorf("%s: unexpected error: %q", descr, err) + } else if got := buf.String(); got != want { + t.Errorf("%s: got %q, expected %q", descr, buf, want) + } +}