Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix 2.0: use default transport to mock http requests #22

Merged
merged 8 commits into from
Mar 25, 2024
14 changes: 9 additions & 5 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ func AddToScheme() error {
var DefaultNewClient = NewClient

func NewClient(token, apiEndpoint string) (runtimeclient.Client, error) {
return NewClientWitTransport(token, apiEndpoint, &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true, // nolint: gosec
},
})
}

func NewClientWitTransport(token, apiEndpoint string, transport http.RoundTripper) (runtimeclient.Client, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func NewClientWitTransport(token, apiEndpoint string, transport http.RoundTripper) (runtimeclient.Client, error) {
func NewClientWithTransport(token, apiEndpoint string, transport http.RoundTripper) (runtimeclient.Client, error) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(small typo)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Friday's morning a2a3c2f

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And thanks for the catch ;-)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my pleasure! 😇

Copy link
Contributor Author

@MatousJobanek MatousJobanek Mar 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please click on approve now? or is there anything still missing?

if err := AddToScheme(); err != nil {
return nil, err
}
Expand All @@ -63,11 +71,7 @@ func NewClient(token, apiEndpoint string) (runtimeclient.Client, error) {
return nil, err
}

cfg.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true, // nolint: gosec
},
}
cfg.Transport = transport
cfg.BearerToken = string(token)
cfg.QPS = 40.0
cfg.Burst = 50
Expand Down
5 changes: 3 additions & 2 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package client_test
import (
"context"
"fmt"
"net/http"
"testing"

toolchainv1alpha1 "github.com/codeready-toolchain/api/api/v1alpha1"
Expand All @@ -26,14 +27,14 @@ import (
func TestNewClientOK(t *testing.T) {
// given
t.Cleanup(gock.OffAll)
gock.New("http://example.com").
gock.New("https://example.com").
Get("api").
Persist().
Reply(200).
BodyString("{}")

// when
cl, err := client.NewClient("cool-token", "http://example.com")
cl, err := client.NewClientWitTransport("cool-token", "https://example.com", http.DefaultTransport)

// then
require.NoError(t, err)
Expand Down
Loading