Skip to content

Commit 7ebcdb7

Browse files
committed
#53 - Allow HttpClient to be passed via constructor.
1 parent ea0014d commit 7ebcdb7

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

WebPush.Test/WebPush.Test.csproj

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

33
<PropertyGroup>
44
<TargetFrameworks>netcoreapp1.0;netcoreapp1.1;netcoreapp2.0</TargetFrameworks>
@@ -7,8 +7,10 @@
77

88
<ItemGroup>
99
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
10+
<PackageReference Include="Moq" Version="4.10.1" />
1011
<PackageReference Include="MSTest.TestAdapter" Version="1.2.0" />
1112
<PackageReference Include="MSTest.TestFramework" Version="1.2.0" />
13+
<PackageReference Include="RichardSzalay.MockHttp" Version="5.0.0" />
1214
</ItemGroup>
1315

1416
<ItemGroup>

WebPush.Test/WebPushClientTest.cs

+19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Net;
5+
using System.Net.Http;
6+
using System.Threading.Tasks;
47
using Microsoft.VisualStudio.TestTools.UnitTesting;
8+
using Moq;
9+
using RichardSzalay.MockHttp;
510

611
namespace WebPush.Test
712
{
@@ -109,5 +114,19 @@ public void TestSetVapidDetails()
109114
Assert.IsTrue(authorizationHeader.StartsWith(@"WebPush "));
110115
Assert.IsTrue(cryptoHeader.Contains(@"p256ecdsa"));
111116
}
117+
118+
[TestMethod]
119+
public void TestPassingHttpClient()
120+
{
121+
var mockHttp = new MockHttpMessageHandler();
122+
mockHttp.When(TestFcmEndpoint).Respond(HttpStatusCode.Created);
123+
124+
var client = new WebPushClient(mockHttp.ToHttpClient());
125+
client.SetVapidDetails("mailto:[email protected]", TestPublicKey, TestPrivateKey);
126+
127+
var subscription = new PushSubscription(TestFcmEndpoint, TestPublicKey, TestPrivateKey);
128+
129+
client.SendNotification(subscription, "123");
130+
}
112131
}
113132
}

WebPush/WebPushClient.cs

+11-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,17 @@ public class WebPushClient
2121
private HttpClient _httpClient;
2222
private VapidDetails _vapidDetails;
2323

24-
public WebPushClient(HttpClientHandler httpClientHandler=null)
24+
public WebPushClient()
25+
{
26+
27+
}
28+
29+
public WebPushClient(HttpClient httpClient)
30+
{
31+
_httpClient = httpClient;
32+
}
33+
34+
public WebPushClient(HttpClientHandler httpClientHandler)
2535
{
2636
_httpClientHandler = httpClientHandler;
2737
}

0 commit comments

Comments
 (0)