diff --git a/examples/example_client_json_api_testing_test.go b/examples/example_client_json_api_testing_test.go index ac384bb..5fe86ea 100644 --- a/examples/example_client_json_api_testing_test.go +++ b/examples/example_client_json_api_testing_test.go @@ -15,19 +15,19 @@ import ( ) func TestGitHubSDK_GetUserByUsername(t *testing.T) { - testClient := httptesting.NewClient(t) - sdk := githubsdk.NewWithClient(testClient.Client) - testClient, err := testClient.WithBaseURL("https://test-api-github-com") + testClient := httptesting.NewMock(t) + mockHTTPClient := testClient.Client + sdk := githubsdk.NewWithClient(mockHTTPClient) + mockHTTPClient, err := mockHTTPClient.WithBaseURL("https://test-api-github-com") require.NoError(t, err) - testClient. - NewMockRequest( - http.MethodGet, - "https://test-api-github-com/users/georgepsarakis", - httpclient.WithHeaders(map[string]string{ - "x-github-api-version": "2022-11-28", - "accept": "application/vnd.github+json", - })). + testClient.NewMockRequest( + http.MethodGet, + "https://test-api-github-com/users/georgepsarakis", + httpclient.WithHeaders(map[string]string{ + "x-github-api-version": "2022-11-28", + "accept": "application/vnd.github+json", + })). RespondWithJSON(http.StatusOK, ` { "id": 963304, diff --git a/httptesting/client.go b/httptesting/client.go index 5007ebc..cf915c2 100644 --- a/httptesting/client.go +++ b/httptesting/client.go @@ -2,7 +2,6 @@ package httptesting import ( "bytes" - "context" "encoding/json" "fmt" "io" @@ -16,59 +15,23 @@ import ( "github.com/georgepsarakis/go-httpclient" ) -type HttpTestRequestParameter httpclient.RequestParameter - -type Client struct { +type Mock struct { *httpclient.Client mock *httpmock.MockTransport t *testing.T } -func NewClient(t *testing.T) *Client { +func NewMock(t *testing.T) *Mock { mt := httpmock.NewMockTransport() - return &Client{ + return &Mock{ Client: httpclient.NewWithTransport(mt), mock: mt, t: t, } } -func (c *Client) Get(ctx context.Context, url string, parameters ...httpclient.RequestParameter) (*http.Response, error) { - return c.Client.Get(ctx, url, parameters...) -} - -func (c *Client) Head(ctx context.Context, url string, parameters ...httpclient.RequestParameter) (*http.Response, error) { - return c.Client.Head(ctx, url, parameters...) -} - -func (c *Client) Post(ctx context.Context, url string, body io.Reader, parameters ...httpclient.RequestParameter) (*http.Response, error) { - return c.Client.Post(ctx, url, body, parameters...) -} - -func (c *Client) Patch(ctx context.Context, url string, body io.Reader, parameters ...httpclient.RequestParameter) (*http.Response, error) { - return c.Client.Patch(ctx, url, body, parameters...) -} - -func (c *Client) Delete(ctx context.Context, url string, parameters ...httpclient.RequestParameter) (*http.Response, error) { - return c.Client.Delete(ctx, url, parameters...) -} - -// WithBaseURL sets the base URL setting for the underlying `httpclient.Client`. -func (c *Client) WithBaseURL(baseURL string) (*Client, error) { - _, err := c.Client.WithBaseURL(baseURL) - if err != nil { - return nil, err - } - return c, nil -} - -func (c *Client) WithDefaultHeaders(headers map[string]string) *Client { - c.Client = c.Client.WithDefaultHeaders(headers) - return c -} - -// HTTPMock exposes the httpmock.MockTransport instance for advanced usage. -func (c *Client) HTTPMock() *httpmock.MockTransport { +// Transport exposes the httpmock.MockTransport instance for advanced usage. +func (c *Mock) Transport() *httpmock.MockTransport { return c.mock } @@ -82,7 +45,7 @@ type MockRequest struct { type MockResponse httpmock.Responder -func (c *Client) NewMockRequest(method, url string, params ...httpclient.RequestParameter) *MockRequest { +func (c *Mock) NewMockRequest(method, url string, params ...httpclient.RequestParameter) *MockRequest { c.t.Helper() req, err := http.NewRequest(method, url, nil) @@ -146,7 +109,7 @@ func (r *MockRequest) RespondWithHeaders(respHeaders map[string]string) *MockReq return r } -func (c *Client) NewJSONBodyMatcher(body string) httpmock.MatcherFunc { +func (c *Mock) NewJSONMatcher(body string) httpmock.MatcherFunc { c.t.Helper() return func(r *http.Request) bool { @@ -161,7 +124,7 @@ func interceptBody(t *testing.T, req *http.Request) []byte { t.Helper() body, err := io.ReadAll(req.Body) require.NoError(t, err) - req.Body.Close() + require.NoError(t, req.Body.Close()) req.Body = io.NopCloser(bytes.NewReader(body)) return body } diff --git a/httptesting/client_test.go b/httptesting/client_test.go index d4214a8..36c674f 100644 --- a/httptesting/client_test.go +++ b/httptesting/client_test.go @@ -14,7 +14,7 @@ import ( ) func TestClient_Get(t *testing.T) { - c := NewClient(t) + c := NewMock(t) requestURL := "http://localhost/p123" c.Client.WithDefaultHeaders(map[string]string{"Content-Type": "application/json"}) c.NewMockRequest(http.MethodGet, requestURL+"?test=1"). @@ -40,7 +40,7 @@ func TestClient_Get(t *testing.T) { } func TestClient_Head(t *testing.T) { - c := NewClient(t) + c := NewMock(t) requestURL := "http://localhost/p123" c.Client.WithDefaultHeaders(map[string]string{"Content-Type": "application/json"}) c.NewMockRequest(http.MethodHead, requestURL+"?test=1", @@ -70,8 +70,8 @@ func TestClient_Head(t *testing.T) { } func TestClient_WithBaseURL(t *testing.T) { - c := NewClient(t) - c, err := c.WithBaseURL("http://www.example.com/test") + c := NewMock(t) + _, err := c.WithBaseURL("http://www.example.com/test") require.NoError(t, err) require.Equal(t, "http://www.example.com/test", c.BaseURL()) }