diff options
author | Ruben Pollan <meskio@sindominio.net> | 2018-11-22 20:50:56 -0600 |
---|---|---|
committer | Ruben Pollan <meskio@sindominio.net> | 2018-12-12 13:37:38 +0100 |
commit | d8e683ef0b15e00bba9c241181e7d3247d30539c (patch) | |
tree | 5ccb04c333c6a9d6d26dfd340d23b7d6ebf83cac /standalone/bonafide.go | |
parent | d7a1eecfd9d40c7c114f9ecab8180e00107766ec (diff) |
[test] make bonafide more testable
Get local timezone and http client as configurable things in bonafide,
so tests can set them. Also separate integration tests and unit tests.
Diffstat (limited to 'standalone/bonafide.go')
-rw-r--r-- | standalone/bonafide.go | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/standalone/bonafide.go b/standalone/bonafide.go index b46d9bb..db9ef7e 100644 --- a/standalone/bonafide.go +++ b/standalone/bonafide.go @@ -20,6 +20,7 @@ import ( "crypto/x509" "encoding/json" "fmt" + "io" "io/ioutil" "log" "math/rand" @@ -72,10 +73,14 @@ UN9SaWRlWKSdP4haujnzCoJbM7dU9bjvlGZNyXEekgeT0W2qFeGGp+yyUWw8tNsp ) type bonafide struct { - client *http.Client + client httpClient + tzOffsetHours int eip *eipService defaultGateway string } +type httpClient interface { + Post(url, contentType string, body io.Reader) (resp *http.Response, err error) +} type eipService struct { Gateways []gateway @@ -108,8 +113,15 @@ func newBonafide() *bonafide { }, }, } + _, tzOffsetSeconds := time.Now().Zone() + tzOffsetHours := tzOffsetSeconds / secondsPerHour - return &bonafide{client, nil, ""} + return &bonafide{ + client: client, + tzOffsetHours: tzOffsetHours, + eip: nil, + defaultGateway: "", + } } func (b *bonafide) getCertPem() ([]byte, error) { @@ -195,8 +207,6 @@ func (b *bonafide) sortGateways() { } gws := []gatewayDistance{} - _, tzOffset := time.Now().Zone() - tzOffset = tzOffset / secondsPerHour for _, gw := range b.eip.Gateways { distance := 13 if gw.Location == b.defaultGateway { @@ -206,7 +216,7 @@ func (b *bonafide) sortGateways() { if err != nil { log.Printf("Error sorting gateways: %v", err) } else { - distance = tzDistance(tzOffset, gwOffset) + distance = tzDistance(b.tzOffsetHours, gwOffset) } } gws = append(gws, gatewayDistance{gw, distance}) |