From 61e9b91637b1fa8c57568f5fe33e13c6f23e6960 Mon Sep 17 00:00:00 2001 From: James Hillyerd Date: Sun, 8 Jan 2017 04:09:27 +0000 Subject: [PATCH 1/3] Generic REST client (HTTP GET only) for #43 --- rest/client/rest.go | 56 +++++++++++++++++++++++++++++ rest/client/rest_test.go | 77 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 rest/client/rest.go create mode 100644 rest/client/rest_test.go diff --git a/rest/client/rest.go b/rest/client/rest.go new file mode 100644 index 0000000..323e260 --- /dev/null +++ b/rest/client/rest.go @@ -0,0 +1,56 @@ +package client + +import ( + "encoding/json" + "fmt" + "net/http" + "net/url" +) + +// httpClient allows http.Client to be mocked for tests +type httpClient interface { + Do(*http.Request) (*http.Response, error) +} + +// Generic REST restClient +type restClient struct { + client httpClient + baseURL *url.URL +} + +// do performs an HTTP request with this client and returns the response +func (c *restClient) do(method, uri string) (*http.Response, error) { + rel, err := url.Parse(uri) + if err != nil { + return nil, err + } + + url := c.baseURL.ResolveReference(rel) + + // Build the request + req, err := http.NewRequest(method, url.String(), nil) + if err != nil { + return nil, err + } + + // Send the request + return c.client.Do(req) +} + +// doGet performs a GET request with this client and marshalls the JSON response into v +func (c *restClient) doGet(uri string, v interface{}) error { + resp, err := c.do("GET", uri) + if err != nil { + return err + } + + defer func() { + _ = resp.Body.Close() + }() + if resp.StatusCode == http.StatusOK { + // Decode response body + return json.NewDecoder(resp.Body).Decode(v) + } + + return fmt.Errorf("Unexpected HTTP response status %v: %s", resp.StatusCode, resp.Status) +} diff --git a/rest/client/rest_test.go b/rest/client/rest_test.go new file mode 100644 index 0000000..29c0eeb --- /dev/null +++ b/rest/client/rest_test.go @@ -0,0 +1,77 @@ +package client + +import ( + "bytes" + "io/ioutil" + "net/http" + "net/url" + "testing" +) + +const baseURLStr = "http://test.local:8080" + +var baseURL *url.URL + +func init() { + var err error + baseURL, err = url.Parse(baseURLStr) + if err != nil { + panic(err) + } +} + +type mockHTTPClient struct { + req *http.Request +} + +func (m *mockHTTPClient) Do(req *http.Request) (resp *http.Response, err error) { + m.req = req + resp = &http.Response{ + Body: ioutil.NopCloser(&bytes.Buffer{}), + } + + return +} + +func TestDo(t *testing.T) { + var want, got string + + mth := &mockHTTPClient{} + c := &restClient{mth, baseURL} + + c.do("POST", "/dopost") + + want = "POST" + got = mth.req.Method + if got != want { + t.Errorf("req.Method == %q, want %q", got, want) + } + + want = baseURLStr + "/dopost" + got = mth.req.URL.String() + if got != want { + t.Errorf("req.URL == %q, want %q", got, want) + } +} + +func TestDoGet(t *testing.T) { + var want, got string + + mth := &mockHTTPClient{} + c := &restClient{mth, baseURL} + + v := new(map[string]interface{}) + c.doGet("/doget", &v) + + want = "GET" + got = mth.req.Method + if got != want { + t.Errorf("req.Method == %q, want %q", got, want) + } + + want = baseURLStr + "/doget" + got = mth.req.URL.String() + if got != want { + t.Errorf("req.URL == %q, want %q", got, want) + } +} From d8255382da1cb1a861864977947842e5da2e163c Mon Sep 17 00:00:00 2001 From: James Hillyerd Date: Sun, 8 Jan 2017 04:25:50 +0000 Subject: [PATCH 2/3] 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) + } +} From c8fd56ca90d1990f16699cb0345b26552de57e91 Mon Sep 17 00:00:00 2001 From: James Hillyerd Date: Sun, 8 Jan 2017 22:11:22 +0000 Subject: [PATCH 3/3] Complete REST client for #43 - Add source, delete and purge --- CHANGELOG.md | 1 + rest/client/apiv1_client.go | 59 +++++++++++++++++++-- rest/client/apiv1_client_test.go | 90 ++++++++++++++++++++++++++++++++ rest/client/rest.go | 14 +++-- rest/client/rest_test.go | 57 +++++++++++++++++--- 5 files changed, 206 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f3c7d1b..28b3292 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - Storage of To: header in messages (likely breaks existing datastores) - Attachment list to [GET message JSON](https://github.com/jhillyerd/inbucket/wiki/REST-GET-message) +- Go client for REST API ### Fixed - No longer run out of file handles when dealing with a large number of diff --git a/rest/client/apiv1_client.go b/rest/client/apiv1_client.go index cdd34cf..5ad5245 100644 --- a/rest/client/apiv1_client.go +++ b/rest/client/apiv1_client.go @@ -1,8 +1,11 @@ package client import ( + "bytes" + "fmt" "net/http" "net/url" + "time" "github.com/jhillyerd/inbucket/rest/model" ) @@ -21,7 +24,9 @@ func NewV1(baseURL string) (*ClientV1, error) { } c := &ClientV1{ restClient{ - client: &http.Client{}, + client: &http.Client{ + Timeout: 30 * time.Second, + }, baseURL: parsedURL, }, } @@ -31,13 +36,61 @@ func NewV1(baseURL string) (*ClientV1, error) { // 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) + err = c.doJSON("GET", 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) + err = c.doJSON("GET", uri, &message) return } + +// GetMessageSource returns the message source given a mailbox name and message ID. +func (c *ClientV1) GetMessageSource(name, id string) (*bytes.Buffer, error) { + uri := "/api/v1/mailbox/" + url.QueryEscape(name) + "/" + id + "/source" + resp, err := c.do("GET", uri) + if err != nil { + return nil, err + } + + defer func() { + _ = resp.Body.Close() + }() + if resp.StatusCode != http.StatusOK { + return nil, + fmt.Errorf("Unexpected HTTP response status %v: %s", resp.StatusCode, resp.Status) + } + buf := new(bytes.Buffer) + _, err = buf.ReadFrom(resp.Body) + return buf, err +} + +// DeleteMessage deletes a single message given the mailbox name and message ID. +func (c *ClientV1) DeleteMessage(name, id string) error { + uri := "/api/v1/mailbox/" + url.QueryEscape(name) + "/" + id + resp, err := c.do("DELETE", uri) + if err != nil { + return err + } + _ = resp.Body.Close() + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("Unexpected HTTP response status %v: %s", resp.StatusCode, resp.Status) + } + return nil +} + +// PurgeMailbox deletes all messages in the given mailbox +func (c *ClientV1) PurgeMailbox(name string) error { + uri := "/api/v1/mailbox/" + url.QueryEscape(name) + resp, err := c.do("DELETE", uri) + if err != nil { + return err + } + _ = resp.Body.Close() + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("Unexpected HTTP response status %v: %s", resp.StatusCode, resp.Status) + } + return nil +} diff --git a/rest/client/apiv1_client_test.go b/rest/client/apiv1_client_test.go index 85a7e5c..1e8ddc7 100644 --- a/rest/client/apiv1_client_test.go +++ b/rest/client/apiv1_client_test.go @@ -53,3 +53,93 @@ func TestClientV1GetMessage(t *testing.T) { t.Errorf("req.URL == %q, want %q", got, want) } } + +func TestClientV1GetMessageSource(t *testing.T) { + var want, got string + + c, err := NewV1(baseURLStr) + if err != nil { + t.Fatal(err) + } + mth := &mockHTTPClient{ + statusCode: 200, + body: "message source", + } + c.client = mth + + // Method under test + source, err := c.GetMessageSource("testbox", "20170107T224128-0000") + if err != nil { + t.Fatal(err) + } + + 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/source" + got = mth.req.URL.String() + if got != want { + t.Errorf("req.URL == %q, want %q", got, want) + } + + want = "message source" + got = source.String() + if got != want { + t.Errorf("Source == %q, want: %q", got, want) + } +} + +func TestClientV1DeleteMessage(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.DeleteMessage("testbox", "20170107T224128-0000") + + want = "DELETE" + 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) + } +} + +func TestClientV1PurgeMailbox(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.PurgeMailbox("testbox") + + want = "DELETE" + 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) + } +} diff --git a/rest/client/rest.go b/rest/client/rest.go index 323e260..1878052 100644 --- a/rest/client/rest.go +++ b/rest/client/rest.go @@ -37,9 +37,9 @@ func (c *restClient) do(method, uri string) (*http.Response, error) { return c.client.Do(req) } -// doGet performs a GET request with this client and marshalls the JSON response into v -func (c *restClient) doGet(uri string, v interface{}) error { - resp, err := c.do("GET", uri) +// doGet performs an HTTP request with this client and marshalls the JSON response into v +func (c *restClient) doJSON(method string, uri string, v interface{}) error { + resp, err := c.do(method, uri) if err != nil { return err } @@ -48,8 +48,12 @@ func (c *restClient) doGet(uri string, v interface{}) error { _ = resp.Body.Close() }() if resp.StatusCode == http.StatusOK { - // Decode response body - return json.NewDecoder(resp.Body).Decode(v) + if v == nil { + return nil + } else { + // Decode response body + return json.NewDecoder(resp.Body).Decode(v) + } } return fmt.Errorf("Unexpected HTTP response status %v: %s", resp.StatusCode, resp.Status) diff --git a/rest/client/rest_test.go b/rest/client/rest_test.go index 29c0eeb..05eb6a7 100644 --- a/rest/client/rest_test.go +++ b/rest/client/rest_test.go @@ -21,13 +21,16 @@ func init() { } type mockHTTPClient struct { - req *http.Request + req *http.Request + statusCode int + body string } func (m *mockHTTPClient) Do(req *http.Request) (resp *http.Response, err error) { m.req = req resp = &http.Response{ - Body: ioutil.NopCloser(&bytes.Buffer{}), + StatusCode: m.statusCode, + Body: ioutil.NopCloser(bytes.NewBufferString(m.body)), } return @@ -39,7 +42,10 @@ func TestDo(t *testing.T) { mth := &mockHTTPClient{} c := &restClient{mth, baseURL} - c.do("POST", "/dopost") + _, err := c.do("POST", "/dopost") + if err != nil { + t.Fatal(err) + } want = "POST" got = mth.req.Method @@ -54,14 +60,51 @@ func TestDo(t *testing.T) { } } -func TestDoGet(t *testing.T) { +func TestDoJSON(t *testing.T) { var want, got string - mth := &mockHTTPClient{} + mth := &mockHTTPClient{ + statusCode: 200, + body: `{"foo": "bar"}`, + } c := &restClient{mth, baseURL} - v := new(map[string]interface{}) - c.doGet("/doget", &v) + var v map[string]interface{} + c.doJSON("GET", "/doget", &v) + + want = "GET" + got = mth.req.Method + if got != want { + t.Errorf("req.Method == %q, want %q", got, want) + } + + want = baseURLStr + "/doget" + got = mth.req.URL.String() + if got != want { + t.Errorf("req.URL == %q, want %q", got, want) + } + + want = "bar" + if val, ok := v["foo"]; ok { + got = val.(string) + if got != want { + t.Errorf("map[foo] == %q, want: %q", got, want) + } + } else { + t.Errorf("Map did not contain key foo, want: %q", want) + } +} + +func TestDoJSONNilV(t *testing.T) { + var want, got string + + mth := &mockHTTPClient{statusCode: 200} + c := &restClient{mth, baseURL} + + err := c.doJSON("GET", "/doget", nil) + if err != nil { + t.Fatal(err) + } want = "GET" got = mth.req.Method