public void Dispatch_WithNancyContext_CallsRequestHandlerWithContext()
        {
            var nancyContext = new NancyContext
            {
                Request = new Request("GET", "/Test", "HTTP")
            };

            var requestDispatcher = GetSubject();

            _mockRequestHandler.Handle(nancyContext).Returns(new Response());

            requestDispatcher.Dispatch(nancyContext, CancellationToken.None);

            _mockRequestHandler.Received(1).Handle(nancyContext);
        }
Exemple #2
0
        public Task <Response> Dispatch(NancyContext context, CancellationToken cancellationToken)
        {
            var tcs = new TaskCompletionSource <Response>();

            if (cancellationToken.IsCancellationRequested)
            {
                tcs.SetException(new OperationCanceledException());
                return(tcs.Task);
            }

            if (context == null)
            {
                tcs.SetException(new ArgumentException("context is null"));
                return(tcs.Task);
            }

            var response = IsAdminRequest(context.Request) ?
                           _adminRequestHandler.Handle(context) :
                           _requestHandler.Handle(context);

            context.Response = response;
            tcs.SetResult(context.Response);

            return(tcs.Task);
        }
        public Task <Response> Dispatch(NancyContext context, CancellationToken cancellationToken)
        {
            var tcs = new TaskCompletionSource <Response>();

            if (cancellationToken.IsCancellationRequested)
            {
                tcs.SetException(new OperationCanceledException());
                return(tcs.Task);
            }

            if (context == null)
            {
                tcs.SetException(new ArgumentException("context is null"));
                return(tcs.Task);
            }

            Response response;

            try
            {
                response = IsAdminRequest(context.Request) ?
                           _adminRequestHandler.Handle(context) :
                           _requestHandler.Handle(context);
            }
            catch (Exception ex)
            {
                if (ex.GetType() != typeof(PactFailureException))
                {
                    _log.ErrorException("Failed to handle the request", ex);
                }

                var exceptionMessage = String.Format("{0} See {1} for details.",
                                                     JsonConvert.ToString(ex.Message).Trim('"'),
                                                     !String.IsNullOrEmpty(_pactConfig.LoggerName) ? LogProvider.CurrentLogProvider.ResolveLogPath(_pactConfig.LoggerName) : "logs");

                response = new Response
                {
                    StatusCode   = HttpStatusCode.InternalServerError,
                    ReasonPhrase = exceptionMessage,
                    Contents     = s =>
                    {
                        var bytes = Encoding.UTF8.GetBytes(exceptionMessage);
                        s.Write(bytes, 0, bytes.Length);
                        s.Flush();
                    }
                };
            }

            context.Response = response;
            tcs.SetResult(context.Response);

            return(tcs.Task);
        }
Exemple #4
0
        public Task <Response> Dispatch(NancyContext context, CancellationToken cancellationToken)
        {
            var tcs = new TaskCompletionSource <Response>();

            if (cancellationToken.IsCancellationRequested)
            {
                tcs.SetException(new OperationCanceledException());
                return(tcs.Task);
            }

            if (context == null)
            {
                tcs.SetException(new ArgumentException("context is null"));
                return(tcs.Task);
            }

            Response response;

            try
            {
                response = IsAdminRequest(context.Request) ?
                           _adminRequestHandler.Handle(context) :
                           _requestHandler.Handle(context);
            }
            catch (Exception ex)
            {
                var exceptionMessage = ex.Message
                                       .Replace("\r", " ")
                                       .Replace("\n", "")
                                       .Replace("\t", " ")
                                       .Replace(@"\", "");

                response = new Response
                {
                    StatusCode   = HttpStatusCode.InternalServerError,
                    ReasonPhrase = exceptionMessage,
                    Contents     = s =>
                    {
                        var bytes = Encoding.UTF8.GetBytes(exceptionMessage);
                        s.Write(bytes, 0, bytes.Length);
                        s.Flush();
                    }
                };
            }

            context.Response = response;
            tcs.SetResult(context.Response);

            return(tcs.Task);
        }