using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
namespace AsbCloudWebApi.Controllers
{
///
/// Имитирует разные типы ответа сервера
///
[Route("api/[controller]")]
[ApiController]
public class MockController : ControllerBase
{
///
/// имитирует http-400
///
[HttpGet("400")]
[ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)]
public IActionResult Get400([FromQuery, Required]IDictionary args)
{
var errors = new Dictionary();
foreach (var arg in args)
{
var countOfErrors = ((arg.Key + arg.Value).Length % 3) + 1;
var errorsText = Enumerable.Range(0, countOfErrors)
.Select(i => $"{arg.Value} не соответствует критериям проверки № {i}");
errors.Add(arg.Key, errorsText.ToArray());
}
if (errors.Any())
{
var problem = new ValidationProblemDetails(errors);
return BadRequest(problem);
}
else
{
var problem = new ValidationProblemDetails { Detail = "at least one argument must be provided" };
return BadRequest(problem);
}
}
///
/// имитирует http-403
///
[HttpGet("403")]
public IActionResult Get403()
{
return Forbid();
}
///
/// имитирует http-401
///
[HttpGet("401")]
public IActionResult Get401()
{
return Unauthorized();
}
///
/// имитирует http-500
///
[HttpGet("500")]
public IActionResult Get500()
{
throw new System.Exception("Это тестовое исключение");
}
}
}