2021-09-21 11:21:04 +02:00
|
|
|
package hibp
|
|
|
|
|
|
|
|
import (
|
2021-09-22 13:59:22 +02:00
|
|
|
"fmt"
|
2021-09-21 19:47:17 +02:00
|
|
|
"os"
|
2021-09-21 11:21:04 +02:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestNew tests the New() function
|
|
|
|
func TestNew(t *testing.T) {
|
|
|
|
hc := New()
|
2022-10-29 15:32:12 +02:00
|
|
|
if *hc.PwnedPassAPI.hibp != hc {
|
2021-09-21 11:21:04 +02:00
|
|
|
t.Errorf("hibp client creation failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-22 13:59:22 +02:00
|
|
|
// TestNewWithNil tests the New() function with a nil option
|
|
|
|
func TestNewWithNil(t *testing.T) {
|
|
|
|
hc := New(nil)
|
2022-10-29 15:32:12 +02:00
|
|
|
if *hc.PwnedPassAPI.hibp != hc {
|
2021-09-22 13:59:22 +02:00
|
|
|
t.Errorf("hibp client creation failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-21 18:21:23 +02:00
|
|
|
// TestNewWithHttpTimeout tests the New() function with the http timeout option
|
2021-09-21 11:21:04 +02:00
|
|
|
func TestNewWithHttpTimeout(t *testing.T) {
|
2022-10-29 15:32:12 +02:00
|
|
|
hc := New(WithHTTPTimeout(time.Second * 10))
|
2021-09-21 11:21:04 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2021-09-21 18:21:23 +02:00
|
|
|
|
|
|
|
// TestNewWithPwnedPadding tests the New() function with the PwnedPadding option
|
|
|
|
func TestNewWithPwnedPadding(t *testing.T) {
|
|
|
|
hc := New(WithPwnedPadding())
|
2022-10-29 15:32:12 +02:00
|
|
|
if !hc.PwnedPassAPIOpts.WithPadding {
|
2021-09-21 18:21:23 +02:00
|
|
|
t.Errorf("hibp client pwned padding option was not set properly. Expected %v, got: %v",
|
2022-10-29 15:32:12 +02:00
|
|
|
true, hc.PwnedPassAPIOpts.WithPadding)
|
2021-09-21 18:21:23 +02:00
|
|
|
}
|
|
|
|
}
|
2021-09-21 19:47:17 +02:00
|
|
|
|
|
|
|
// TestNewWithApiKey tests the New() function with the API key set
|
|
|
|
func TestNewWithApiKey(t *testing.T) {
|
|
|
|
apiKey := os.Getenv("HIBP_API_KEY")
|
2022-10-29 15:32:12 +02:00
|
|
|
hc := New(WithAPIKey(apiKey), WithRateLimitSleep())
|
2021-09-21 19:47:17 +02:00
|
|
|
if hc.ak != apiKey {
|
|
|
|
t.Errorf("hibp client API key was not set properly. Expected %s, got: %s",
|
|
|
|
apiKey, hc.ak)
|
|
|
|
}
|
|
|
|
}
|
2021-09-22 13:59:22 +02:00
|
|
|
|
|
|
|
// TestNewWithUserAgent tests the New() function with a custom user agent
|
|
|
|
func TestNewWithUserAgent(t *testing.T) {
|
|
|
|
hc := New()
|
|
|
|
if hc.ua != DefaultUserAgent {
|
|
|
|
t.Errorf("hibp client default user agent was not set properly. Expected %s, got: %s",
|
|
|
|
DefaultUserAgent, hc.ua)
|
|
|
|
}
|
|
|
|
|
|
|
|
custUA := fmt.Sprintf("customUA v%s", Version)
|
|
|
|
hc = New(WithUserAgent(custUA))
|
|
|
|
if hc.ua != custUA {
|
|
|
|
t.Errorf("hibp client custom user agent was not set properly. Expected %s, got: %s",
|
|
|
|
custUA, hc.ua)
|
|
|
|
}
|
2022-04-12 22:52:24 +02:00
|
|
|
|
|
|
|
hc = New(WithUserAgent(""))
|
|
|
|
if hc.ua != DefaultUserAgent {
|
|
|
|
t.Errorf("hibp client custom user agent was not set properly. Expected %s, got: %s",
|
|
|
|
DefaultUserAgent, hc.ua)
|
|
|
|
}
|
2021-09-22 13:59:22 +02:00
|
|
|
}
|