mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-17 17:47:03 +00:00
Generic REST client (HTTP GET only) for #43
This commit is contained in:
56
rest/client/rest.go
Normal file
56
rest/client/rest.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
77
rest/client/rest_test.go
Normal file
77
rest/client/rest_test.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user