~ewintr/erikwinternl

80274c23bd05d71a093a98cb8b84dd87896b6092 — Erik Winter 3 months ago 054203e http_mock
added auth
2 files changed, 19 insertions(+), 6 deletions(-)

M httpmock/client.go
M httpmock/client_test.go
M httpmock/client.go => httpmock/client.go +11 -5
@@ 15,14 15,18 @@ var (
)

type FooClient struct {
	url    string
	client *http.Client
	url      string
	username string
	password string
	client   *http.Client
}

func NewFooClient(url string) *FooClient {
func NewFooClient(url, username, password string) *FooClient {
	return &FooClient{
		url:    url,
		client: &http.Client{},
		url:      url,
		username: username,
		password: password,
		client:   &http.Client{},
	}
}



@@ 39,6 43,8 @@ func (fc *FooClient) DoStuff(param string) (string, error) {
	if err != nil {
		return "", fmt.Errorf("%w: %v", ErrInternalFailure, err)
	}
	req.SetBasicAuth(fc.username, fc.password)
	req.Header.Set("Content-Type", "application/json;charset=utf-8")

	resp, err := fc.client.Do(req)
	if err != nil {

M httpmock/client_test.go => httpmock/client_test.go +8 -1
@@ 12,6 12,8 @@ import (

func TestFooClientDoStuff(t *testing.T) {
	path := "/path"
	username := "username"
	password := "password"

	for _, tc := range []struct {
		name      string


@@ 58,7 60,7 @@ func TestFooClientDoStuff(t *testing.T) {
				},
			})

			client := httpmock.NewFooClient(mockServer.URL)
			client := httpmock.NewFooClient(mockServer.URL, username, password)

			actResult, actErr := client.DoStuff(tc.param)



@@ 75,6 77,11 @@ func TestFooClientDoStuff(t *testing.T) {
			test.Equals(t, expBody, actBody)

			// check request headers
			expHeaders := []http.Header{{
				"Authorization": []string{"Basic dXNlcm5hbWU6cGFzc3dvcmQ="},
				"Content-Type":  []string{"application/json;charset=utf-8"},
			}}
			test.Equals(t, expHeaders, record.Headers(path, http.MethodPost))
		})
	}
}