Skip to content

Commit

Permalink
More code to spec, starting to add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Redth committed May 16, 2013
1 parent 9fde7cb commit 4308012
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 25 deletions.
26 changes: 26 additions & 0 deletions PushSharp.Google.Chrome/ChromePushChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@

using PushSharp.Core;
using System.Net.Http;
using System.Threading.Tasks;

namespace PushSharp.Google.Chrome
{
public class ChromePushChannel : IPushChannel
{
ChromePushChannelSettings chromeSettings = null;

public string AccessToken { get; private set; }
public DateTime Expires { get; private set; }


public ChromePushChannel(ChromePushChannelSettings channelSettings)
{
chromeSettings = channelSettings as ChromePushChannelSettings;
Expand All @@ -27,6 +32,27 @@ static ChromePushChannel()
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, policyErrs) => { return true; };
}

public void RefreshToken()
{
RefreshAccessToken ();
}

public void RefreshAccessToken()
{
var http = new HttpClient ();

var p = new Dictionary<string, string> ();

p.Add ("client_id", chromeSettings.ClientId);
p.Add ("client_secret", chromeSettings.ClientSecret);
p.Add ("refresh_token", chromeSettings.RefreshToken);
p.Add ("grant_type", chromeSettings.GrantType);

var response = http.PostAsync (chromeSettings.AuthUrl, new FormUrlEncodedContent (p)).Result;

Console.WriteLine ("RESPONSE: " + response.Content.ReadAsStringAsync().Result);
}

public async void SendNotification(INotification notification, SendNotificationCallbackDelegate callback)
{
bool success = false;
Expand Down
12 changes: 10 additions & 2 deletions PushSharp.Google.Chrome/ChromePushChannelSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,34 @@ namespace PushSharp.Google.Chrome
public class ChromePushChannelSettings : IPushChannelSettings
{
private const string CHROME_SEND_URL = "https://www.googleapis.com/gcm_for_chrome/v1/messages";
private const string CHROME_AUTH_URL = "https://accounts.google.com/o/auth2/auth";

public ChromePushChannelSettings(string clientId, string clientSecret)
{
this.ClientId = clientId;
this.ClientSecret = clientSecret;

this.Url = CHROME_SEND_URL;
this.AuthUrl = CHROME_AUTH_URL;
}

public string ClientId { get; private set; }
public string ClientSecret { get; private set; }

internal string RefreshToken { get; set; }
internal string GrantType { get; set; }
public string RefreshToken { get; set; }
public string GrantType { get; set; }

public string Url { get; private set; }
public string AuthUrl { get; private set; }

public void OverrideUrl(string url)
{
Url = url;
}

public void OverrideAuthUrl(string url)
{
AuthUrl = url;
}
}
}
20 changes: 2 additions & 18 deletions PushSharp.Google.Chrome/PushSharp.Google.Chrome.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<OutputType>Library</OutputType>
<RootNamespace>PushSharp.Google.Chrome</RootNamespace>
<AssemblyName>PushSharp.Google.Chrome</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -31,25 +32,8 @@
<Reference Include="System.Runtime">
<HintPath>..\packages\Microsoft.Bcl.1.0.19\lib\net40\System.Runtime.dll</HintPath>
</Reference>
<Reference Include="System.Threading.Tasks">
<HintPath>..\packages\Microsoft.Bcl.1.0.19\lib\net40\System.Threading.Tasks.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Threading.Tasks.Extensions.Desktop">
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.16\lib\net40\Microsoft.Threading.Tasks.Extensions.Desktop.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Threading.Tasks.Extensions">
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.16\lib\net40\Microsoft.Threading.Tasks.Extensions.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Threading.Tasks">
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.16\lib\net40\Microsoft.Threading.Tasks.dll</HintPath>
</Reference>
<Reference Include="System.Net" />
<Reference Include="System.Net.Http.WebRequest">
<HintPath>..\packages\Microsoft.Net.Http.2.0.20710.0\lib\net40\System.Net.Http.WebRequest.dll</HintPath>
</Reference>
<Reference Include="System.Net.Http">
<HintPath>..\packages\Microsoft.Net.Http.2.0.20710.0\lib\net40\System.Net.Http.dll</HintPath>
</Reference>
<Reference Include="System.Net.Http" />
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
2 changes: 1 addition & 1 deletion Tests/PushSharp.Tests/CoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void Setup()
public void TestAppleRegistration()
{
broker.RegisterAppleService(new ApplePushChannelSettings(false, appleCert, "pushsharp"));
Assert.IsNotEmpty(broker.GetRegistrations<AppleNotification>());
Assert.IsNotEmpty(broker.GetRegistrations<AppleNotification>().ToString());
}
}
}
28 changes: 28 additions & 0 deletions Tests/PushSharp.Tests/GcmChromeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Threading.Tasks;
using NUnit.Framework;

namespace PushSharp.Tests
{
public class GcmChromeTests
{
const string oauthClientId = "785671162406-hdjh5dfe38bp2b6c2quvh2oelq0ie48i.apps.googleusercontent.com";
const string oauthSecret = "GgYTVbDxX4JogGSeh9IxS83f";
const string redirectUrl = "https://developers.google.com/oauthplayground";

public GcmChromeTests ()
{
}

[Test]
public void TestOAuth()
{
Google.Chrome.ChromePushChannel chan = new PushSharp.Google.Chrome.ChromePushChannel (new PushSharp.Google.Chrome.ChromePushChannelSettings(oauthClientId, oauthSecret)
{
GrantType = "4/m11YZRjo5yoYyuy3Wx8bIY1NtN3I.kvLUtsadF1YSuJJVnL49Cc_yg3RGfQI", RefreshToken = "1/737Kzd3sjIr8ME97HYam3fPW8euce6lHeP800RUXl8Y"
});
chan.RefreshAccessToken ();
}
}
}

12 changes: 8 additions & 4 deletions Tests/PushSharp.Tests/PushSharp.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>PushSharp.Tests</RootNamespace>
<AssemblyName>PushSharp.Tests</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
<RestorePackages>true</RestorePackages>
<ProductVersion>12.0.0</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -26,7 +26,6 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
Expand All @@ -48,8 +47,8 @@
<Reference Include="Newtonsoft.Json">
<HintPath>..\..\packages\Newtonsoft.Json.4.5.11\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="nunit.framework, Version=2.6.2.12296, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77">
<HintPath>..\..\packages\NUnit.2.6.2\lib\nunit.framework.dll</HintPath>
<Reference Include="nunit.framework, Version=2.4.8.0, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77">
<Private>False</Private>
</Reference>
</ItemGroup>
<ItemGroup>
Expand All @@ -60,6 +59,7 @@
<Compile Include="PushServiceTests.cs" />
<Compile Include="TestServers\GcmTestServer.cs" />
<Compile Include="TestServers\ApnsTestServer.cs" />
<Compile Include="GcmChromeTests.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand All @@ -85,6 +85,10 @@
<Project>{0EC3A31E-B869-4465-ABDC-90C2E3CCC17D}</Project>
<Name>PushSharp.Windows</Name>
</ProjectReference>
<ProjectReference Include="..\..\PushSharp.Google.Chrome\PushSharp.Google.Chrome.csproj">
<Project>{84961658-42B9-4943-B738-ABED75EDE303}</Project>
<Name>PushSharp.Google.Chrome</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
Expand Down
3 changes: 3 additions & 0 deletions Tests/PushSharp.Tests/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Bcl" version="1.0.19" targetFramework="net45" />
<package id="Microsoft.Bcl.Async" version="1.0.16" targetFramework="net45" />
<package id="Microsoft.Bcl.Build" version="1.0.5" targetFramework="net45" />
<package id="Moq" version="4.0.10827" targetFramework="net45" />
<package id="Newtonsoft.Json" version="4.5.11" targetFramework="net45" />
<package id="NUnit" version="2.6.2" targetFramework="net45" />
Expand Down

0 comments on commit 4308012

Please sign in to comment.