mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-17 09:37:02 +00:00
[Rest Client] changing to enable relative urls (#477)
* changing to enable relative urls Signed-off-by: Corey Aloia <corey.aloia@sap.com>
This commit is contained in:
@@ -22,11 +22,7 @@ type restClient struct {
|
|||||||
|
|
||||||
// do performs an HTTP request with this client and returns the response.
|
// do performs an HTTP request with this client and returns the response.
|
||||||
func (c *restClient) do(method, uri string, body []byte) (*http.Response, error) {
|
func (c *restClient) do(method, uri string, body []byte) (*http.Response, error) {
|
||||||
rel, err := url.Parse(uri)
|
url := c.baseURL.JoinPath(uri)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
url := c.baseURL.ResolveReference(rel)
|
|
||||||
var r io.Reader
|
var r io.Reader
|
||||||
if body != nil {
|
if body != nil {
|
||||||
r = bytes.NewReader(body)
|
r = bytes.NewReader(body)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package client
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
@@ -9,15 +10,22 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const baseURLStr = "http://test.local:8080"
|
const baseURLStr = "http://test.local:8080"
|
||||||
|
const baseURLPathStr = "http://test.local:8080/inbucket"
|
||||||
|
|
||||||
var baseURL *url.URL
|
var baseURL *url.URL
|
||||||
|
|
||||||
|
var baseURLPath *url.URL
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
var err error
|
var err error
|
||||||
baseURL, err = url.Parse(baseURLStr)
|
baseURL, err = url.Parse(baseURLStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
baseURLPath, err = url.Parse(baseURLPathStr)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type mockHTTPClient struct {
|
type mockHTTPClient struct {
|
||||||
@@ -51,32 +59,39 @@ func (m *mockHTTPClient) ReqBody() []byte {
|
|||||||
return body
|
return body
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDo(t *testing.T) {
|
func TestDoTable(t *testing.T) {
|
||||||
var want, got string
|
tests := []struct {
|
||||||
mth := &mockHTTPClient{}
|
method string
|
||||||
c := &restClient{mth, baseURL}
|
uri string
|
||||||
body := []byte("Test body")
|
wantMethod string
|
||||||
|
base *url.URL
|
||||||
_, err := c.do("POST", "/dopost", body)
|
wantUrl string
|
||||||
if err != nil {
|
wantBody []byte
|
||||||
t.Fatal(err)
|
}{
|
||||||
|
{method: "GET", wantMethod: "GET", uri: "/doget", base: baseURL, wantUrl: baseURLStr + "/doget", wantBody: []byte("Test body 1")},
|
||||||
|
{method: "POST", wantMethod: "POST", uri: "/dopost", base: baseURL, wantUrl: baseURLStr + "/dopost", wantBody: []byte("Test body 2")},
|
||||||
|
{method: "GET", wantMethod: "GET", uri: "/doget", base: baseURLPath, wantUrl: baseURLPathStr + "/doget", wantBody: []byte("Test body 3")},
|
||||||
|
{method: "POST", wantMethod: "POST", uri: "/dopost", base: baseURLPath, wantUrl: baseURLPathStr + "/dopost", wantBody: []byte("Test body 4")},
|
||||||
}
|
}
|
||||||
|
for _, test := range tests {
|
||||||
want = "POST"
|
testname := fmt.Sprintf("%s,%s", test.method, test.wantUrl)
|
||||||
got = mth.req.Method
|
t.Run(testname, func(t *testing.T) {
|
||||||
if got != want {
|
mth := &mockHTTPClient{}
|
||||||
t.Errorf("req.Method == %q, want %q", got, want)
|
c := &restClient{mth, test.base}
|
||||||
}
|
_, err := c.do(test.method, test.uri, test.wantBody)
|
||||||
|
if err != nil {
|
||||||
want = baseURLStr + "/dopost"
|
t.Fatal(err)
|
||||||
got = mth.req.URL.String()
|
}
|
||||||
if got != want {
|
if mth.req.Method != test.wantMethod {
|
||||||
t.Errorf("req.URL == %q, want %q", got, want)
|
t.Errorf("req.Method == %q, want %q", mth.req.Method, test.wantMethod)
|
||||||
}
|
}
|
||||||
|
if mth.req.URL.String() != test.wantUrl {
|
||||||
b := mth.ReqBody()
|
t.Errorf("req.URL == %q, want %q", mth.req.URL.String(), test.wantUrl)
|
||||||
if !bytes.Equal(b, body) {
|
}
|
||||||
t.Errorf("req.Body == %q, want %q", b, body)
|
if !bytes.Equal(mth.ReqBody(), test.wantBody) {
|
||||||
|
t.Errorf("req.Body == %q, want %q", mth.ReqBody(), test.wantBody)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user