Merge pull request #20 from wneessen/testcoverage

Better test coverage
This commit is contained in:
Winni Neessen 2022-10-29 17:02:40 +02:00 committed by GitHub
commit 8a77d77ee6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,6 +2,7 @@ package hibp
import (
"fmt"
"net/http"
"os"
"testing"
"time"
@ -72,3 +73,39 @@ func TestNewWithUserAgent(t *testing.T) {
DefaultUserAgent, hc.ua)
}
}
func TestClient_HTTPReq(t *testing.T) {
u1 := "this://is.invalid.tld/with/invalid/chars/" + string([]byte{0x7f})
u2 := "this://is.invalid.tld/"
hc := New()
_, err := hc.HTTPReq(http.MethodGet, u1, map[string]string{"foo": "bar"})
if err == nil {
t.Errorf("HTTP GET request was supposed to fail, but didn't")
}
_, err = hc.HTTPReq("äöü", u2, map[string]string{"foo": "bar"})
if err == nil {
t.Errorf("HTTP GET request was supposed to fail, but didn't")
}
_, err = hc.HTTPReq("POST", u2, map[string]string{"foo": "bar"})
if err != nil {
t.Errorf("HTTP POST request failed: %s", err)
}
}
func TestClient_HTTPResBody(t *testing.T) {
u1 := "this://is.invalid.tld/with/invalid/chars/" + string([]byte{0x7f})
u2 := "this://is.invalid.tld/"
hc := New()
_, _, err := hc.HTTPResBody(http.MethodGet, u1, map[string]string{"foo": "bar"})
if err == nil {
t.Errorf("HTTP GET request was supposed to fail, but didn't")
}
_, _, err = hc.HTTPResBody("äöü", u2, map[string]string{"foo": "bar"})
if err == nil {
t.Errorf("HTTP GET request was supposed to fail, but didn't")
}
_, _, err = hc.HTTPResBody("POST", u2, map[string]string{"foo": "bar"})
if err == nil {
t.Errorf("HTTP POST request was supposed to fail, but didn't")
}
}