1+ using Microsoft . VisualStudio . TestTools . UnitTesting ;
2+ using RichardSzalay . MockHttp ;
13using System ;
24using System . Collections . Generic ;
35using System . Linq ;
46using System . Net ;
5- using System . Net . Http ;
6- using System . Threading . Tasks ;
7- using Microsoft . VisualStudio . TestTools . UnitTesting ;
8- using Moq ;
9- using RichardSzalay . MockHttp ;
107
118namespace WebPush . Test
129{
@@ -23,11 +20,21 @@ public class WebPushClientTest
2320 private const string TestFcmEndpoint =
2421 @"https://fcm.googleapis.com/fcm/send/efz_TLX_rLU:APA91bE6U0iybLYvv0F3mf6" ;
2522
23+ public const string TestSubject = "mailto:[email protected] " ; 24+
25+ private MockHttpMessageHandler httpMessageHandlerMock ;
26+ private WebPushClient client ;
27+
28+ [ TestInitialize ]
29+ public void InitializeTest ( )
30+ {
31+ httpMessageHandlerMock = new MockHttpMessageHandler ( ) ;
32+ client = new WebPushClient ( httpMessageHandlerMock . ToHttpClient ( ) ) ;
33+ }
34+
2635 [ TestMethod ]
2736 public void TestGcmApiKeyInOptions ( )
2837 {
29- var client = new WebPushClient ( ) ;
30-
3138 var gcmAPIKey = @"teststring" ;
3239 var subscription = new PushSubscription ( TestGcmEndpoint , TestPublicKey , TestPrivateKey ) ;
3340
@@ -50,8 +57,6 @@ public void TestGcmApiKeyInOptions()
5057 [ TestMethod ]
5158 public void TestSetGcmApiKey ( )
5259 {
53- var client = new WebPushClient ( ) ;
54-
5560 var gcmAPIKey = @"teststring" ;
5661 client . SetGcmApiKey ( gcmAPIKey ) ;
5762 var subscription = new PushSubscription ( TestGcmEndpoint , TestPublicKey , TestPrivateKey ) ;
@@ -64,16 +69,13 @@ public void TestSetGcmApiKey()
6469 [ TestMethod ]
6570 public void TestSetGCMAPIKeyEmptyString ( )
6671 {
67- var client = new WebPushClient ( ) ;
68-
6972 Assert . ThrowsException < ArgumentException > ( delegate { client . SetGcmApiKey ( "" ) ; } ) ;
7073 }
7174
7275 [ TestMethod ]
7376 public void TestSetGcmApiKeyNonGcmPushService ( )
7477 {
7578 // Ensure that the API key doesn't get added on a service that doesn't accept it.
76- var client = new WebPushClient ( ) ;
7779
7880 var gcmAPIKey = @"teststring" ;
7981 client . SetGcmApiKey ( gcmAPIKey ) ;
@@ -87,8 +89,6 @@ public void TestSetGcmApiKeyNonGcmPushService()
8789 [ TestMethod ]
8890 public void TestSetGcmApiKeyNull ( )
8991 {
90- var client = new WebPushClient ( ) ;
91-
9292 client . SetGcmApiKey ( @"somestring" ) ;
9393 client . SetGcmApiKey ( null ) ;
9494
@@ -102,9 +102,7 @@ public void TestSetGcmApiKeyNull()
102102 [ TestMethod ]
103103 public void TestSetVapidDetails ( )
104104 {
105- var client = new WebPushClient ( ) ;
106-
107- client . SetVapidDetails ( "mailto:[email protected] " , TestPublicKey , TestPrivateKey ) ; 105+ client . SetVapidDetails ( TestSubject , TestPublicKey , TestPrivateKey ) ;
108106
109107 var subscription = new PushSubscription ( TestFcmEndpoint , TestPublicKey , TestPrivateKey ) ;
110108 var message = client . GenerateRequestDetails ( subscription , @"test payload" ) ;
@@ -116,15 +114,33 @@ public void TestSetVapidDetails()
116114 }
117115
118116 [ TestMethod ]
119- public void TestPassingHttpClient ( )
117+ [ DataRow ( HttpStatusCode . Created ) ]
118+ [ DataRow ( HttpStatusCode . Accepted ) ]
119+ public void TestHandlingSuccessHttpCodes ( HttpStatusCode status )
120120 {
121- var mockHttp = new MockHttpMessageHandler ( ) ;
122- mockHttp . When ( TestFcmEndpoint ) . Respond ( HttpStatusCode . Created ) ;
121+ TestSendNotification ( status ) ;
122+ }
123123
124- var client = new WebPushClient ( mockHttp . ToHttpClient ( ) ) ;
125- client . SetVapidDetails ( "mailto:[email protected] " , TestPublicKey , TestPrivateKey ) ; 124+ [ TestMethod ]
125+ [ DataRow ( HttpStatusCode . BadRequest , "Bad Request" ) ]
126+ [ DataRow ( HttpStatusCode . RequestEntityTooLarge , "Payload too large" ) ]
127+ [ DataRow ( ( HttpStatusCode ) 429 , "Too many request." ) ]
128+ [ DataRow ( HttpStatusCode . NotFound , "Subscription no longer valid" ) ]
129+ [ DataRow ( HttpStatusCode . Gone , "Subscription no longer valid" ) ]
130+ [ DataRow ( HttpStatusCode . InternalServerError , "Received unexpected response code: 500" ) ]
131+ public void TestHandlingFailureHttpCodes ( HttpStatusCode status , string expectedMessage )
132+ {
133+ var actual = Assert . ThrowsException < WebPushException > ( ( ) => TestSendNotification ( status ) ) ;
126134
127- var subscription = new PushSubscription ( TestFcmEndpoint , TestPublicKey , TestPrivateKey ) ;
135+ Assert . AreEqual ( expectedMessage , actual . Message ) ;
136+ }
137+
138+ private void TestSendNotification ( HttpStatusCode status )
139+ {
140+ var subscription = new PushSubscription ( TestFcmEndpoint , TestPublicKey , TestPrivateKey ) ; ;
141+ httpMessageHandlerMock . When ( TestFcmEndpoint ) . Respond ( status ) ;
142+
143+ client . SetVapidDetails ( TestSubject , TestPublicKey , TestPrivateKey ) ;
128144
129145 client . SendNotification ( subscription , "123" ) ;
130146 }
0 commit comments