-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from go-spectest/add-doc-comment
Refactor: aggregate variables into a structure
- Loading branch information
Showing
16 changed files
with
373 additions
and
154 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package spectest | ||
|
||
import "fmt" | ||
|
||
// basicAuth is represents the basic auth credentials | ||
type basicAuth struct { | ||
// userName is the userName for basic auth | ||
userName string | ||
// password is the password for basic auth | ||
password string | ||
} | ||
|
||
// newBasicAuth creates a new basic auth | ||
func newBasicAuth(userName, password string) basicAuth { | ||
return basicAuth{ | ||
userName: userName, | ||
password: password, | ||
} | ||
} | ||
|
||
// isUserNameEmpty returns true if the userName is empty | ||
func (b basicAuth) isUserNameEmpty() bool { | ||
return b.userName == "" | ||
} | ||
|
||
// isPasswordEmpty returns true if the password is empty | ||
func (b basicAuth) isPasswordEmpty() bool { | ||
return b.password == "" | ||
} | ||
|
||
// auth will authenticate the user | ||
// auth will return an error if the user is not authenticated | ||
func (b basicAuth) auth(userName, password string) error { | ||
if b.userName != userName { | ||
return fmt.Errorf("basic auth request username '%s' did not match mock username '%s'", | ||
userName, b.userName) | ||
} | ||
|
||
if b.password != password { | ||
return fmt.Errorf("basic auth request password '%s' did not match mock password '%s'", | ||
password, b.password) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package spectest | ||
|
||
import "testing" | ||
|
||
func TestBasicAuthAuth(t *testing.T) { | ||
type fields struct { | ||
userName string | ||
password string | ||
} | ||
type args struct { | ||
userName string | ||
password string | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "auth with valid credentials", | ||
fields: fields{ | ||
userName: "user", | ||
password: "password", | ||
}, | ||
args: args{ | ||
userName: "user", | ||
password: "password", | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "auth with invalid credentials. bad password", | ||
fields: fields{ | ||
userName: "user", | ||
password: "password", | ||
}, | ||
args: args{ | ||
userName: "user", | ||
password: "invalid-password", | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "auth with invalid credentials. bad user name", | ||
fields: fields{ | ||
userName: "user", | ||
password: "password", | ||
}, | ||
args: args{ | ||
userName: "invalid-user", | ||
password: "password", | ||
}, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
b := &basicAuth{ | ||
userName: tt.fields.userName, | ||
password: tt.fields.password, | ||
} | ||
if err := b.auth(tt.args.userName, tt.args.password); (err != nil) != tt.wantErr { | ||
t.Errorf("basicAuth.auth() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.