From d8255382da1cb1a861864977947842e5da2e163c Mon Sep 17 00:00:00 2001 From: James Hillyerd Date: Sun, 8 Jan 2017 04:25:50 +0000 Subject: [PATCH] Start of V1 REST client for #43 - List mailbox contents - Get message --- rest/client/apiv1_client.go | 43 +++++++++++++++++++++++++ rest/client/apiv1_client_test.go | 55 ++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 rest/client/apiv1_client.go create mode 100644 rest/client/apiv1_client_test.go diff --git a/rest/client/apiv1_client.go b/rest/client/apiv1_client.go new file mode 100644 index 0000000..cdd34cf --- /dev/null +++ b/rest/client/apiv1_client.go @@ -0,0 +1,43 @@ +package client + +import ( + "net/http" + "net/url" + + "github.com/jhillyerd/inbucket/rest/model" +) + +// ClientV1 accesses the Inbucket REST API v1 +type ClientV1 struct { + restClient +} + +// NewV1 creates a new v1 REST API client given the base URL of an Inbucket server, ex: +// "http://localhost:9000" +func NewV1(baseURL string) (*ClientV1, error) { + parsedURL, err := url.Parse(baseURL) + if err != nil { + return nil, err + } + c := &ClientV1{ + restClient{ + client: &http.Client{}, + baseURL: parsedURL, + }, + } + return c, nil +} + +// ListMailbox returns a list of messages for the requested mailbox +func (c *ClientV1) ListMailbox(name string) (headers []*model.JSONMessageHeaderV1, err error) { + uri := "/api/v1/mailbox/" + url.QueryEscape(name) + err = c.doGet(uri, &headers) + return +} + +// GetMessage returns the message details given a mailbox name and message ID. +func (c *ClientV1) GetMessage(name, id string) (message *model.JSONMessageV1, err error) { + uri := "/api/v1/mailbox/" + url.QueryEscape(name) + "/" + id + err = c.doGet(uri, &message) + return +} diff --git a/rest/client/apiv1_client_test.go b/rest/client/apiv1_client_test.go new file mode 100644 index 0000000..85a7e5c --- /dev/null +++ b/rest/client/apiv1_client_test.go @@ -0,0 +1,55 @@ +package client + +import "testing" + +func TestClientV1ListMailbox(t *testing.T) { + var want, got string + + c, err := NewV1(baseURLStr) + if err != nil { + t.Fatal(err) + } + mth := &mockHTTPClient{} + c.client = mth + + // Method under test + c.ListMailbox("testbox") + + want = "GET" + got = mth.req.Method + if got != want { + t.Errorf("req.Method == %q, want %q", got, want) + } + + want = baseURLStr + "/api/v1/mailbox/testbox" + got = mth.req.URL.String() + if got != want { + t.Errorf("req.URL == %q, want %q", got, want) + } +} + +func TestClientV1GetMessage(t *testing.T) { + var want, got string + + c, err := NewV1(baseURLStr) + if err != nil { + t.Fatal(err) + } + mth := &mockHTTPClient{} + c.client = mth + + // Method under test + c.GetMessage("testbox", "20170107T224128-0000") + + want = "GET" + got = mth.req.Method + if got != want { + t.Errorf("req.Method == %q, want %q", got, want) + } + + want = baseURLStr + "/api/v1/mailbox/testbox/20170107T224128-0000" + got = mth.req.URL.String() + if got != want { + t.Errorf("req.URL == %q, want %q", got, want) + } +}