Skip to content

Commit 69eaa49

Browse files
committed
#79 Implement an interface for mocking in UnitTests
1 parent 77eb971 commit 69eaa49

File tree

5 files changed

+129
-6
lines changed

5 files changed

+129
-6
lines changed

WebPush.Test/WebPush.Test.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netcoreapp1.0;netcoreapp1.1;netcoreapp2.0</TargetFrameworks>
4+
<TargetFrameworks>net45;net46;net471;net48;net5.0;netcoreapp2.0;netcoreapp3.1</TargetFrameworks>
55
<IsPackable>false</IsPackable>
66
</PropertyGroup>
77

WebPush.Test/WebPushClientTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ private void TestSendNotification(HttpStatusCode status, string response=null)
165165
{
166166
var subscription = new PushSubscription(TestFcmEndpoint, TestPublicKey, TestPrivateKey);
167167
var httpContent = response == null ? null : new StringContent(response);
168-
httpMessageHandlerMock.When(TestFcmEndpoint).Respond(new HttpResponseMessage { StatusCode = status, Content = httpContent });
168+
httpMessageHandlerMock.When(TestFcmEndpoint).Respond(req => new HttpResponseMessage { StatusCode = status, Content = httpContent });
169169
client.SetVapidDetails(TestSubject, TestPublicKey, TestPrivateKey);
170170
client.SendNotification(subscription, "123");
171171
}

WebPush/IWebPushClient.cs

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Net.Http;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
7+
namespace WebPush
8+
{
9+
public interface IWebPushClient : IDisposable
10+
{
11+
/// <summary>
12+
/// When sending messages to a GCM endpoint you need to set the GCM API key
13+
/// by either calling setGcmApiKey() or passing in the API key as an option
14+
/// to sendNotification()
15+
/// </summary>
16+
/// <param name="gcmApiKey">The API key to send with the GCM request.</param>
17+
void SetGcmApiKey(string gcmApiKey);
18+
19+
/// <summary>
20+
/// When marking requests where you want to define VAPID details, call this method
21+
/// before sendNotifications() or pass in the details and options to
22+
/// sendNotification.
23+
/// </summary>
24+
/// <param name="vapidDetails"></param>
25+
void SetVapidDetails(VapidDetails vapidDetails);
26+
27+
/// <summary>
28+
/// When marking requests where you want to define VAPID details, call this method
29+
/// before sendNotifications() or pass in the details and options to
30+
/// sendNotification.
31+
/// </summary>
32+
/// <param name="subject">This must be either a URL or a 'mailto:' address</param>
33+
/// <param name="publicKey">The public VAPID key as a base64 encoded string</param>
34+
/// <param name="privateKey">The private VAPID key as a base64 encoded string</param>
35+
void SetVapidDetails(string subject, string publicKey, string privateKey);
36+
37+
/// <summary>
38+
/// To get a request without sending a push notification call this method.
39+
/// This method will throw an ArgumentException if there is an issue with the input.
40+
/// </summary>
41+
/// <param name="subscription">The PushSubscription you wish to send the notification to.</param>
42+
/// <param name="payload">The payload you wish to send to the user</param>
43+
/// <param name="options">
44+
/// Options for the GCM API key and vapid keys can be passed in if they are unique for each
45+
/// notification.
46+
/// </param>
47+
/// <returns>A HttpRequestMessage object that can be sent.</returns>
48+
HttpRequestMessage GenerateRequestDetails(PushSubscription subscription, string payload,
49+
Dictionary<string, object> options = null);
50+
51+
/// <summary>
52+
/// To send a push notification call this method with a subscription, optional payload and any options
53+
/// Will exception if unsuccessful
54+
/// </summary>
55+
/// <param name="subscription">The PushSubscription you wish to send the notification to.</param>
56+
/// <param name="payload">The payload you wish to send to the user</param>
57+
/// <param name="options">
58+
/// Options for the GCM API key and vapid keys can be passed in if they are unique for each
59+
/// notification.
60+
/// </param>
61+
void SendNotification(PushSubscription subscription, string payload = null,
62+
Dictionary<string, object> options = null);
63+
64+
/// <summary>
65+
/// To send a push notification call this method with a subscription, optional payload and any options
66+
/// Will exception if unsuccessful
67+
/// </summary>
68+
/// <param name="subscription">The PushSubscription you wish to send the notification to.</param>
69+
/// <param name="payload">The payload you wish to send to the user</param>
70+
/// <param name="vapidDetails">The vapid details for the notification.</param>
71+
void SendNotification(PushSubscription subscription, string payload, VapidDetails vapidDetails);
72+
73+
/// <summary>
74+
/// To send a push notification call this method with a subscription, optional payload and any options
75+
/// Will exception if unsuccessful
76+
/// </summary>
77+
/// <param name="subscription">The PushSubscription you wish to send the notification to.</param>
78+
/// <param name="payload">The payload you wish to send to the user</param>
79+
/// <param name="gcmApiKey">The GCM API key</param>
80+
void SendNotification(PushSubscription subscription, string payload, string gcmApiKey);
81+
82+
/// <summary>
83+
/// To send a push notification asynchronous call this method with a subscription, optional payload and any options
84+
/// Will exception if unsuccessful
85+
/// </summary>
86+
/// <param name="subscription">The PushSubscription you wish to send the notification to.</param>
87+
/// <param name="payload">The payload you wish to send to the user</param>
88+
/// <param name="options">
89+
/// Options for the GCM API key and vapid keys can be passed in if they are unique for each
90+
/// notification.
91+
/// </param>
92+
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
93+
Task SendNotificationAsync(PushSubscription subscription, string payload = null,
94+
Dictionary<string, object> options = null, CancellationToken cancellationToken=default);
95+
96+
/// <summary>
97+
/// To send a push notification asynchronous call this method with a subscription, optional payload and any options
98+
/// Will exception if unsuccessful
99+
/// </summary>
100+
/// <param name="subscription">The PushSubscription you wish to send the notification to.</param>
101+
/// <param name="payload">The payload you wish to send to the user</param>
102+
/// <param name="vapidDetails">The vapid details for the notification.</param>
103+
/// <param name="cancellationToken"></param>
104+
Task SendNotificationAsync(PushSubscription subscription, string payload,
105+
VapidDetails vapidDetails, CancellationToken cancellationToken=default);
106+
107+
/// <summary>
108+
/// To send a push notification asynchronous call this method with a subscription, optional payload and any options
109+
/// Will exception if unsuccessful
110+
/// </summary>
111+
/// <param name="subscription">The PushSubscription you wish to send the notification to.</param>
112+
/// <param name="payload">The payload you wish to send to the user</param>
113+
/// <param name="gcmApiKey">The GCM API key</param>
114+
/// <param name="cancellationToken"></param>
115+
Task SendNotificationAsync(PushSubscription subscription, string payload, string gcmApiKey, CancellationToken cancellationToken=default);
116+
}
117+
}

WebPush/WebPush.csproj

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netstandard1.1;netstandard2.0;net45;net46</TargetFrameworks>
4+
<TargetFrameworks>net45;net46;net471;net48;net5.0;netstandard1.3;netstandard2.0;netstandard2.1</TargetFrameworks>
55
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
66
<Version>1.0.11</Version>
77
<Authors>Cory Thompson</Authors>
@@ -25,7 +25,11 @@
2525
<ItemGroup>
2626
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
2727
<PackageReference Include="Portable.BouncyCastle" Version="1.8.1.3" />
28-
<PackageReference Include="System.Net.Http" Version="4.3.3" />
28+
</ItemGroup>
29+
30+
31+
<ItemGroup>
32+
<Reference Include="System.Net.Http" Condition="'$(TargetFramework)' == 'net46' OR '$(TargetFramework)' == 'net45' OR '$(TargetFramework)' == 'net48' OR '$(TargetFramework)' == 'net471'" />
2933
</ItemGroup>
3034

3135
</Project>

WebPush/WebPushClient.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace WebPush
1414
{
15-
public class WebPushClient : IDisposable
15+
public class WebPushClient : IWebPushClient
1616
{
1717
// default TTL is 4 weeks.
1818
private const int DefaultTtl = 2419200;
@@ -265,7 +265,9 @@ public HttpRequestMessage GenerateRequestDetails(PushSubscription subscription,
265265
public void SendNotification(PushSubscription subscription, string payload = null,
266266
Dictionary<string, object> options = null)
267267
{
268-
SendNotification(subscription, payload, options);
268+
var request = GenerateRequestDetails(subscription, payload, options);
269+
var response = HttpClient.SendAsync(request).Result;
270+
HandleResponse(response, subscription);
269271
}
270272

271273

0 commit comments

Comments
 (0)