Skip to content

Commit

Permalink
add default client method & test
Browse files Browse the repository at this point in the history
  • Loading branch information
natebrennand committed Nov 7, 2022
1 parent 0bad2da commit ca4bc43
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
4 changes: 4 additions & 0 deletions client/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,9 @@ func (w wrapperClient) SendRequestWithCtx(ctx context.Context, method string, ra
// wrapBaseClientWithNoopCtx "upgrades" a BaseClient to BaseClientWithCtx so that requests can be
// send with a request context.
func wrapBaseClientWithNoopCtx(c BaseClient) BaseClientWithCtx {
// the default library client has SendRequestWithCtx, use it if available.
if typedClient, ok := c.(BaseClientWithCtx); ok {
return typedClient
}
return wrapperClient{BaseClient: c}
}
11 changes: 9 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package client

import (
"context"
"encoding/json"
"fmt"
"net/http"
Expand Down Expand Up @@ -44,7 +45,7 @@ func defaultHTTPClient() *http.Client {
}
}

func (c *Client) basicAuth() (string, string) {
func (c *Client) basicAuth() (username, password string) {
return c.Credentials.Username, c.Credentials.Password
}

Expand Down Expand Up @@ -89,6 +90,12 @@ func (c *Client) doWithErr(req *http.Request) (*http.Response, error) {

// SendRequest verifies, constructs, and authorizes an HTTP request.
func (c *Client) SendRequest(method string, rawURL string, data url.Values,
headers map[string]interface{}) (*http.Response, error) {
return c.SendRequestWithCtx(context.TODO(), method, rawURL, data, headers)
}

// SendRequestWithCtx verifies, constructs, and authorizes an HTTP request.
func (c *Client) SendRequestWithCtx(ctx context.Context, method string, rawURL string, data url.Values,
headers map[string]interface{}) (*http.Response, error) {
u, err := url.Parse(rawURL)
if err != nil {
Expand All @@ -112,7 +119,7 @@ func (c *Client) SendRequest(method string, rawURL string, data url.Values,
valueReader = strings.NewReader(data.Encode())
}

req, err := http.NewRequest(method, u.String(), valueReader)
req, err := http.NewRequestWithContext(ctx, method, u.String(), valueReader)
if err != nil {
return nil, err
}
Expand Down
28 changes: 28 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client_test

import (
"context"
"encoding/json"
"io"
"net/http"
Expand Down Expand Up @@ -210,6 +211,33 @@ func TestClient_SetTimeoutTimesOut(t *testing.T) {
assert.Error(t, err)
}

func TestClient_SetTimeoutTimesOutViaContext(t *testing.T) {
handlerDelay := 100 * time.Microsecond
clientTimeout := 10 * time.Microsecond
assert.True(t, clientTimeout < handlerDelay)

timeoutServer := httptest.NewServer(http.HandlerFunc(
func(writer http.ResponseWriter, _ *http.Request) {
d := map[string]interface{}{
"response": "ok",
}
time.Sleep(100 * time.Microsecond)
encoder := json.NewEncoder(writer)
err := encoder.Encode(&d)
if err != nil {
t.Error(err)
}
writer.WriteHeader(http.StatusOK)
}))
defer timeoutServer.Close()

c := NewClient("user", "pass")
ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Microsecond)
defer cancel()
_, err := c.SendRequestWithCtx(ctx, "GET", timeoutServer.URL, nil, nil) //nolint:bodyclose
assert.Error(t, err)
}

func TestClient_SetTimeoutSucceeds(t *testing.T) {
timeoutServer := httptest.NewServer(http.HandlerFunc(
func(writer http.ResponseWriter, request *http.Request) {
Expand Down

0 comments on commit ca4bc43

Please sign in to comment.