예제 #1
0
        public void ExecuteThrowsExceptionWhenResponseStartedTest()
        {
            var mvcOptions    = new MvcOptions();
            var options       = Substitute.For <IOptions <MvcOptions> >();
            var writerFactory = Substitute.For <IHttpResponseStreamWriterFactory>();
            var loggerFactory = Substitute.For <ILoggerFactory>();

            options.Value.Returns(mvcOptions);

            var formatSelector = new DefaultOutputFormatterSelector(options, loggerFactory);

            var executor = new ObjectResultExecutor(formatSelector, writerFactory, loggerFactory);
            var result   = new ObjectResult(Guid.NewGuid().ToString());
            var context  = Substitute.For <HttpContext>();
            var response = Substitute.For <HttpResponse>();

            response.HasStarted.Returns(true);

            context.Response.Returns(response);

            var sut = new ResultExecutor(executor);

            Func <Task> action = async() => await sut.Execute(context, result).ConfigureAwait(false);

            action.Should().Throw <InvalidOperationException>();
        }
예제 #2
0
    private static ObjectResultExecutor CreateExecutor(MvcOptions options = null)
    {
        options ??= new MvcOptions();
        var optionsAccessor = Options.Create(options);
        var selector        = new DefaultOutputFormatterSelector(optionsAccessor, NullLoggerFactory.Instance);

        return(new ObjectResultExecutor(selector, new TestHttpResponseStreamWriterFactory(), NullLoggerFactory.Instance, optionsAccessor));
    }
예제 #3
0
        public void ExecuteThrowsExceptionWithNullContextTest()
        {
            var mvcOptions = new MvcOptions();
            var options    = Substitute.For <IOptions <MvcOptions> >();

            options.Value.Returns(mvcOptions);

            var writerFactory = Substitute.For <IHttpResponseStreamWriterFactory>();
            var loggerFactory = Substitute.For <ILoggerFactory>();

            var formatSelector = new DefaultOutputFormatterSelector(options, loggerFactory);

            var executor = new ObjectResultExecutor(formatSelector, writerFactory, loggerFactory);
            var result   = new ObjectResult(Guid.NewGuid().ToString());

            var sut = new ResultExecutor(executor);

            Func <Task> action = async() => await sut.Execute(null, result).ConfigureAwait(false);

            action.Should().Throw <ArgumentNullException>();
        }
예제 #4
0
        public async Task ExecuteSendsResultToExecutorTest()
        {
            const HttpStatusCode StatusCode = HttpStatusCode.OK;
            var mvcOptions = new MvcOptions();
            var options    = Substitute.For <IOptions <MvcOptions> >();

            mvcOptions.OutputFormatters.Add(new StringOutputFormatter());
            options.Value.Returns(mvcOptions);

            var writerFactory = Substitute.For <IHttpResponseStreamWriterFactory>();
            var loggerFactory = Substitute.For <ILoggerFactory>();

            var formatSelector = new DefaultOutputFormatterSelector(options, loggerFactory);

            var executor = Substitute.For <ObjectResultExecutor>(formatSelector, writerFactory, loggerFactory);
            var result   = new ObjectResult(Guid.NewGuid().ToString())
            {
                StatusCode = (int)StatusCode
            };
            var context  = Substitute.For <HttpContext>();
            var response = Substitute.For <HttpResponse>();
            var headers  = new HeaderDictionary();
            var body     = Substitute.For <Stream>();

            response.HasStarted.Returns(false);
            response.HttpContext.Returns(context);
            response.Headers.Returns(headers);
            response.Body.Returns(body);
            context.Response.Returns(response);

            var sut = new ResultExecutor(executor);

            await sut.Execute(context, result).ConfigureAwait(false);

            response.StatusCode.Should().Be((int)StatusCode);
            await executor.Received().ExecuteAsync(
                Arg.Is <ActionContext>(x => x.HttpContext == context),
                Arg.Is <ObjectResult>(x => x == result)).ConfigureAwait(false);
        }
 public AcceptHeaderOutputFormatterSelector(IOptions <MvcOptions> options, ILoggerFactory loggerFactory)
 {
     _fallbackSelector = new DefaultOutputFormatterSelector(options, loggerFactory);
     _formatters       = new List <IOutputFormatter>(options.Value.OutputFormatters);
 }
예제 #6
0
 public AcceptHeaderOutputFormatterSelector(IOptions <MvcOptions> options, ILoggerFactory loggerFactory, string defaultContentType = "application/json")
 {
     _fallbackSelector   = new DefaultOutputFormatterSelector(options, loggerFactory);
     _formatters         = new List <IOutputFormatter>(options.Value.OutputFormatters);
     _defaultContentType = defaultContentType;
 }