mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-18 18:17:03 +00:00
43
rest/client/apiv1_client.go
Normal file
43
rest/client/apiv1_client.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
55
rest/client/apiv1_client_test.go
Normal file
55
rest/client/apiv1_client_test.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user