// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections.Generic; using System.Linq; using System.Reactive.Linq; using System.Threading.Channels; using System.Threading.Tasks; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http.Connections; using Microsoft.AspNetCore.Http.Features; namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests { public class TestHub : Hub { public string HelloWorld() => TestHubMethodsImpl.HelloWorld(); public string Echo(string message) => TestHubMethodsImpl.Echo(message); public IObservable Stream(int count) => TestHubMethodsImpl.Stream(count); public ChannelReader StreamException() => TestHubMethodsImpl.StreamException(); public ChannelReader StreamBroken() => TestHubMethodsImpl.StreamBroken(); public async Task CallEcho(string message) { await Clients.Client(Context.ConnectionId).SendAsync("Echo", message); } public async Task CallHandlerThatDoesntExist() { await Clients.Client(Context.ConnectionId).SendAsync("NoClientHandler"); } public IEnumerable GetHeaderValues(string[] headerNames) { var context = Context.GetHttpContext(); if (context == null) { throw new InvalidOperationException("Unable to get HttpContext from request."); } var headers = context.Request.Headers; if (headers == null) { throw new InvalidOperationException("Unable to get headers from context."); } return headerNames.Select(h => (string)headers[h]); } public string GetCookieValue(string cookieName) { return Context.GetHttpContext().Request.Cookies[cookieName]; } public object[] GetIHttpConnectionFeatureProperties() { var feature = Context.Features.Get(); object[] result = { feature.LocalPort, feature.RemotePort, feature.LocalIpAddress.ToString(), feature.RemoteIpAddress.ToString() }; return result; } public string GetActiveTransportName() { return Context.Items[ConnectionMetadataNames.Transport].ToString(); } } public class DynamicTestHub : DynamicHub { public string HelloWorld() => TestHubMethodsImpl.HelloWorld(); public string Echo(string message) => TestHubMethodsImpl.Echo(message); public IObservable Stream(int count) => TestHubMethodsImpl.Stream(count); public ChannelReader StreamException() => TestHubMethodsImpl.StreamException(); public ChannelReader StreamBroken() => TestHubMethodsImpl.StreamBroken(); public async Task CallEcho(string message) { await Clients.Client(Context.ConnectionId).Echo(message); } public async Task CallHandlerThatDoesntExist() { await Clients.Client(Context.ConnectionId).NoClientHandler(); } } public class TestHubT : Hub { public string HelloWorld() => TestHubMethodsImpl.HelloWorld(); public string Echo(string message) => TestHubMethodsImpl.Echo(message); public IObservable Stream(int count) => TestHubMethodsImpl.Stream(count); public ChannelReader StreamException() => TestHubMethodsImpl.StreamException(); public ChannelReader StreamBroken() => TestHubMethodsImpl.StreamBroken(); public async Task CallEcho(string message) { await Clients.Client(Context.ConnectionId).Echo(message); } public async Task CallHandlerThatDoesntExist() { await Clients.Client(Context.ConnectionId).NoClientHandler(); } } internal static class TestHubMethodsImpl { public static string HelloWorld() { return "Hello World!"; } public static string Echo(string message) { return message; } public static IObservable Stream(int count) { return Observable.Interval(TimeSpan.FromMilliseconds(1)) .Select((_, index) => index) .Take(count); } public static ChannelReader StreamException() { throw new InvalidOperationException("Error occurred while streaming."); } public static ChannelReader StreamBroken() => null; } public interface ITestHub { Task Echo(string message); Task Send(string message); Task NoClientHandler(); } [Authorize(JwtBearerDefaults.AuthenticationScheme)] public class HubWithAuthorization : Hub { public string Echo(string message) => TestHubMethodsImpl.Echo(message); } }