Skip to content

Commit

Permalink
Add basic auth mock matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Stein Fletcher committed Apr 17, 2020
1 parent a687d9b commit 932dadd
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
36 changes: 34 additions & 2 deletions mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ type MockRequest struct {
url *url.URL
method string
headers map[string][]string
basicAuthUsername string
basicAuthPassword string
headerPresent []string
headerNotPresent []string
formData map[string][]string
Expand Down Expand Up @@ -443,6 +445,13 @@ func (r *MockRequest) HeaderNotPresent(key string) *MockRequest {
return r
}

// BasicAuth configures the mock request to match the given basic auth parameters
func (r *MockRequest) BasicAuth(username, password string) *MockRequest {
r.basicAuthUsername = username
r.basicAuthPassword = password
return r
}

// FormData configures the mock request to math the given form data
func (r *MockRequest) FormData(key string, values ...string) *MockRequest {
r.formData[key] = append(r.formData[key], values...)
Expand Down Expand Up @@ -475,8 +484,7 @@ func (r *MockRequest) QueryParams(queryParams map[string]string) *MockRequest {
return r
}

// QueryCollection configures the mock request to match a number of repeating query params, e.g.
// ?a=1&a=2&a=3
// QueryCollection configures the mock request to match a number of repeating query params, e.g. ?a=1&a=2&a=3
func (r *MockRequest) QueryCollection(queryParams map[string][]string) *MockRequest {
for k, v := range queryParams {
for _, val := range v {
Expand Down Expand Up @@ -689,6 +697,29 @@ var headerMatcher = func(req *http.Request, spec *MockRequest) error {
return nil
}

var basicAuthMatcher = func(req *http.Request, spec *MockRequest) error {
if spec.basicAuthUsername == "" {
return nil
}

username, password, ok := req.BasicAuth()
if !ok {
return errors.New("request did not contain valid HTTP Basic Authentication string")
}

if spec.basicAuthUsername != username {
return fmt.Errorf("basic auth request username '%s' did not match mock username '%s'",
username, spec.basicAuthUsername)
}

if spec.basicAuthPassword != password {
return fmt.Errorf("basic auth request password '%s' did not match mock password '%s'",
password, spec.basicAuthPassword)
}

return nil
}

var headerPresentMatcher = func(req *http.Request, spec *MockRequest) error {
for _, header := range spec.headerPresent {
if req.Header.Get(header) == "" {
Expand Down Expand Up @@ -924,6 +955,7 @@ var defaultMatchers = []Matcher{
schemeMatcher,
methodMatcher,
headerMatcher,
basicAuthMatcher,
headerPresentMatcher,
headerNotPresentMatcher,
queryParamMatcher,
Expand Down
51 changes: 51 additions & 0 deletions mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,57 @@ func TestMocks_HeaderNotPresentMatcher(t *testing.T) {
}
}

func TestMocks_BasicAuth(t *testing.T) {
tests := map[string]struct {
reqUsername string
reqPassword string
mockUsername string
mockPassword string
expectedError error
}{
"matches": {
reqUsername: "myUser",
reqPassword: "myPassword",
mockUsername: "myUser",
mockPassword: "myPassword",
expectedError: nil,
},
"not matches username": {
reqUsername: "notMyUser",
reqPassword: "myPassword",
mockUsername: "myUser",
mockPassword: "myPassword",
expectedError: errors.New("basic auth request username 'notMyUser' did not match mock username 'myUser'"),
},
"not matches password": {
reqUsername: "myUser",
reqPassword: "notMyPassword",
mockUsername: "myUser",
mockPassword: "myPassword",
expectedError: errors.New("basic auth request password 'notMyPassword' did not match mock password 'myPassword'"),
},
"not matches if no auth header": {
mockUsername: "myUser",
mockPassword: "myPassword",
expectedError: errors.New("request did not contain valid HTTP Basic Authentication string"),
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/", nil)
if test.reqUsername != "" {
req.SetBasicAuth(test.reqUsername, test.reqPassword)
}

mockRequest := NewMock().Get("/").BasicAuth(test.mockUsername, test.mockPassword)

matchError := basicAuthMatcher(req, mockRequest)

assert.Equal(t, test.expectedError, matchError)
})
}
}

func TestMocks_QueryMatcher_Success(t *testing.T) {
tests := []struct {
requestUrl string
Expand Down

0 comments on commit 932dadd

Please sign in to comment.