-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
1,081 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package httpclient | ||
|
||
import "github.com/go-resty/resty/v2" | ||
|
||
type ( | ||
BytesClient interface { | ||
Get(urlPath string, opts ...RequestOption) ([]byte, error) | ||
Post(urlPath string, body interface{}, opts ...RequestOption) ([]byte, error) | ||
Put(urlPath string, body interface{}, opts ...RequestOption) ([]byte, error) | ||
Patch(urlPath string, body interface{}, opts ...RequestOption) ([]byte, error) | ||
Delete(urlPath string, body interface{}, opts ...RequestOption) ([]byte, error) | ||
Execute(method, urlPath string, body interface{}, opts ...RequestOption) ([]byte, error) | ||
} | ||
|
||
defaultBytesClient struct { | ||
client Client | ||
} | ||
) | ||
|
||
var _ BytesClient = (*defaultBytesClient)(nil) | ||
|
||
func NewBytesClient(addr string, opts ...RequestOption) BytesClient { | ||
return NewBytesClientRaw(NewClient(addr, opts...)) | ||
} | ||
|
||
func NewBytesClientRaw(cli Client) BytesClient { | ||
return &defaultBytesClient{ | ||
client: cli, | ||
} | ||
} | ||
|
||
func (c *defaultBytesClient) Get(urlPath string, opts ...RequestOption) ([]byte, error) { | ||
return c.convertResponse(c.client.Get(urlPath, opts...)) | ||
} | ||
|
||
func (c *defaultBytesClient) Post(urlPath string, body interface{}, opts ...RequestOption) ([]byte, error) { | ||
return c.convertResponse(c.client.Post(urlPath, body, opts...)) | ||
} | ||
|
||
func (c *defaultBytesClient) Put(urlPath string, body interface{}, opts ...RequestOption) ([]byte, error) { | ||
return c.convertResponse(c.client.Put(urlPath, body, opts...)) | ||
} | ||
|
||
func (c *defaultBytesClient) Patch(urlPath string, body interface{}, opts ...RequestOption) ([]byte, error) { | ||
return c.convertResponse(c.client.Patch(urlPath, body, opts...)) | ||
} | ||
|
||
func (c *defaultBytesClient) Delete(urlPath string, body interface{}, opts ...RequestOption) ([]byte, error) { | ||
return c.convertResponse(c.client.Delete(urlPath, body, opts...)) | ||
} | ||
|
||
func (c *defaultBytesClient) Execute(method, urlPath string, body interface{}, opts ...RequestOption) ([]byte, error) { | ||
return c.convertResponse(c.client.Execute(method, urlPath, body, opts...)) | ||
} | ||
|
||
func (*defaultBytesClient) convertResponse(resp *resty.Response, err error) ([]byte, error) { | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if err = NewResponseErrorNotSuccess(resp); err != nil { | ||
return nil, err | ||
} | ||
|
||
return resp.Body(), err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package httpclient | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/go-resty/resty/v2" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBytesClient(t *testing.T) { | ||
reqBody := []byte("testReqBody") | ||
respBody := []byte("testRespBody") | ||
reqFuncMap := map[string]func(BytesClient) ([]byte, error){ | ||
resty.MethodGet: func(c BytesClient) ([]byte, error) { | ||
return c.Get("") | ||
}, | ||
resty.MethodPost: func(c BytesClient) ([]byte, error) { | ||
return c.Post("", reqBody) | ||
}, | ||
resty.MethodPut: func(c BytesClient) ([]byte, error) { | ||
return c.Put("", reqBody) | ||
}, | ||
resty.MethodPatch: func(c BytesClient) ([]byte, error) { | ||
return c.Patch("", reqBody) | ||
}, | ||
resty.MethodDelete: func(c BytesClient) ([]byte, error) { | ||
return c.Delete("", reqBody) | ||
}, | ||
} | ||
|
||
for _, statusCode := range []int{200, 301, 404, 500, 502} { | ||
for _, method := range []string{resty.MethodGet, resty.MethodPost, resty.MethodPut, resty.MethodPatch, resty.MethodDelete} { | ||
t.Run(fmt.Sprintf("%s:%d", method, statusCode), func(t *testing.T) { | ||
ast := assert.New(t) | ||
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
ast.Equal(method, r.Method) | ||
if r.Method != resty.MethodGet { | ||
body, err := ioutil.ReadAll(r.Body) | ||
ast.NoError(err) | ||
ast.Equal(reqBody, body) | ||
} | ||
ast.Equal(url.Values{ | ||
"q0": []string{"qv0"}, | ||
"q1": []string{"qv1"}, | ||
"q2": []string{"qv2"}, | ||
}, r.URL.Query()) | ||
ast.Equal([]string{"hv0", "hv1", "hv2"}, []string{r.Header.Get("h0"), r.Header.Get("h1"), r.Header.Get("h2")}) | ||
ast.Equal("Bearer AuthToken", r.Header.Get("Authorization")) | ||
|
||
w.WriteHeader(statusCode) | ||
w.Write(respBody) | ||
})) | ||
|
||
defer testServer.Close() | ||
|
||
checkHookFunc := func(resp *resty.Response, err error) { | ||
if statusCode == 301 { | ||
ast.Error(err) | ||
ast.Contains(err.Error(), "301 response missing Location header") | ||
} else { | ||
ast.NoError(err) | ||
ast.Equal(statusCode, resp.StatusCode()) | ||
ast.Equal(respBody, resp.Body()) | ||
} | ||
} | ||
checkFunc := func(respBytes []byte, err error) { | ||
if statusCode == 301 { | ||
ast.Error(err) | ||
ast.Contains(err.Error(), "301 response missing Location header") | ||
} else if statusCode != 200 { | ||
ast.Nil(respBytes) | ||
respErr, ok := AsResponseError(err) | ||
ast.True(ok) | ||
resp := respErr.GetResponse() | ||
ast.NotNil(resp) | ||
ast.Equal(statusCode, resp.StatusCode()) | ||
ast.Equal(respBody, resp.Body()) | ||
} else { | ||
ast.NoError(err) | ||
ast.Equal(respBody, respBytes) | ||
} | ||
} | ||
|
||
c := NewBytesClient(testServer.URL, | ||
WithQueryParam("q0", "qv0"), | ||
WithQueryParams(map[string]string{ | ||
"q1": "qv1", | ||
"q2": "qv2", | ||
}), | ||
WithHeader("h0", "hv0"), | ||
WithHeaders(map[string]string{ | ||
"h1": "hv1", | ||
"h2": "hv2", | ||
}), | ||
WithAuthToken("AuthToken"), | ||
WithAfterRequestHook(func(_ *resty.Request, resp *resty.Response, err error) { | ||
checkHookFunc(resp, err) | ||
}), | ||
WithAfterRequestHook(func(_ *resty.Request, resp *resty.Response, err error) { | ||
checkHookFunc(resp, err) | ||
}), | ||
) | ||
f := reqFuncMap[method] | ||
if ast.NotNil(f) { | ||
resp, err := f(c) | ||
checkFunc(resp, err) | ||
} | ||
var executeBody interface{} | ||
if method != resty.MethodGet { | ||
executeBody = reqBody | ||
} | ||
resp, err := c.Execute(method, "", executeBody) | ||
checkFunc(resp, err) | ||
}) | ||
} | ||
} | ||
} |
Oops, something went wrong.