1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00

test: allow usage of httpmock in tests (#2576)

* add test case

* add flag for default transport

* Update go.mod

* Update go.sum

* fix spaces

* fix format

* Update http_test.go

* Update http_test.go

* Update http_test.go

* Update http_test.go

* Update http_test.go
This commit is contained in:
Christopher Fenner 2021-02-04 14:58:35 +01:00 committed by GitHub
parent 426c106765
commit 9ec282fd0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 3 deletions

1
go.mod
View File

@ -38,6 +38,7 @@ require (
github.com/hashicorp/vault/api v1.0.4 github.com/hashicorp/vault/api v1.0.4
github.com/huandu/xstrings v1.3.2 // indirect github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.11 // indirect github.com/imdario/mergo v0.3.11 // indirect
github.com/jarcoal/httpmock v1.0.6
github.com/magiconair/properties v1.8.4 github.com/magiconair/properties v1.8.4
github.com/mailru/easyjson v0.7.6 // indirect github.com/mailru/easyjson v0.7.6 // indirect
github.com/mitchellh/mapstructure v1.3.3 // indirect github.com/mitchellh/mapstructure v1.3.3 // indirect

2
go.sum
View File

@ -536,6 +536,8 @@ github.com/iris-contrib/blackfriday v2.0.0+incompatible/go.mod h1:UzZ2bDEoaSGPbk
github.com/iris-contrib/go.uuid v2.0.0+incompatible/go.mod h1:iz2lgM/1UnEf1kP0L/+fafWORmlnuysV2EMP8MW+qe0= github.com/iris-contrib/go.uuid v2.0.0+incompatible/go.mod h1:iz2lgM/1UnEf1kP0L/+fafWORmlnuysV2EMP8MW+qe0=
github.com/iris-contrib/i18n v0.0.0-20171121225848-987a633949d0/go.mod h1:pMCz62A0xJL6I+umB2YTlFRwWXaDFA0jy+5HzGiJjqI= github.com/iris-contrib/i18n v0.0.0-20171121225848-987a633949d0/go.mod h1:pMCz62A0xJL6I+umB2YTlFRwWXaDFA0jy+5HzGiJjqI=
github.com/iris-contrib/schema v0.0.1/go.mod h1:urYA3uvUNG1TIIjOSCzHr9/LmbQo8LrOcOqfqxa4hXw= github.com/iris-contrib/schema v0.0.1/go.mod h1:urYA3uvUNG1TIIjOSCzHr9/LmbQo8LrOcOqfqxa4hXw=
github.com/jarcoal/httpmock v1.0.6 h1:e81vOSexXU3mJuJ4l//geOmKIt+Vkxerk1feQBC8D0g=
github.com/jarcoal/httpmock v1.0.6/go.mod h1:ATjnClrvW/3tijVmpL/va5Z3aAyGvqU3gCT8nX0Txik=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=

View File

@ -36,6 +36,7 @@ type Client struct {
cookieJar http.CookieJar cookieJar http.CookieJar
doLogRequestBodyOnDebug bool doLogRequestBodyOnDebug bool
doLogResponseBodyOnDebug bool doLogResponseBodyOnDebug bool
useDefaultTransport bool
} }
// ClientOptions defines the options to be set on the client // ClientOptions defines the options to be set on the client
@ -57,6 +58,7 @@ type ClientOptions struct {
CookieJar http.CookieJar CookieJar http.CookieJar
DoLogRequestBodyOnDebug bool DoLogRequestBodyOnDebug bool
DoLogResponseBodyOnDebug bool DoLogResponseBodyOnDebug bool
UseDefaultTransport bool
} }
// TransportWrapper is a wrapper for central logging capabilities // TransportWrapper is a wrapper for central logging capabilities
@ -196,6 +198,7 @@ func (c *Client) SendRequest(method, url string, body io.Reader, header http.Hea
func (c *Client) SetOptions(options ClientOptions) { func (c *Client) SetOptions(options ClientOptions) {
c.doLogRequestBodyOnDebug = options.DoLogRequestBodyOnDebug c.doLogRequestBodyOnDebug = options.DoLogRequestBodyOnDebug
c.doLogResponseBodyOnDebug = options.DoLogResponseBodyOnDebug c.doLogResponseBodyOnDebug = options.DoLogResponseBodyOnDebug
c.useDefaultTransport = options.UseDefaultTransport
c.transportTimeout = options.TransportTimeout c.transportTimeout = options.TransportTimeout
c.transportSkipVerification = options.TransportSkipVerification c.transportSkipVerification = options.TransportSkipVerification
c.maxRequestDuration = options.MaxRequestDuration c.maxRequestDuration = options.MaxRequestDuration
@ -237,14 +240,18 @@ func (c *Client) initialize() *http.Client {
retryClient := retryablehttp.NewClient() retryClient := retryablehttp.NewClient()
retryClient.HTTPClient.Timeout = c.maxRequestDuration retryClient.HTTPClient.Timeout = c.maxRequestDuration
retryClient.HTTPClient.Jar = c.cookieJar retryClient.HTTPClient.Jar = c.cookieJar
retryClient.HTTPClient.Transport = transport
retryClient.RetryMax = c.maxRetries retryClient.RetryMax = c.maxRetries
if !c.useDefaultTransport {
retryClient.HTTPClient.Transport = transport
}
httpClient = retryClient.StandardClient() httpClient = retryClient.StandardClient()
} else { } else {
httpClient = &http.Client{} httpClient = &http.Client{}
httpClient.Timeout = c.maxRequestDuration httpClient.Timeout = c.maxRequestDuration
httpClient.Jar = c.cookieJar httpClient.Jar = c.cookieJar
httpClient.Transport = transport if !c.useDefaultTransport {
httpClient.Transport = transport
}
} }
if c.transportSkipVerification { if c.transportSkipVerification {

View File

@ -14,12 +14,51 @@ import (
"testing" "testing"
"time" "time"
"github.com/SAP/jenkins-library/pkg/log" "github.com/jarcoal/httpmock"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock" "github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/SAP/jenkins-library/pkg/log"
) )
func TestDefaultTransport(t *testing.T) {
const testURL string = "https://localhost/api"
t.Run("with default transport", func(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(http.MethodGet, testURL, httpmock.NewStringResponder(200, `OK`))
client := Client{}
client.SetOptions(ClientOptions{UseDefaultTransport: true})
// test
response, err := client.SendRequest("GET", testURL, nil, nil, nil)
// assert
assert.NoError(t, err)
// assert.NotEmpty(t, count)
assert.Equal(t, 1, httpmock.GetTotalCallCount(), "unexpected number of requests")
content, err := ioutil.ReadAll(response.Body)
defer response.Body.Close()
require.NoError(t, err, "unexpected error while reading response body")
assert.Equal(t, "OK", string(content), "unexpected response content")
})
t.Run("with custom transport", func(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(http.MethodGet, testURL, httpmock.NewStringResponder(200, `OK`))
client := Client{}
// test
_, err := client.SendRequest("GET", testURL, nil, nil, nil)
// assert
assert.Error(t, err)
assert.Contains(t, err.Error(), "connect: connection refused")
assert.Equal(t, 0, httpmock.GetTotalCallCount(), "unexpected number of requests")
})
}
func TestSendRequest(t *testing.T) { func TestSendRequest(t *testing.T) {
var passedHeaders = map[string][]string{} var passedHeaders = map[string][]string{}
passedCookies := []*http.Cookie{} passedCookies := []*http.Cookie{}