public void AzureSignalRClient_ParsesConnectionString() { var azureSignalR = new AzureSignalRClient("Endpoint=https://foo.service.signalr.net;AccessKey=/abcdefghijklmnopqrstu/v/wxyz11111111111111=;", null); Assert.Equal("https://foo.service.signalr.net", azureSignalR.BaseEndpoint); Assert.Equal("/abcdefghijklmnopqrstu/v/wxyz11111111111111=", azureSignalR.AccessKey); }
public async Task SendMessage_CallsAzureSignalRService() { var connectionString = "Endpoint=https://foo.service.signalr.net;AccessKey=/abcdefghijklmnopqrstu/v/wxyz11111111111111=;"; var hubName = "chat"; var requestHandler = new FakeHttpMessageHandler(); var httpClient = new HttpClient(requestHandler); var azureSignalR = new AzureSignalRClient(connectionString, httpClient); await azureSignalR.SendMessage(hubName, new SignalRMessage { Target = "newMessage", Arguments = new object[] { "arg1", "arg2" } }); const string expectedEndpoint = "https://foo.service.signalr.net:5002/api/v1-preview/hub/chat"; var request = requestHandler.HttpRequestMessage; Assert.Equal("application/json", request.Content.Headers.ContentType.MediaType); Assert.Equal(expectedEndpoint, request.RequestUri.AbsoluteUri); var actualRequestBody = JsonConvert.DeserializeObject <SignalRMessage>(await request.Content.ReadAsStringAsync()); Assert.Equal("newMessage", actualRequestBody.Target); Assert.Equal("arg1", actualRequestBody.Arguments[0]); Assert.Equal("arg2", actualRequestBody.Arguments[1]); var authorizationHeader = request.Headers.Authorization; Assert.Equal("Bearer", authorizationHeader.Scheme); TestHelpers.EnsureValidAccessToken( audience: expectedEndpoint, signingKey: "/abcdefghijklmnopqrstu/v/wxyz11111111111111=", accessToken: authorizationHeader.Parameter); }
public void GetClientConnectionInfo() { var hubName = "TestHub"; var hubUrl = "http://localhost"; var accessKey = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; var connectionString = $"Endpoint={hubUrl};AccessKey={accessKey};Version=1.0;"; var userId = "User"; var idToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"; var expectedName = "John Doe"; var expectedIat = "1516239022"; var claimTypeList = new string[] { "name", "iat" }; var serviceManager = new ServiceManagerBuilder() .WithOptions(o => { o.ConnectionString = connectionString; }) .Build(); var serviceHubContextStore = new ServiceHubContextStore(serviceManager, null); var azureSignalRClient = new AzureSignalRClient(serviceHubContextStore, serviceManager); var connectionInfo = azureSignalRClient.GetClientConnectionInfo(hubName, userId, idToken, claimTypeList); Assert.Equal(connectionInfo.Url, $"{hubUrl}/client/?hub={hubName.ToLower()}"); var claims = new JwtSecurityTokenHandler().ReadJwtToken(connectionInfo.AccessToken).Claims; Assert.Equal(expectedName, GetClaimValue(claims, "name")); Assert.Equal(expectedIat, GetClaimValue(claims, $"{AzureSignalRClient.AzureSignalRUserPrefix}iat")); }
public async Task GetClientConnectionInfo() { var hubName = "TestHub"; var hubUrl = "http://localhost"; var accessKey = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; var connectionString = $"Endpoint={hubUrl};AccessKey={accessKey};Version=1.0;"; var userId = "User"; var idToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"; var expectedName = "John Doe"; var expectedIat = "1516239022"; var claimTypeList = new string[] { "name", "iat" }; var connectionStringKey = Constants.AzureSignalRConnectionStringName; var configDict = new Dictionary <string, string>() { { Constants.ServiceTransportTypeName, "Transient" }, { connectionStringKey, connectionString } }; var configuration = new ConfigurationBuilder().AddInMemoryCollection(configDict).Build(); var serviceManagerStore = new ServiceManagerStore(configuration, NullLoggerFactory.Instance, new TestRouter()); var azureSignalRClient = new AzureSignalRClient(serviceManagerStore, connectionStringKey, hubName); var connectionInfo = await azureSignalRClient.GetClientConnectionInfoAsync(userId, idToken, claimTypeList, null); Assert.Equal(connectionInfo.Url, $"{hubUrl}/client/?hub={hubName.ToLower()}"); var claims = new JwtSecurityTokenHandler().ReadJwtToken(connectionInfo.AccessToken).Claims; Assert.Equal(expectedName, GetClaimValue(claims, "name")); Assert.Equal(expectedIat, GetClaimValue(claims, $"{AzureSignalRClient.AzureSignalRUserPrefix}iat")); }
public void AzureSignalRClient_GetServerConnectionInfo_ReturnsValidInfo() { var azureSignalR = new AzureSignalRClient("Endpoint=https://foo.service.signalr.net;AccessKey=/abcdefghijklmnopqrstu/v/wxyz11111111111111=;", null); var info = azureSignalR.GetServerConnectionInfo("chat"); const string expectedUrl = "https://foo.service.signalr.net:5002/api/v1-preview/hub/chat"; TestHelpers.EnsureValidAccessToken( audience: expectedUrl, signingKey: "/abcdefghijklmnopqrstu/v/wxyz11111111111111=", accessToken: info.AccessToken); Assert.Equal(expectedUrl, info.Url); }
public async Task ServiceEndpointsNotSet() { var rootHubContextMock = new Mock <ServiceHubContext>().As <IInternalServiceHubContext>(); var childHubContextMock = new Mock <ServiceHubContext>().As <IInternalServiceHubContext>(); rootHubContextMock.Setup(c => c.WithEndpoints(It.IsAny <ServiceEndpoint[]>())).Returns(childHubContextMock.Object); rootHubContextMock.Setup(c => c.Clients.All.SendCoreAsync(It.IsAny <string>(), It.IsAny <object[]>(), It.IsAny <CancellationToken>())).Returns(Task.CompletedTask); var serviceManagerStore = Mock.Of <IServiceManagerStore>(s => s.GetOrAddByConnectionStringKey(It.IsAny <string>()).GetAsync(It.IsAny <string>()) == new ValueTask <IServiceHubContext>(rootHubContextMock.Object)); var azureSignalRClient = new AzureSignalRClient(serviceManagerStore, "key", "hub"); var data = new SignalRData { Target = "target", Arguments = new object[] { "arg1" } }; await azureSignalRClient.SendToAll(data); rootHubContextMock.Verify(c => c.Clients.All.SendCoreAsync(data.Target, data.Arguments, default), Times.Once); childHubContextMock.Verify(c => c.Clients.All.SendCoreAsync(It.IsAny <string>(), It.IsAny <object[]>(), It.IsAny <CancellationToken>()), Times.Never); }
public void AzureSignalRClient_GetClientConnectionInfoWithUserId_ReturnsValidInfoWithUserId() { var azureSignalR = new AzureSignalRClient("Endpoint=https://foo.service.signalr.net;AccessKey=/abcdefghijklmnopqrstu/v/wxyz11111111111111=;Version=1.0;", null); var claims = new [] { new Claim(ClaimTypes.NameIdentifier, "foo") }; var info = azureSignalR.GetClientConnectionInfo("chat", claims); const string expectedEndpoint = "https://foo.service.signalr.net/client/?hub=chat"; var claimsPrincipal = TestHelpers.EnsureValidAccessToken( audience: expectedEndpoint, signingKey: "/abcdefghijklmnopqrstu/v/wxyz11111111111111=", accessToken: info.AccessToken); Assert.Contains(claimsPrincipal.Claims, c => c.Type == ClaimTypes.NameIdentifier && c.Value == "foo"); Assert.Equal(expectedEndpoint, info.Url); }