コード例 #1
0
        public async Task SendToAll_CallsAzureSignalRService()
        {
            var connectionString = "Endpoint=https://foo.service.signalr.net;AccessKey=/abcdefghijklmnopqrstu/v/wxyz11111111111111=;Version=1.0;";
            var hubName          = "chat";
            var requestHandler   = new FakeHttpMessageHandler();
            var httpClient       = new HttpClient(requestHandler);
            var azureSignalR     = new AzureSignalRClient(connectionString, httpClient);

            await azureSignalR.SendToAll(hubName, new SignalRData
            {
                Target    = "newMessage",
                Arguments = new object[] { "arg1", "arg2" }
            });

            const string expectedEndpoint = "https://foo.service.signalr.net/api/v1/hubs/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 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);
        }