Skip to content

Commit

Permalink
Refactor tests to use mock requests, not servers
Browse files Browse the repository at this point in the history
  • Loading branch information
zackproser committed Jun 26, 2022
1 parent 4d35c6c commit 103ab81
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 128 deletions.
21 changes: 21 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ workflows:
- test
- test-windows
- test-mac
- test-mac-12-go-1-18
- release:
# Only run this job on git tag pushes
filters:
Expand Down Expand Up @@ -60,6 +61,26 @@ jobs:
command: |
mkdir -p /tmp/logs
go test -v ./... --timeout 5m | tee /tmp/logs/test.log
# Specifically test Go 1.18 on mac to catch syscall issues
test-mac-12-go-1-18:
macos:
xcode: 12.5.1
steps:
- checkout
- run:
name: install go
command: |
brew install [email protected]
# Make Go available in the PATH upon first being installed
echo 'export PATH="/usr/local/opt/[email protected]/bin:$PATH"' >> /Users/distiller/.bash_profile
- run:
name: run tests
command: |
mkdir -p /tmp/logs
go test -v ./... --timeout 5m | tee /tmp/logs/test.log
release:
docker:
- image: cimg/go:1.17
Expand Down
5 changes: 3 additions & 2 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,16 @@ func RunCLI() error {
}
log.SetLevel(level)

if parseErr := parseBlockListInput(blockList); parseErr != nil {
p := NewProcrastiproxy()

if parseErr := parseBlockListInput(blockList, p.GetList()); parseErr != nil {
return parseErr
}

if parseErr := parseStartAndEndTimes(*blockStartTime, *blockEndTime); parseErr != nil {
return parseErr
}

p := NewProcrastiproxy()
// Configure proxy time-based block settings
p.ConfigureProxyTimeSettings(*blockStartTime, *blockEndTime)

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ go 1.14
require (
github.com/hashicorp/go-multierror v1.1.1
github.com/sirupsen/logrus v1.8.1
golang.org/x/sys v0.0.0-20220624220833-87e55d714810 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220624220833-87e55d714810 h1:rHZQSjJdAI4Xf5Qzeh2bBc5YJIkPFVM6oDtMFYmgws0=
golang.org/x/sys v0.0.0-20220624220833-87e55d714810/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
4 changes: 2 additions & 2 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (p *Procrastiproxy) timeAwareHandler(w http.ResponseWriter, r *http.Request

func (p *Procrastiproxy) blockListAwareHandler(w http.ResponseWriter, r *http.Request) {
host := sanitizeHost(r.URL.Host)
if hostIsBlocked(host) {
if hostIsOnBlockList(host, p.GetList()) {
log.Debugf("Blocking request to host: %s. User explicitly blocked and present time is within configured proxy block window", host)
blockRequest(w)
return
Expand All @@ -94,7 +94,7 @@ func (p *Procrastiproxy) adminHandler(w http.ResponseWriter, r *http.Request) {
log.Println(err)
}
var respMsg string
list := GetList()
list := p.GetList()
if adminCmd.Command == "block" {
list.Add(adminCmd.Host)
respMsg = fmt.Sprintf("Successfully added: %s to the block list\n", adminCmd.Host)
Expand Down
4 changes: 2 additions & 2 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"sync"
)

var list = newList()
var list = NewList()

type List struct {
m sync.Mutex
Expand All @@ -16,7 +16,7 @@ func GetList() *List {
return list
}

func newList() *List {
func NewList() *List {
return &List{
members: make(map[string]bool),
}
Expand Down
10 changes: 8 additions & 2 deletions procrastiproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ var procrastiproxy *Procrastiproxy
var DefaultNow = time.Now

type Procrastiproxy struct {
Now func() time.Time
Now func() time.Time
List *List
ProxyTimeSettings
}

Expand All @@ -23,11 +24,16 @@ func NewProcrastiproxy() *Procrastiproxy {
return procrastiproxy
}
procrastiproxy = &Procrastiproxy{
Now: DefaultNow,
Now: DefaultNow,
List: NewList(),
}
return procrastiproxy
}

func (p *Procrastiproxy) GetList() *List {
return p.List
}

// custom errors

type EmptyBlockListError struct{}
Expand Down
5 changes: 2 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ func sanitizeHost(host string) string {
return strings.ToLower(strings.TrimSpace(strings.Replace(host, "\n", "", -1)))
}

func hostIsBlocked(host string) bool {
func hostIsOnBlockList(host string, list *List) bool {
host = sanitizeHost(host)
blockList := GetList()
return blockList.Contains(host)
return list.Contains(host)
}

func RunServer(args []string) {
Expand Down
Loading

0 comments on commit 103ab81

Please sign in to comment.