forked from ddrilling/AsbCloudServer
MockController Add method testing sirnalR
This commit is contained in:
parent
6ef0d62cb9
commit
7436cfd11a
@ -1,17 +1,32 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using AsbCloudApp.Exceptions;
|
||||||
|
using AsbCloudWebApi.SignalR;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.SignalR;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace AsbCloudWebApi.Controllers
|
namespace AsbCloudWebApi.Controllers;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Имитирует разные типы ответа сервера
|
||||||
|
/// </summary>
|
||||||
|
[Route("api/[controller]")]
|
||||||
|
[ApiController]
|
||||||
|
public class MockController : ControllerBase
|
||||||
{
|
{
|
||||||
/// <summary>
|
private readonly IServiceProvider provider;
|
||||||
/// Имитирует разные типы ответа сервера
|
|
||||||
/// </summary>
|
public MockController(IServiceProvider provider)
|
||||||
[Route("api/[controller]")]
|
|
||||||
[ApiController]
|
|
||||||
public class MockController : ControllerBase
|
|
||||||
{
|
{
|
||||||
|
this.provider = provider;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// имитирует http-400
|
/// имитирует http-400
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -68,5 +83,37 @@ namespace AsbCloudWebApi.Controllers
|
|||||||
{
|
{
|
||||||
throw new System.Exception("Это тестовое исключение");
|
throw new System.Exception("Это тестовое исключение");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// имитация отправки SignalR данных
|
||||||
|
/// </summary>
|
||||||
|
/// <example>
|
||||||
|
/// </example>
|
||||||
|
/// <param name="hubName">
|
||||||
|
/// Поддерживаемые hubЫ: wellInfo, notifications, telemetry, reports
|
||||||
|
/// </param>
|
||||||
|
/// <param name="methodName">Название вызываемого на клиенте метода. Прим.:"ReceiveDataSaub". Список методов см. в swagger definition signalr</param>
|
||||||
|
/// <param name="groupName">Группа пользователей. Прим.: "well_1". Если не задана - все пользователи. Шаблон формирования групп см. описание методов в swagger definition signalr</param>
|
||||||
|
/// <param name="body">передаваемая нагрузка. (json)</param>
|
||||||
|
/// <param name="token"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpPost("signalr/hubs/{hubName}/{methodName}/{groupName}")]
|
||||||
|
[Authorize]
|
||||||
|
public async Task<IActionResult> PostAsync(string hubName, string methodName, string? groupName, object body, CancellationToken token)
|
||||||
|
{
|
||||||
|
IHubClients clients = hubName.ToLower() switch {
|
||||||
|
"wellinfo" => provider.GetRequiredService<IHubContext<NotificationHub>>().Clients,
|
||||||
|
"notifications" => provider.GetRequiredService<IHubContext<NotificationHub>>().Clients,
|
||||||
|
"telemetry" => provider.GetRequiredService<IHubContext<TelemetryHub>>().Clients,
|
||||||
|
"reports" => provider.GetRequiredService<IHubContext<ReportsHub>>().Clients,
|
||||||
|
_ => throw new ArgumentInvalidException(nameof(hubName), "hubName does not listed"),
|
||||||
|
};
|
||||||
|
|
||||||
|
IClientProxy selectedClients = string.IsNullOrEmpty(groupName)
|
||||||
|
? clients.All
|
||||||
|
: clients.Group(groupName);
|
||||||
|
|
||||||
|
await selectedClients.SendAsync(methodName, body, token);
|
||||||
|
return Ok();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user