Example #1
0
 public void CreateWriter_ResultIsNull_ThrowsArgumentNullException(INdjsonWriterFactory ndjsonWriterFactory)
 {
     Assert.Throws <ArgumentNullException>("result", () =>
     {
         ndjsonWriterFactory.CreateWriter <ValueType>(PrepareActionContext(), null);
     });
 }
Example #2
0
 public void CreateWriter_ContextIsNull_ThrowsArgumentNullException(INdjsonWriterFactory ndjsonWriterFactory)
 {
     Assert.Throws <ArgumentNullException>("context", () =>
     {
         ndjsonWriterFactory.CreateWriter <ValueType>(null, OK_RESULT);
     });
 }
Example #3
0
        public void CreateWriter_CreatesWriter(INdjsonWriterFactory ndjsonWriterFactory)
        {
            ActionContext actionContext = PrepareActionContext();

            INdjsonWriter <ValueType> ndjsonWriter = ndjsonWriterFactory.CreateWriter <ValueType>(actionContext, OK_RESULT);

            Assert.NotNull(ndjsonWriter);
        }
Example #4
0
        public void CreateWriter_ResponseStatusCodeIsProvided(INdjsonWriterFactory ndjsonWriterFactory)
        {
            ActionContext actionContext = PrepareActionContext();

            ndjsonWriterFactory.CreateWriter <ValueType>(actionContext, OK_RESULT);

            Assert.Equal(OK_RESULT.StatusCode, actionContext.HttpContext.Response.StatusCode);
        }
Example #5
0
        public void CreateWriter_ResponseContentTypeIsSetToNdjsonWithUtf8Encoding(INdjsonWriterFactory ndjsonWriterFactory)
        {
            ActionContext actionContext = PrepareActionContext();

            ndjsonWriterFactory.CreateWriter <ValueType>(actionContext, OK_RESULT);

            Assert.Equal(CONTENT_TYPE, actionContext.HttpContext.Response.ContentType);
        }
Example #6
0
        public void CreateWriter_DisablesResponseBuffering(INdjsonWriterFactory ndjsonWriterFactory)
        {
            Mock <StreamResponseBodyFeature> httpResponseBodyFeatureMock = new (Stream.Null);
            ActionContext actionContext = PrepareActionContext(httpResponseBodyFeatureMock.Object);

            ndjsonWriterFactory.CreateWriter <ValueType>(actionContext, OK_RESULT);

            httpResponseBodyFeatureMock.Verify(m => m.DisableBuffering(), Times.Once);
        }
        public override async Task ExecuteResultAsync(ActionContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            INdjsonWriterFactory ndjsonTextWriterFactory = context.HttpContext.RequestServices.GetRequiredService <INdjsonWriterFactory>();

            using (_ndjsonTextWriter = ndjsonTextWriterFactory.CreateWriter(context, this))
            {
                _readyTaskCompletionSource.SetResult(true);

                await _completeTaskCompletionSource.Task;
            }
        }
Example #8
0
        /// <summary>
        /// Executes the result operation of the action method asynchronously. This method is called by MVC to process the result of an action method.
        /// </summary>
        /// <param name="context">The <see cref="ActionContext"/> in which the result is executed. The context information includes information about the action that was executed and request information.</param>
        /// <returns>A <see cref="Task"/> that represents the asynchronous execute operation.</returns>
        public override async Task ExecuteResultAsync(ActionContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            INdjsonWriterFactory ndjsonTextWriterFactory = context.HttpContext.RequestServices.GetRequiredService <INdjsonWriterFactory>();

            using (INdjsonWriter ndjsonTextWriter = ndjsonTextWriterFactory.CreateWriter(context, this))
            {
                await foreach (object value in _values)
                {
                    await ndjsonTextWriter.WriteAsync(value);
                }
            }
        }
        /// <summary>
        /// Executes the result operation of the action method asynchronously. This method is called by MVC to process the result of an action method.
        /// </summary>
        /// <param name="context">The <see cref="ActionContext"/> in which the result is executed. The context information includes information about the action that was executed and request information.</param>
        /// <returns>A <see cref="Task"/> that represents the asynchronous execute operation.</returns>
        public override async Task ExecuteResultAsync(ActionContext context)
        {
            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            INdjsonWriterFactory ndjsonTextWriterFactory = context.HttpContext.RequestServices.GetRequiredService <INdjsonWriterFactory>();

            using INdjsonWriter <T> ndjsonTextWriter = ndjsonTextWriterFactory.CreateWriter <T>(context, this);

            try
            {
                await foreach (T value in _values.WithCancellation(context.HttpContext.RequestAborted))
                {
                    await ndjsonTextWriter.WriteAsync(value, context.HttpContext.RequestAborted);
                }
            }
            catch (OperationCanceledException) when(context.HttpContext.RequestAborted.IsCancellationRequested)
            {
            }
        }