Skip to content

Commit

Permalink
Add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sideshow committed Jan 16, 2016
1 parent 496d8f8 commit 0e98c2b
Showing 1 changed file with 45 additions and 24 deletions.
69 changes: 45 additions & 24 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import (
"testing"
"time"

"golang.org/x/net/http2"

apns "github.com/sideshow/apns2"
"github.com/sideshow/apns2/certificate"
)

// Mocks
Expand Down Expand Up @@ -53,6 +56,48 @@ func TestClientProductionHost(t *testing.T) {
}
}

func TestClientBadUrlError(t *testing.T) {
n := mockNotification()
res, err := mockClient("badurl://badurl.com").Push(n)
if err == nil {
t.Error("Expected a HttpClient error")
}
if res != nil {
t.Error("Response not expected, got", res)
}
}

func TestClientBadTransportError(t *testing.T) {
n := mockNotification()
client := mockClient("badurl://badurl.com")
client.HTTPClient.Transport = nil
res, err := client.Push(n)
if err == nil {
t.Error("Expected a HttpClient error")
}
if res != nil {
t.Error("Response not expected, got", res)
}
}

func TestClientNameToCertificate(t *testing.T) {
certficate2 := tls.Certificate{}
client2 := apns.NewClient(certficate2)
name2 := client2.HTTPClient.Transport.(*http2.Transport).TLSClientConfig.NameToCertificate

if len(name2) != 0 {
t.Error("Expected TLSClientConfig NameToCertificate to have no items")
}

certficate, _ := certificate.FromP12File("certificate/_fixtures/certificate-valid.p12", "")
client := apns.NewClient(certficate)
name := client.HTTPClient.Transport.(*http2.Transport).TLSClientConfig.NameToCertificate

if len(name) != 1 {
t.Error("Expected TLSClientConfig NameToCertificate to have one item")
}
}

// Functional Tests

func TestURL(t *testing.T) {
Expand Down Expand Up @@ -241,27 +286,3 @@ func TestMalformedJSONResponse(t *testing.T) {
t.Error("Sent should be false")
}
}

func TestBadUrlHttpClientError(t *testing.T) {
n := mockNotification()
res, err := mockClient("badurl://badurl.com").Push(n)
if err == nil {
t.Error("Expected a HttpClient error")
}
if res != nil {
t.Error("Response not expected, got", res)
}
}

func TestBadTransportHttpClientError(t *testing.T) {
n := mockNotification()
client := mockClient("badurl://badurl.com")
client.HTTPClient.Transport = nil
res, err := client.Push(n)
if err == nil {
t.Error("Expected a HttpClient error")
}
if res != nil {
t.Error("Response not expected, got", res)
}
}

0 comments on commit 0e98c2b

Please sign in to comment.