示例#1
0
        public void Test_HttpContextExtensions_GetClaim()
        {
            // Arrange
            var fakeRequest   = FakeHttpContext.GetResponseHttpContext(Encoding.UTF8.GetBytes("test"));
            var actionContext = new ActionContext(fakeRequest, new RouteData(), new ActionDescriptor());

            // Act/Assert
            actionContext.HttpContext.Response.HttpContext.GetClaimValue <string>("testKey").Should().Be("testVal");
            actionContext.HttpContext.Response.HttpContext.GetClaimValue <string>("doesnotexist").Should().Be(null);
        }
示例#2
0
        public void Test_HttpExtensions_ResponseToFormattedString()
        {
            // Arrange
            var fake = FakeHttpContext.GetResponseHttpContext(Encoding.UTF8.GetBytes("test"));

            // Act
            var str = fake.Response.ToFormattedString();

            // Assert
            str.IndexOf("Headers: testKey:testVal", StringComparison.InvariantCulture).Should().BeGreaterThan(0);
            str.IndexOf("Authenticated: False", StringComparison.InvariantCulture).Should().BeGreaterThan(0);
        }
示例#3
0
        public void Test_HttpContextExtensions_GetConnectionInfo()
        {
            // Arrange
            var fakeRequest    = FakeHttpContext.GetResponseHttpContext(Encoding.UTF8.GetBytes("test"));
            var actionContext  = new ActionContext(fakeRequest, new RouteData(), new ActionDescriptor());
            var defaultAddress = IPAddress.Parse("127.0.0.1").ToString();

            // Act
            var clientIp = actionContext.HttpContext.Response.HttpContext.GetClientIpAddress();

            // Assert
            clientIp.Should().Be(defaultAddress);
        }
        public void Test_CsvOutputFormatter_CanWrite()
        {
            // Arrange
            var options = new CsvFormatterOptions {
                CsvDelimiter = ",", UseSingleLineHeaderInCsv = true
            };
            var instance = new CsvOutputFormatter(options);

            // Expected output after conversion.
            var content = $"HeaderOne,HeaderTwo,HeaderThree{Environment.NewLine}1,2,3{Environment.NewLine}4,5,6";

            // Mock contexts for using with the output formatter (along with object that will be converted to Csv).
            var mockHttpContext = FakeHttpContext.GetResponseHttpContext(Encoding.ASCII.GetBytes(content), "text/cv");
            var mockContext     = new FakeOutputFormatterContext(mockHttpContext, (s, e) => new StreamWriter(new MemoryStream()),
                                                                 typeof(List <TestCsvParsed>),
                                                                 new List <TestCsvParsed>()
            {
                new TestCsvParsed {
                    HeaderOne = "1", HeaderTwo = "2", HeaderThree = "3"
                },
                new TestCsvParsed {
                    HeaderOne = "4", HeaderTwo = "5", HeaderThree = "6"
                }
            });

            // Act
            instance.WriteResponseBodyAsync(mockContext).GetAwaiter().GetResult();

            // Assert
            instance.ContentType.Should().Be("text/csv");
            instance.CanWriteResult(mockContext).Should().BeTrue();
            instance.SupportedMediaTypes.Should().BeEquivalentTo(new MediaTypeCollection()
            {
                "text/csv"
            });

            Assert.Throws <ArgumentNullException>(() => instance.CanWriteTypeInternal(null));

            StreamReader reader      = new StreamReader(mockContext.HttpContext.Response.Body);
            string       strResponse = reader.ReadToEnd();

            strResponse.Should().Be(content);
        }