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

Refactor: aggregate variables into a structure #82

Merged
merged 6 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Changelog

## [0.0.5, unreleased] - 2023-XX-XX
## [0.0.8, unreleased] - 2023-xx-xx
### Changed
- Refactoring
- Refactoring for internal code. For example, introduce network struct.

## [0.0.5 - 0.0.7] - 2023-10-12
### Added
- GitHub Actions workflow for [gosec](https://github.com/securego/gosec).
- Fix gosec issue
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ test: ## Run unit tests
test-examples: ## Run unit tests for examples directory
make -C examples test

clean: ## Clean up
rm -f coverage.out coverage.html

.DEFAULT_GOAL := help
help: ## Show this help
@grep -E '^[0-9a-zA-Z_-]+[[:blank:]]*:.*?## .*$$' $(MAKEFILE_LIST) | sort \
Expand Down
4 changes: 2 additions & 2 deletions assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,13 +324,13 @@ type labeledContent struct {
// NoopVerifier is a verifier that does not perform verification
type NoopVerifier struct{}

var _ Verifier = NoopVerifier{}

// True is always true
func (n NoopVerifier) True(_ TestingT, _ bool, _ ...interface{}) bool {
return true
}

var _ Verifier = NoopVerifier{}

// Equal does not perform any assertion and always returns true
func (n NoopVerifier) Equal(_ TestingT, _, _ interface{}, _ ...interface{}) bool {
return true
Expand Down
44 changes: 44 additions & 0 deletions authentication.go
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
}
69 changes: 69 additions & 0 deletions authentication_test.go
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)
}
})
}
}
86 changes: 86 additions & 0 deletions debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,103 @@ package spectest

import (
"fmt"
"net/http"
"net/http/httputil"
"strings"
)

// debug is used to enable/disable debug logging
type debug struct {
enabled bool
}

// newDebug creates a new debug setting
func newDebug() *debug {
return &debug{}
}

// enable will enable debug logging
func (d *debug) enable() {
d.enabled = true
}

// disable will disable debug logging
func (d *debug) disable() {
nao1215 marked this conversation as resolved.
Show resolved Hide resolved
d.enabled = false
}

// isEnable returns true if debug logging is enabled
func (d *debug) isEnable() bool {
return d.enabled
}

// dumpResponse is used to dump the response.
// If debug logging is disabled, this method will do nothing.
func (d *debug) dumpRequest(req *http.Request) {
if !d.isEnable() {
return
}
requestDump, err := httputil.DumpRequest(req, true)
if err == nil {
debugLog(requestDebugPrefix(), "inbound http request", string(requestDump))
}
// TODO: handle error
}

// dumpResponse is used to dump the response.
// If debug logging is disabled, this method will do nothing.
func (d *debug) dumpResponse(res *http.Response) {
if !d.isEnable() {
return
}
responseDump, err := httputil.DumpResponse(res, true)
if err == nil {
debugLog(responseDebugPrefix(), "final response", string(responseDump))
}
// TODO: handle error
}

// duration is used to print the duration of the test.
// If debug logging is disabled, this method will do nothing.
func (d *debug) duration(interval *Interval) {
if !d.isEnable() {
return
}
fmt.Printf("Duration: %s\n", interval.Duration())
}

// mock is used to print the request and response from the mock.
func (d *debug) mock(res *http.Response, req *http.Request) {
if !d.isEnable() {
return
}

requestDump, err := httputil.DumpRequestOut(req, true)
if err == nil {
debugLog(requestDebugPrefix(), "request to mock", string(requestDump))
}

if res != nil {
responseDump, err := httputil.DumpResponse(res, true)
if err == nil {
debugLog(responseDebugPrefix(), "response from mock", string(responseDump))
}
} else {
debugLog(responseDebugPrefix(), "response from mock", "")
}
}

// debugLog is used to print debug information
func debugLog(prefix, header, msg string) {
fmt.Printf("\n%s %s\n%s\n", prefix, header, msg)
}

// requestDebugLog is used to print debug information for the request
func requestDebugPrefix() string {
return fmt.Sprintf("%s>", strings.Repeat("-", 10))
}

// responseDebugLog is used to print debug information for the response
func responseDebugPrefix() string {
return fmt.Sprintf("<%s", strings.Repeat("-", 10))
}
Loading
Loading