mirror of
https://github.com/wneessen/go-hibp.git
synced 2024-11-22 21:00:51 +01:00
Better test coverage for breaches API
This commit is contained in:
parent
a8623f1162
commit
2a92929c20
1 changed files with 46 additions and 0 deletions
|
@ -1,11 +1,19 @@
|
||||||
package hibp
|
package hibp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
validDateJSON = `{"date": "2022-10-01"}`
|
||||||
|
validNullDateJSON = `{"date": "null""}`
|
||||||
|
invalidJSON = `{"date": '2022-10-01'}`
|
||||||
|
invalidDateJSON = `{"date": "202299-10-01"}`
|
||||||
|
)
|
||||||
|
|
||||||
// TestBreaches tests the Breaches() method of the breaches API
|
// TestBreaches tests the Breaches() method of the breaches API
|
||||||
func TestBreaches(t *testing.T) {
|
func TestBreaches(t *testing.T) {
|
||||||
hc := New()
|
hc := New()
|
||||||
|
@ -238,6 +246,44 @@ func TestBreachedAccountWithoutTruncate(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestApiDate_UnmarshalJSON_Time tests the ApiDate type JSON unmarshalling
|
||||||
|
func TestApiDate_UnmarshalJSON_Time(t *testing.T) {
|
||||||
|
type testData struct {
|
||||||
|
Date *ApiDate `json:"date"`
|
||||||
|
}
|
||||||
|
tt := []struct {
|
||||||
|
n string
|
||||||
|
j []byte
|
||||||
|
d string
|
||||||
|
nil bool
|
||||||
|
sf bool
|
||||||
|
}{
|
||||||
|
{"valid Date JSON", []byte(validDateJSON), "2022-10-01", false, false},
|
||||||
|
{"valid Null Date JSON", []byte(validNullDateJSON), "", true, false},
|
||||||
|
{"invalid JSON", []byte(invalidJSON), "", true, true},
|
||||||
|
{"invalid Date", []byte(invalidDateJSON), "", true, true},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tt {
|
||||||
|
t.Run(tc.n, func(t *testing.T) {
|
||||||
|
var td testData
|
||||||
|
if err := json.Unmarshal(tc.j, &td); err != nil && !tc.sf {
|
||||||
|
t.Errorf("failed to unmarshal test JSON: %s", err)
|
||||||
|
}
|
||||||
|
if td.Date == nil && !tc.nil {
|
||||||
|
t.Errorf("unmarshal on ApiDate type failed. Expected data but got nil")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !tc.nil {
|
||||||
|
tdd := td.Date.Time().Format("2006-01-02")
|
||||||
|
if tdd != tc.d && !tc.sf {
|
||||||
|
t.Errorf(`unmarshal of ApiDate type failed. Expected: %q, got %q"`, tc.d, tdd)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ExampleBreachApi_Breaches_getAllBreaches is a code example to show how to fetch all breaches from the
|
// ExampleBreachApi_Breaches_getAllBreaches is a code example to show how to fetch all breaches from the
|
||||||
// HIBP breaches API
|
// HIBP breaches API
|
||||||
func ExampleBreachApi_Breaches_getAllBreaches() {
|
func ExampleBreachApi_Breaches_getAllBreaches() {
|
||||||
|
|
Loading…
Reference in a new issue