From 29ff711f01639afddaacebd67224ab1d5831d392 Mon Sep 17 00:00:00 2001 From: Erik Seliger Date: Sun, 12 Nov 2023 22:42:22 +0100 Subject: [PATCH] client: Allow custom Doer 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. --- stream/client.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/stream/client.go b/stream/client.go index 827cecbf..4bc4f6de 100644 --- a/stream/client.go +++ b/stream/client.go @@ -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 @@ -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.