Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cprc & jsonclient: support request modifiers #781

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/crpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func (c *Client) WithUASuffix(suffix string) *Client {
}

// Do executes an RPC request against the configured server.
func (c *Client) Do(ctx context.Context, method, version string, src, dst interface{}) error {
err := c.Client.Do(ctx, "POST", path.Join(version, method), nil, src, dst)
func (c *Client) Do(ctx context.Context, method, version string, src, dst interface{}, requestModifiers ...func(r *http.Request)) error {
err := c.Client.Do(ctx, "POST", path.Join(version, method), nil, src, dst, requestModifiers...)

if err == nil {
return nil
Expand Down
10 changes: 7 additions & 3 deletions lib/jsonclient/jsonclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ func NewClient(baseURL string, c *http.Client) *Client {
}

// Do executes an HTTP request against the configured server.
func (c *Client) Do(ctx context.Context, method, path string, params url.Values, src, dst interface{}) error {
return c.DoWithHeaders(ctx, method, path, nil, params, src, dst)
func (c *Client) Do(ctx context.Context, method, path string, params url.Values, src, dst interface{}, requestModifiers ...func(r *http.Request)) error {
return c.DoWithHeaders(ctx, method, path, nil, params, src, dst, requestModifiers...)
}

// DoWithHeaders executes an HTTP request against the configured server with custom headers.
func (c *Client) DoWithHeaders(ctx context.Context, method, path string, headers http.Header, params url.Values, src, dst interface{}) error {
func (c *Client) DoWithHeaders(ctx context.Context, method, path string, headers http.Header, params url.Values, src, dst interface{}, requestModifiers ...func(r *http.Request)) error {
if c.Client == nil {
c.Client = http.DefaultClient
}
Expand Down Expand Up @@ -104,6 +104,10 @@ func (c *Client) DoWithHeaders(ctx context.Context, method, path string, headers
req.Header[key] = value
}

for _, modifier := range requestModifiers {
modifier(req)
}

err := c.setRequestBody(req, src)
if err != nil {
return &ClientRequestError{"could not marshal", err}
Expand Down
24 changes: 24 additions & 0 deletions lib/jsonclient/jsonclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,30 @@ func TestRequestBody(t *testing.T) {
assert.True(t, gock.IsDone())
}

func TestRequestModifier(t *testing.T) {
defer gock.Off()

testJSON := map[string]bool{"testing": true}

modifier := func(req *http.Request) {
req.Header.Add("X-Test-Header", "test")
}

gock.New("http://coo.va/").
Post("/test").
MatchType("application/json; charset=utf-8").
JSON(testJSON).
MatchHeader("X-Test-Header", "test").
Reply(http.StatusNoContent)

client := NewClient("http://coo.va/", nil)
gock.InterceptClient(client.Client)

err := client.Do(context.Background(), "POST", "test", nil, testJSON, nil, modifier)
assert.Nil(t, err)
assert.True(t, gock.IsDone())
}

func TestResponseBody(t *testing.T) {
defer gock.Off()

Expand Down
Loading