Skip to content

Commit

Permalink
Merge pull request #82 from go-spectest/add-doc-comment
Browse files Browse the repository at this point in the history
Refactor: aggregate variables into a structure
  • Loading branch information
nao1215 authored Oct 12, 2023
2 parents a7ba59b + 1af2464 commit a6747ee
Show file tree
Hide file tree
Showing 16 changed files with 373 additions and 154 deletions.
15 changes: 0 additions & 15 deletions .github/workflows/govulncheck.yml

This file was deleted.

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
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
[![LinuxUnitTest](https://github.com/go-spectest/spectest/actions/workflows/linux_test.yml/badge.svg)](https://github.com/go-spectest/spectest/actions/workflows/linux_test.yml)
[![MacUnitTest](https://github.com/go-spectest/spectest/actions/workflows/mac_test.yml/badge.svg)](https://github.com/go-spectest/spectest/actions/workflows/mac_test.yml)
[![WindowsUnitTest](https://github.com/go-spectest/spectest/actions/workflows/windows_test.yml/badge.svg)](https://github.com/go-spectest/spectest/actions/workflows/windows_test.yml)
[![Vuluncheck](https://github.com/go-spectest/spectest/actions/workflows/govulncheck.yml/badge.svg)](https://github.com/go-spectest/spectest/actions/workflows/govulncheck.yml)
[![UnitTestExampleCodes](https://github.com/go-spectest/spectest/actions/workflows/test-examples.yml/badge.svg)](https://github.com/go-spectest/spectest/actions/workflows/test-examples.yml)
[![reviewdog](https://github.com/go-spectest/spectest/actions/workflows/reviewdog.yml/badge.svg)](https://github.com/go-spectest/spectest/actions/workflows/reviewdog.yml)
[![Gosec](https://github.com/go-spectest/spectest/actions/workflows/gosec.yml/badge.svg)](https://github.com/go-spectest/spectest/actions/workflows/gosec.yml)
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() {
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))
}
17 changes: 16 additions & 1 deletion examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,19 @@ test:
cd sequence-diagrams && go test -v ./...
cd sequence-diagrams-with-mysql-database && go test -v ./...
cd sequence-diagrams-with-postgres-database && go test -v ./...
cd sequence-diagrams-with-sqlite-database && go test -v -covermode=atomic ./...
cd sequence-diagrams-with-sqlite-database && go test -v -covermode=atomic ./...

mod:
cd echo && go mod tidy
cd fiber && go mod tidy
cd gin && go mod tidy
cd ginkgo && go mod tidy
cd gorilla && go mod tidy
cd httprouter && go mod tidy
#cd iris && go mod tidy
cd mocks && go mod tidy
cd plantuml && go mod tidy
cd sequence-diagrams && go mod tidy
cd sequence-diagrams-with-mysql-database && go mod tidy
cd sequence-diagrams-with-postgres-database && go mod tidy
cd sequence-diagrams-with-sqlite-database && go mod tidy
2 changes: 1 addition & 1 deletion examples/sequence-diagrams/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/go-spectest/spectest/examples/sequence-diagrams
go 1.18

require (
github.com/go-spectest/spectest v0.0.5
github.com/go-spectest/spectest v0.0.6
github.com/gorilla/mux v1.8.0
)

Expand Down
4 changes: 2 additions & 2 deletions examples/sequence-diagrams/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-spectest/diff v0.0.0-20231006143314-ce490574d4a9 h1:I05FIUaZLNe9Jo6ZCvODDGBqHMk3TYd7V/wV9dZhwXk=
github.com/go-spectest/diff v0.0.0-20231006143314-ce490574d4a9/go.mod h1:wWRXl4ClWLDIPkL/lS8SSxojHRg1cGngvPcya/mYhf0=
github.com/go-spectest/spectest v0.0.5 h1:f3Bi4cIFbS8IxAJawd0L28OszzAqvw8tlaXGh/G4fjI=
github.com/go-spectest/spectest v0.0.5/go.mod h1:XIYK9byIF4TJDO2wrdXMcc06Gg2PfxiJF0Oc7uEfJXM=
github.com/go-spectest/spectest v0.0.6 h1:IqQUw1+Tq49ml75C6C3DBtXFilnOXUW8ZBFKlftxBQM=
github.com/go-spectest/spectest v0.0.6/go.mod h1:XIYK9byIF4TJDO2wrdXMcc06Gg2PfxiJF0Oc7uEfJXM=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
Expand Down
Loading

0 comments on commit a6747ee

Please sign in to comment.