public async Task Test_Write_WritesBody()
        {
            var ms           = new MemoryStream();
            var resposneMock = new Mock <HttpResponse>();

            resposneMock.SetupGet(x => x.Body)
            .Returns(ms);
            var httpContextMock = new Mock <HttpContext>();

            httpContextMock.SetupGet(x => x.Response)
            .Returns(resposneMock.Object);
            httpContextMock.SetupGet(x => x.RequestAborted)
            .Returns(new CancellationToken());
            var next            = Mock.Of <RequestDelegate>();
            var handlingContext = new HandlingContext(httpContextMock.Object, Encoding.UTF8, next)
            {
                WriteResponse = true
            };
            var sourceResponse = new MemoryStream(new byte[1]);
            var sourceMock     = new Mock <HttpResponse>();

            sourceMock.SetupGet(x => x.StatusCode)
            .Returns(200);
            sourceMock.SetupGet(x => x.Body)
            .Returns(sourceResponse);
            var wrapper = new RawServerResponseWrapper(sourceMock.Object);

            await wrapper.Write(handlingContext, new HeaderJsonRpcSerializer());

            ms.Length.Should().BePositive();
        }
        public async Task Test_Write_ChecksMemoryStream()
        {
            var ms           = new MemoryStream();
            var resposneMock = new Mock <HttpResponse>();

            resposneMock.SetupGet(x => x.Body)
            .Returns(ms);
            var httpContextMock = new Mock <HttpContext>();

            httpContextMock.SetupGet(x => x.Response)
            .Returns(resposneMock.Object);
            httpContextMock.SetupGet(x => x.RequestAborted)
            .Returns(new CancellationToken());
            var next            = Mock.Of <RequestDelegate>();
            var handlingContext = new HandlingContext(httpContextMock.Object, Encoding.UTF8, next)
            {
                WriteResponse = true
            };
            var sourceResponse = Mock.Of <Stream>();
            var sourceMock     = new Mock <HttpResponse>();

            sourceMock.SetupGet(x => x.StatusCode)
            .Returns(200);
            sourceMock.SetupGet(x => x.Body)
            .Returns(sourceResponse);
            var wrapper = new RawServerResponseWrapper(sourceMock.Object);

            Func <Task> action = async() => await wrapper.Write(handlingContext, new HeaderJsonRpcSerializer());

            await action.Should().ThrowAsync <JsonRpcInternalException>();
        }
        public async Task Test_Write_SetsResponseProperties()
        {
            var resposneMock    = new Mock <HttpResponse>();
            var httpContextMock = new Mock <HttpContext>();

            httpContextMock.SetupGet(x => x.Response)
            .Returns(resposneMock.Object);
            var next            = Mock.Of <RequestDelegate>();
            var handlingContext = new HandlingContext(httpContextMock.Object, Encoding.UTF8, next)
            {
                WriteResponse = false
            };
            var sourceMock = new Mock <HttpResponse>();

            sourceMock.SetupGet(x => x.StatusCode)
            .Returns(201);
            var wrapper = new RawServerResponseWrapper(sourceMock.Object);

            await wrapper.Write(handlingContext, new HeaderJsonRpcSerializer());

            resposneMock.VerifySet(x => x.StatusCode    = 201);
            resposneMock.VerifySet(x => x.ContentLength = null);
        }
        public async Task Test_GetResponse_ReturnsRawWhenAllowed()
        {
            var responseMock = new Mock <HttpResponse>();
            var items        = new Dictionary <object, object> {
                [JsonRpcConstants.ActionResultTypeItemKey] = Mock.Of <Type>()
            };
            var httpContextMock = new Mock <HttpContext>();

            httpContextMock.SetupGet(x => x.Response)
            .Returns(responseMock.Object);
            httpContextMock.SetupGet(x => x.Items)
            .Returns(items);
            var expected = new RawServerResponseWrapper(responseMock.Object);

            responseReaderMock.Setup(x => x.GetResponseWrapper(It.IsAny <HttpResponse>(), It.IsAny <Type>(), It.IsAny <IUntypedCall>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(expected);

            var result = await responseReaderMock.Object.GetResponse(httpContextMock.Object, Mock.Of <IUntypedCall>(), true, new CancellationToken());

            result.Should().Be(expected);
            responseReaderMock.Verify(x => x.GetResponseWrapper(It.IsAny <HttpResponse>(), It.IsAny <Type>(), It.IsAny <IUntypedCall>(), It.IsAny <CancellationToken>()));
            responseReaderMock.Verify(x => x.GetResponse(It.IsAny <HttpContext>(), It.IsAny <IUntypedCall>(), It.IsAny <bool>(), It.IsAny <CancellationToken>()));
        }
        public async Task Test_GetResponse_ThrowsOnRawWhenNotAllowed()
        {
            var responseMock = new Mock <HttpResponse>();
            var items        = new Dictionary <object, object> {
                [JsonRpcConstants.ActionResultTypeItemKey] = Mock.Of <Type>()
            };
            var httpContextMock = new Mock <HttpContext>();

            httpContextMock.SetupGet(x => x.Response)
            .Returns(responseMock.Object);
            httpContextMock.SetupGet(x => x.Items)
            .Returns(items);
            var expected = new RawServerResponseWrapper(responseMock.Object);

            responseReaderMock.Setup(x => x.GetResponseWrapper(It.IsAny <HttpResponse>(), It.IsAny <Type>(), It.IsAny <IUntypedCall>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(expected);
            Func <Task> action = async() => await responseReaderMock.Object.GetResponse(httpContextMock.Object, Mock.Of <IUntypedCall>(), false, new CancellationToken());

            await action.Should().ThrowAsync <JsonRpcInternalException>();

            responseReaderMock.Verify(x => x.GetResponseWrapper(It.IsAny <HttpResponse>(), It.IsAny <Type>(), It.IsAny <IUntypedCall>(), It.IsAny <CancellationToken>()));
            responseReaderMock.Verify(x => x.GetResponse(It.IsAny <HttpContext>(), It.IsAny <IUntypedCall>(), It.IsAny <bool>(), It.IsAny <CancellationToken>()));
        }
        public async Task Test_Write_CopiesHeaders()
        {
            var responseHeaders = new HeaderDictionary()
            {
                { "a", "b" }
            };
            var resposneMock = new Mock <HttpResponse>();

            resposneMock.SetupGet(x => x.Headers)
            .Returns(responseHeaders);
            var httpContextMock = new Mock <HttpContext>();

            httpContextMock.SetupGet(x => x.Response)
            .Returns(resposneMock.Object);
            var next            = Mock.Of <RequestDelegate>();
            var handlingContext = new HandlingContext(httpContextMock.Object, Encoding.UTF8, next)
            {
                WriteResponse = false
            };
            var headers = new HeaderDictionary
            {
                { "header", "value" }
            };
            var sourceMock = new Mock <HttpResponse>();

            sourceMock.SetupGet(x => x.StatusCode)
            .Returns(201);
            sourceMock.SetupGet(x => x.Headers)
            .Returns(headers);
            var wrapper = new RawServerResponseWrapper(sourceMock.Object);

            await wrapper.Write(handlingContext, new HeaderJsonRpcSerializer());

            resposneMock.VerifyGet(x => x.Headers);
            responseHeaders.Should().HaveCount(2);
            responseHeaders.Should().ContainKey("header");
        }