Skip to content

Commit

Permalink
Rename httptesting.Client to Mock
Browse files Browse the repository at this point in the history
  • Loading branch information
georgepsarakis committed Oct 7, 2024
1 parent 2a83145 commit 0d40faa
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 60 deletions.
22 changes: 11 additions & 11 deletions examples/example_client_json_api_testing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
53 changes: 8 additions & 45 deletions httptesting/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package httptesting

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
Expand All @@ -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
}

Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
8 changes: 4 additions & 4 deletions httptesting/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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").
Expand All @@ -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",
Expand Down Expand Up @@ -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())
}

0 comments on commit 0d40faa

Please sign in to comment.