From 6c164980d69fbd270fd9d5dc4d0641721616ef30 Mon Sep 17 00:00:00 2001 From: Giuliano Barberi Date: Fri, 29 Mar 2019 13:30:13 -0400 Subject: [PATCH 1/2] Allowing the HTTP client to be set by the consumer. Useful for testing or implementing more robust HTTP clients. --- contentful.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/contentful.go b/contentful.go index cf4d689..a4bb2a5 100644 --- a/contentful.go +++ b/contentful.go @@ -120,6 +120,11 @@ func (c *Contentful) SetOrganization(organizationID string) *Contentful { return c } +// SetHTTPClient sets the underlying http.Client used to make requests. +func (c *Contentful) SetHTTPClient(client *http.Client) { + c.client = client +} + func (c *Contentful) newRequest(method, path string, query url.Values, body io.Reader) (*http.Request, error) { u, err := url.Parse(c.BaseURL) if err != nil { From 10f9ee305931948c683ec0d0a5f7e4107d7735cd Mon Sep 17 00:00:00 2001 From: Giuliano Barberi Date: Fri, 29 Mar 2019 13:34:19 -0400 Subject: [PATCH 2/2] Adding test for contentful.SetClient --- contentful_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/contentful_test.go b/contentful_test.go index 3163dcc..b7346dc 100644 --- a/contentful_test.go +++ b/contentful_test.go @@ -179,6 +179,15 @@ func TestContentfulSetOrganization(t *testing.T) { assert.Equal(organizationID, cma.Headers["X-Contentful-Organization"]) } +func TestContentfulSetClient(t *testing.T) { + assert := assert.New(t) + + newClient := &http.Client{} + cma := NewCMA(CMAToken) + cma.SetHTTPClient(newClient) + assert.Equal(newClient, cma.client) +} + func TestNewRequest(t *testing.T) { setup() defer teardown()