Makes no sense to return a Client pointer. Also cleaned up examples directory

This commit is contained in:
Winni Neessen 2021-09-22 15:46:36 +02:00
parent 297b5b86f1
commit 7da9c54633
Signed by: wneessen
GPG key ID: 385AC9889632126E
10 changed files with 13 additions and 103 deletions

View file

@ -9,11 +9,6 @@ import (
// TestBreaches tests the Breaches() method of the breaches API
func TestBreaches(t *testing.T) {
hc := New()
if hc == nil {
t.Errorf("hibp client creation failed")
return
}
breachList, _, err := hc.BreachApi.Breaches()
if err != nil {
t.Error(err)
@ -26,11 +21,6 @@ func TestBreaches(t *testing.T) {
// TestBreachesWithNil tests the Breaches() method of the breaches API with a nil option
func TestBreachesWithNil(t *testing.T) {
hc := New()
if hc == nil {
t.Errorf("hibp client creation failed")
return
}
breachList, _, err := hc.BreachApi.Breaches(nil)
if err != nil {
t.Error(err)
@ -52,11 +42,6 @@ func TestBreachesWithDomain(t *testing.T) {
}
hc := New()
if hc == nil {
t.Error("failed to create HIBP client")
return
}
for _, tc := range testTable {
t.Run(tc.testName, func(t *testing.T) {
breachList, _, err := hc.BreachApi.Breaches(WithDomain(tc.domain))
@ -96,11 +81,6 @@ func TestBreachesWithoutUnverified(t *testing.T) {
}
hc := New()
if hc == nil {
t.Error("failed to create HIBP client")
return
}
for _, tc := range testTable {
t.Run(tc.testName, func(t *testing.T) {
breachList, _, err := hc.BreachApi.Breaches(WithDomain(tc.domain), WithoutUnverified())
@ -129,11 +109,6 @@ func TestBreachByName(t *testing.T) {
}
hc := New()
if hc == nil {
t.Error("failed to create HIBP client")
return
}
for _, tc := range testTable {
t.Run(tc.testName, func(t *testing.T) {
breachDetails, _, err := hc.BreachApi.BreachByName(tc.breachName)
@ -156,11 +131,6 @@ func TestBreachByName(t *testing.T) {
// TestDataClasses tests the DataClasses() method of the breaches API
func TestDataClasses(t *testing.T) {
hc := New()
if hc == nil {
t.Errorf("hibp client creation failed")
return
}
classList, _, err := hc.BreachApi.DataClasses()
if err != nil {
t.Error(err)
@ -190,11 +160,6 @@ func TestBreachedAccount(t *testing.T) {
t.SkipNow()
}
hc := New(WithApiKey(apiKey))
if hc == nil {
t.Error("failed to create HIBP client")
return
}
for _, tc := range testTable {
t.Run(tc.testName, func(t *testing.T) {
breachDetails, _, err := hc.BreachApi.BreachedAccount(
@ -245,11 +210,6 @@ func TestBreachedAccountWithoutTruncate(t *testing.T) {
t.SkipNow()
}
hc := New(WithApiKey(apiKey), WithRateLimitSleep())
if hc == nil {
t.Error("failed to create HIBP client")
return
}
for _, tc := range testTable {
t.Run(tc.testName, func(t *testing.T) {
breachDetails, _, err := hc.BreachApi.BreachedAccount(

View file

@ -2,15 +2,11 @@ package main
import (
"fmt"
hibp "github.com/wneessen/go-hibp"
"github.com/wneessen/go-hibp"
)
func main() {
hc := hibp.New()
if hc == nil {
panic("failed to create HIBP client")
}
bl, _, err := hc.BreachApi.Breaches()
if err != nil {
panic(err)

View file

@ -2,15 +2,11 @@ package main
import (
"fmt"
hibp "github.com/wneessen/go-hibp"
"github.com/wneessen/go-hibp"
)
func main() {
hc := hibp.New()
if hc == nil {
panic("failed to create HIBP client")
}
bl, _, err := hc.BreachApi.Breaches()
if err != nil {
panic(err)

View file

@ -7,10 +7,6 @@ import (
func main() {
hc := hibp.New()
if hc == nil {
panic("failed to create HIBP client")
}
bd, _, err := hc.BreachApi.BreachByName("Adobe")
if err != nil {
panic(err)

View file

@ -2,15 +2,11 @@ package main
import (
"fmt"
hibp "github.com/wneessen/go-hibp"
"github.com/wneessen/go-hibp"
)
func main() {
hc := hibp.New(hibp.WithPwnedPadding())
if hc == nil {
panic("failed to create HIBP client")
}
m, _, err := hc.PwnedPassApi.CheckPassword("test")
if err != nil {
panic(err)

View file

@ -2,15 +2,11 @@ package main
import (
"fmt"
hibp "github.com/wneessen/go-hibp"
"github.com/wneessen/go-hibp"
)
func main() {
hc := hibp.New()
if hc == nil {
panic("failed to create HIBP client")
}
m, _, err := hc.PwnedPassApi.CheckPassword("test")
if err != nil {
panic(err)

View file

@ -2,15 +2,11 @@ package main
import (
"fmt"
hibp "github.com/wneessen/go-hibp"
"github.com/wneessen/go-hibp"
)
func main() {
hc := hibp.New()
if hc == nil {
panic("failed to create HIBP client")
}
pwHash := "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" // represents the PW: "test"
m, _, err := hc.PwnedPassApi.CheckSHA1(pwHash)
if err != nil {

12
hibp.go
View file

@ -12,7 +12,7 @@ import (
)
// Version represents the version of this package
const Version = "0.1.4"
const Version = "0.1.5"
// BaseUrl is the base URL for the majority of API calls
const BaseUrl = "https://haveibeenpwned.com/api/v3"
@ -43,8 +43,8 @@ type Client struct {
type Option func(*Client)
// New creates and returns a new HIBP client object
func New(options ...Option) *Client {
c := &Client{}
func New(options ...Option) Client {
c := Client{}
// Set defaults
c.to = time.Second * 5
@ -56,15 +56,15 @@ func New(options ...Option) *Client {
if opt == nil {
continue
}
opt(c)
opt(&c)
}
// Add a http client to the Client object
c.hc = httpClient(c.to)
// Associate the different HIBP service APIs with the Client
c.PwnedPassApi = &PwnedPassApi{hibp: c}
c.BreachApi = &BreachApi{hibp: c}
c.PwnedPassApi = &PwnedPassApi{hibp: &c}
c.BreachApi = &BreachApi{hibp: &c}
return c
}

View file

@ -10,7 +10,7 @@ import (
// TestNew tests the New() function
func TestNew(t *testing.T) {
hc := New()
if hc == nil {
if *hc.PwnedPassApi.hibp != hc {
t.Errorf("hibp client creation failed")
}
}
@ -18,7 +18,7 @@ func TestNew(t *testing.T) {
// TestNewWithNil tests the New() function with a nil option
func TestNewWithNil(t *testing.T) {
hc := New(nil)
if hc == nil {
if *hc.PwnedPassApi.hibp != hc {
t.Errorf("hibp client creation failed")
}
}
@ -26,10 +26,6 @@ func TestNewWithNil(t *testing.T) {
// TestNewWithHttpTimeout tests the New() function with the http timeout option
func TestNewWithHttpTimeout(t *testing.T) {
hc := New(WithHttpTimeout(time.Second * 10))
if hc == nil {
t.Errorf("hibp client creation failed")
return
}
if hc.to != time.Second*10 {
t.Errorf("hibp client timeout option was not set properly. Expected %d, got: %d",
time.Second*10, hc.to)
@ -39,10 +35,6 @@ func TestNewWithHttpTimeout(t *testing.T) {
// TestNewWithPwnedPadding tests the New() function with the PwnedPadding option
func TestNewWithPwnedPadding(t *testing.T) {
hc := New(WithPwnedPadding())
if hc == nil {
t.Errorf("hibp client creation failed")
return
}
if !hc.PwnedPassApiOpts.WithPadding {
t.Errorf("hibp client pwned padding option was not set properly. Expected %v, got: %v",
true, hc.PwnedPassApiOpts.WithPadding)
@ -53,10 +45,6 @@ func TestNewWithPwnedPadding(t *testing.T) {
func TestNewWithApiKey(t *testing.T) {
apiKey := os.Getenv("HIBP_API_KEY")
hc := New(WithApiKey(apiKey))
if hc == nil {
t.Errorf("hibp client creation failed")
return
}
if hc.ak != apiKey {
t.Errorf("hibp client API key was not set properly. Expected %s, got: %s",
apiKey, hc.ak)
@ -66,10 +54,6 @@ func TestNewWithApiKey(t *testing.T) {
// TestNewWithUserAgent tests the New() function with a custom user agent
func TestNewWithUserAgent(t *testing.T) {
hc := New()
if hc == nil {
t.Errorf("hibp client creation failed")
return
}
if hc.ua != DefaultUserAgent {
t.Errorf("hibp client default user agent was not set properly. Expected %s, got: %s",
DefaultUserAgent, hc.ua)
@ -77,10 +61,6 @@ func TestNewWithUserAgent(t *testing.T) {
custUA := fmt.Sprintf("customUA v%s", Version)
hc = New(WithUserAgent(custUA))
if hc == nil {
t.Errorf("hibp client creation failed")
return
}
if hc.ua != custUA {
t.Errorf("hibp client custom user agent was not set properly. Expected %s, got: %s",
custUA, hc.ua)

View file

@ -16,7 +16,6 @@ func TestPwnedPasswordString(t *testing.T) {
"F/0Ws#.%{Z/NVax=OU8Ajf1qTRLNS12p/?s/adX", false},
}
hc := New()
for _, tc := range testTable {
t.Run(tc.testName, func(t *testing.T) {
m, _, err := hc.PwnedPassApi.CheckPassword(tc.pwString)
@ -47,11 +46,6 @@ func TestPwnedPasswordHash(t *testing.T) {
"90efc095c82eab44e882fda507cfab1a2cd31fc0", false},
}
hc := New()
if hc == nil {
t.Error("failed to create HIBP client")
return
}
for _, tc := range testTable {
t.Run(tc.testName, func(t *testing.T) {
m, _, err := hc.PwnedPassApi.CheckSHA1(tc.pwHash)