Skip to content

Commit

Permalink
test SendToUser
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustavo Armenta committed Aug 15, 2013
1 parent e0a561b commit 936a788
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void Reset()

public Task LongRunningMethod(int i)
{
Clients.Caller.serverIsWaiting(i).Wait();
Clients.Caller.serverIsWaiting(i);

return Task.Run(() =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,62 @@ public async Task CanSendViaUserWhenPrincipalSet()
}
}

[Fact]
public async Task SendToUser()
{
using (var host = CreateHost(HostType.IISExpress))
{
host.Initialize();

var connection1 = CreateHubConnection(host, path: "/basicauth/signalr", useDefaultUrl: false);
var connection2 = CreateHubConnection(host, path: "/basicauth/signalr", useDefaultUrl: false);
var connection3 = CreateHubConnection(host, path: "/basicauth/signalr", useDefaultUrl: false);
var connection4 = CreateHubConnection(host, path: "/basicauth/signalr", useDefaultUrl: false);
connection1.Credentials = new System.Net.NetworkCredential("user1", "password");
connection2.Credentials = new System.Net.NetworkCredential("user1", "password");
connection3.Credentials = new System.Net.NetworkCredential("user1", "password");
connection4.Credentials = new System.Net.NetworkCredential("user2", "password");

var wh1 = new ManualResetEventSlim();
var wh2 = new ManualResetEventSlim();
var wh3 = new ManualResetEventSlim();
var wh4 = new ManualResetEventSlim();

var hub1 = connection1.CreateHubProxy("AuthenticatedEchoHub");
var hub2 = connection2.CreateHubProxy("AuthenticatedEchoHub");
var hub3 = connection3.CreateHubProxy("AuthenticatedEchoHub");
var hub4 = connection4.CreateHubProxy("AuthenticatedEchoHub");
hub1.On("echo", () => wh1.Set());
hub2.On("echo", () => wh2.Set());
hub3.On("echo", () => wh3.Set());
hub4.On("echo", () => wh4.Set());

using (connection1)
{
using (connection2)
{
using (connection3)
{
using (connection4)
{
await connection1.Start(new Microsoft.AspNet.SignalR.Client.Transports.WebSocketTransport());
await connection2.Start(new Microsoft.AspNet.SignalR.Client.Transports.ServerSentEventsTransport());
await connection3.Start(new Microsoft.AspNet.SignalR.Client.Transports.LongPollingTransport());
await connection4.Start();

await hub4.Invoke("SendToUser", "user1", "message");

Assert.True(wh1.Wait(TimeSpan.FromSeconds(5)));
Assert.True(wh2.Wait(TimeSpan.FromSeconds(5)));
Assert.True(wh3.Wait(TimeSpan.FromSeconds(5)));
Assert.False(wh4.Wait(TimeSpan.FromSeconds(5)));
}
}
}
}
}
}

[Theory]
[InlineData(HostType.IISExpress, TransportType.ServerSentEvents)]
[InlineData(HostType.IISExpress, TransportType.LongPolling)]
Expand Down
5 changes: 5 additions & 0 deletions tests/Microsoft.AspNet.SignalR.Tests.Common/Hubs/EchoHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,10 @@ public string EchoReturn(string message)
{
return message;
}

public Task SendToUser(string userId, string message)
{
return Clients.User(userId).echo(message);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Security.Claims;
using System;
using System.Security.Claims;
using System.Security.Principal;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -10,7 +11,7 @@ public class BasicAuthenticationProvider : IBasicAuthenticationProvider
{
public Task<IPrincipal> AuthenticateAsync(string userName, string password, CancellationToken cancellationToken)
{
if (userName == "user" && password == "password")
if (!String.IsNullOrEmpty(userName) && password == "password")
{
var identity = new ClaimsIdentity("Basic");
identity.AddClaim(new Claim(ClaimTypes.Name, userName));
Expand Down

0 comments on commit 936a788

Please sign in to comment.