コード例 #1
0
        public async Task ExecuteWhenDeleteSubscriptionCalledReturnsNullActionContextParameter()
        {
            //Arrange
            A.CallTo(() => subscriptionRegistrationService.DeleteSubscription(A <string> .Ignored)).Throws <RestException>();
            A.CallTo(() => _request.Method).Returns("DELETE");
            //Execute async manually - happens automatically in request pipeline
            var context = new ActionContext();

            context.HttpContext = A.Fake <HttpContext>();

            //Act
            StatusCodeResult result = (StatusCodeResult) await RunFunction("test-subscription-name");

            // Assert
            await Assert.ThrowsAsync <ArgumentNullException>(async() => await result.ExecuteResultAsync(null).ConfigureAwait(false));
        }
コード例 #2
0
 public virtual async Task ExecuteResultAsync(ActionContext context)
 {
     var objectResult = new StatusCodeResult((int)StatusCode);
     await objectResult.ExecuteResultAsync(context);
 }
コード例 #3
0
ファイル: MazeRequestExecuter.cs プロジェクト: 5l1v3r1/Maze-1
        /// <inheritdoc />
        public async Task Execute(MazeContext context, IChannelServer channelServer)
        {
            _logger.LogDebug($"Resolve Maze path {context.Request.Path}");
            var result = _routeResolver.Resolve(context);

            if (!result.Success)
            {
                _logger.LogDebug("Path not found");
                await WriteError(context, BusinessErrors.Commander.RouteNotFound(context.Request.Path),
                                 StatusCodes.Status404NotFound);

                return;
            }

            _logger.LogDebug(
                $"Route resolved (package: {result.RouteDescription.PackageIdentity}). Get cached route info.");
            var route         = _routeCache.Routes[result.RouteDescription];
            var actionContext = new DefaultActionContext(context, route, result.Parameters.ToImmutableDictionary());

            _logger.LogDebug($"Invoke method {route.RouteMethod}");

            IActionResult actionResult;

            try
            {
                switch (route.RouteType)
                {
                case RouteType.Http:
                    actionResult = await route.ActionInvoker.Value.Invoke(actionContext);

                    break;

                case RouteType.ChannelInit:
                    _logger.LogDebug("Create channel {channelName}", actionContext.Route.ControllerType.FullName);
                    var channel = await route.ActionInvoker.Value.InitializeChannel(actionContext, channelServer);

                    context.Response.Headers.Add(HeaderNames.Location, "ws://channels/" + channel.ChannelId);
                    actionResult = new StatusCodeResult(StatusCodes.Status201Created);
                    break;

                case RouteType.Channel:
                    var channelId = int.Parse(actionContext.Context.Request.Headers["ChannelId"]);
                    _logger.LogDebug("Request channel with id {channelId}", channelId);

                    var foundChannel = channelServer.GetChannel(channelId);
                    actionResult =
                        await route.ActionInvoker.Value.InvokeChannel(actionContext, (MazeChannel)foundChannel);

                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
            catch (Exception e)
            {
                if (context.RequestAborted.IsCancellationRequested)
                {
                    return;
                }

                _logger.LogError(e,
                                 $"Error occurred when invoking method {route.RouteMethod} of package {result.RouteDescription.PackageIdentity} (path: {context.Request.Path})");
                await WriteError(context,
                                 BusinessErrors.Commander.ActionError(e.GetType().Name, route.RouteMethod.Name, e.Message),
                                 StatusCodes.Status500InternalServerError);

                return;
            }

            if (context.RequestAborted.IsCancellationRequested)
            {
                return;
            }

            try
            {
                await actionResult.ExecuteResultAsync(actionContext);
            }
            catch (Exception e)
            {
                if (context.RequestAborted.IsCancellationRequested)
                {
                    return;
                }

                _logger.LogError(e,
                                 $"Error occurred when executing action result {route.RouteMethod} of package {result.RouteDescription.PackageIdentity} (path: {context.Request.Path})");
                await WriteError(context,
                                 BusinessErrors.Commander.ResultExecutionError(e.GetType().Name, actionResult?.GetType().Name,
                                                                               e.Message), StatusCodes.Status500InternalServerError);

                return;
            }

            _logger.LogDebug("Request successfully executed.");
        }