1
+ using Microsoft . VisualStudio . TestTools . UnitTesting ;
2
+ using RichardSzalay . MockHttp ;
1
3
using System ;
2
4
using System . Collections . Generic ;
3
5
using System . Linq ;
4
6
using 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 ;
10
7
11
8
namespace WebPush . Test
12
9
{
@@ -23,11 +20,21 @@ public class WebPushClientTest
23
20
private const string TestFcmEndpoint =
24
21
@"https://fcm.googleapis.com/fcm/send/efz_TLX_rLU:APA91bE6U0iybLYvv0F3mf6" ;
25
22
23
+ public const string TestSubject = "mailto:example@example.com" ;
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
+
26
35
[ TestMethod ]
27
36
public void TestGcmApiKeyInOptions ( )
28
37
{
29
- var client = new WebPushClient ( ) ;
30
-
31
38
var gcmAPIKey = @"teststring" ;
32
39
var subscription = new PushSubscription ( TestGcmEndpoint , TestPublicKey , TestPrivateKey ) ;
33
40
@@ -50,8 +57,6 @@ public void TestGcmApiKeyInOptions()
50
57
[ TestMethod ]
51
58
public void TestSetGcmApiKey ( )
52
59
{
53
- var client = new WebPushClient ( ) ;
54
-
55
60
var gcmAPIKey = @"teststring" ;
56
61
client . SetGcmApiKey ( gcmAPIKey ) ;
57
62
var subscription = new PushSubscription ( TestGcmEndpoint , TestPublicKey , TestPrivateKey ) ;
@@ -64,16 +69,13 @@ public void TestSetGcmApiKey()
64
69
[ TestMethod ]
65
70
public void TestSetGCMAPIKeyEmptyString ( )
66
71
{
67
- var client = new WebPushClient ( ) ;
68
-
69
72
Assert . ThrowsException < ArgumentException > ( delegate { client . SetGcmApiKey ( "" ) ; } ) ;
70
73
}
71
74
72
75
[ TestMethod ]
73
76
public void TestSetGcmApiKeyNonGcmPushService ( )
74
77
{
75
78
// Ensure that the API key doesn't get added on a service that doesn't accept it.
76
- var client = new WebPushClient ( ) ;
77
79
78
80
var gcmAPIKey = @"teststring" ;
79
81
client . SetGcmApiKey ( gcmAPIKey ) ;
@@ -87,8 +89,6 @@ public void TestSetGcmApiKeyNonGcmPushService()
87
89
[ TestMethod ]
88
90
public void TestSetGcmApiKeyNull ( )
89
91
{
90
- var client = new WebPushClient ( ) ;
91
-
92
92
client . SetGcmApiKey ( @"somestring" ) ;
93
93
client . SetGcmApiKey ( null ) ;
94
94
@@ -102,9 +102,7 @@ public void TestSetGcmApiKeyNull()
102
102
[ TestMethod ]
103
103
public void TestSetVapidDetails ( )
104
104
{
105
- var client = new WebPushClient ( ) ;
106
-
107
- client . SetVapidDetails ( "mailto:example@example.com" , TestPublicKey , TestPrivateKey ) ;
105
+ client . SetVapidDetails ( TestSubject , TestPublicKey , TestPrivateKey ) ;
108
106
109
107
var subscription = new PushSubscription ( TestFcmEndpoint , TestPublicKey , TestPrivateKey ) ;
110
108
var message = client . GenerateRequestDetails ( subscription , @"test payload" ) ;
@@ -116,15 +114,33 @@ public void TestSetVapidDetails()
116
114
}
117
115
118
116
[ TestMethod ]
119
- public void TestPassingHttpClient ( )
117
+ [ DataRow ( HttpStatusCode . Created ) ]
118
+ [ DataRow ( HttpStatusCode . Accepted ) ]
119
+ public void TestHandlingSuccessHttpCodes ( HttpStatusCode status )
120
120
{
121
- var mockHttp = new MockHttpMessageHandler ( ) ;
122
- mockHttp . When ( TestFcmEndpoint ) . Respond ( HttpStatusCode . Created ) ;
121
+ TestSendNotification ( status ) ;
122
+ }
123
123
124
- var client = new WebPushClient ( mockHttp . ToHttpClient ( ) ) ;
125
- client . SetVapidDetails ( "mailto:example@example.com" , 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 ) ) ;
126
134
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 ) ;
128
144
129
145
client . SendNotification ( subscription , "123" ) ;
130
146
}
0 commit comments