mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-18 18:17:03 +00:00
Merge branch 'feature/rest-client' into develop
This commit is contained in:
@@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
|
|||||||
- Storage of To: header in messages (likely breaks existing datastores)
|
- Storage of To: header in messages (likely breaks existing datastores)
|
||||||
- Attachment list to [GET message
|
- Attachment list to [GET message
|
||||||
JSON](https://github.com/jhillyerd/inbucket/wiki/REST-GET-message)
|
JSON](https://github.com/jhillyerd/inbucket/wiki/REST-GET-message)
|
||||||
|
- Go client for REST API
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
- No longer run out of file handles when dealing with a large number of
|
- No longer run out of file handles when dealing with a large number of
|
||||||
|
|||||||
96
rest/client/apiv1_client.go
Normal file
96
rest/client/apiv1_client.go
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"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{
|
||||||
|
Timeout: 30 * time.Second,
|
||||||
|
},
|
||||||
|
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.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.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
|
||||||
|
}
|
||||||
145
rest/client/apiv1_client_test.go
Normal file
145
rest/client/apiv1_client_test.go
Normal file
@@ -0,0 +1,145 @@
|
|||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
60
rest/client/rest.go
Normal file
60
rest/client/rest.go
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
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 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
|
||||||
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
_ = resp.Body.Close()
|
||||||
|
}()
|
||||||
|
if resp.StatusCode == http.StatusOK {
|
||||||
|
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)
|
||||||
|
}
|
||||||
120
rest/client/rest_test.go
Normal file
120
rest/client/rest_test.go
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
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
|
||||||
|
statusCode int
|
||||||
|
body string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mockHTTPClient) Do(req *http.Request) (resp *http.Response, err error) {
|
||||||
|
m.req = req
|
||||||
|
resp = &http.Response{
|
||||||
|
StatusCode: m.statusCode,
|
||||||
|
Body: ioutil.NopCloser(bytes.NewBufferString(m.body)),
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDo(t *testing.T) {
|
||||||
|
var want, got string
|
||||||
|
|
||||||
|
mth := &mockHTTPClient{}
|
||||||
|
c := &restClient{mth, baseURL}
|
||||||
|
|
||||||
|
_, err := c.do("POST", "/dopost")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
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 TestDoJSON(t *testing.T) {
|
||||||
|
var want, got string
|
||||||
|
|
||||||
|
mth := &mockHTTPClient{
|
||||||
|
statusCode: 200,
|
||||||
|
body: `{"foo": "bar"}`,
|
||||||
|
}
|
||||||
|
c := &restClient{mth, baseURL}
|
||||||
|
|
||||||
|
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
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user