Skip to content

Commit

Permalink
client: Allow custom Doer (#690)
Browse files Browse the repository at this point in the history
This PR makes the httpClient an interface with the minimal surface area required by the stream.Client.
This lets us inject a custom doer more easily, which can be beneficial for testing, and in the case of Sourcegraph, lets us augment the Doer with middlewares.
  • Loading branch information
eseliger authored Nov 15, 2023
1 parent 5e2620e commit 334e30f
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions stream/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@ import (
"github.com/sourcegraph/zoekt/query"
)

// Doer implements the minimal surface of *http.Client and http.RoundTripper needed
// by Client.
type Doer interface {
Do(*http.Request) (*http.Response, error)
}

// NewClient returns a client which implements StreamSearch. If httpClient is
// nil, http.DefaultClient is used.
func NewClient(address string, httpClient *http.Client) *Client {
func NewClient(address string, httpClient Doer) *Client {
registerGob()
if httpClient == nil {
httpClient = http.DefaultClient
Expand All @@ -31,7 +37,7 @@ type Client struct {
address string

// httpClient when set is used instead of http.DefaultClient
httpClient *http.Client
httpClient Doer
}

// SenderFunc is an adapter to allow the use of ordinary functions as Sender.
Expand Down

0 comments on commit 334e30f

Please sign in to comment.