public void CommaSeperatedCookieNamesTest_Multiple_Cookie_Values_Json_Formatting() { var expectedResult = "[{\"key\":\"TEST\"},{\"Key1\":\"TEST1\"}]"; var cookie = new HttpCookie("key", "TEST") { ["Key1"] = "TEST1" }; var layoutRender = new AspNetRequestCookieLayoutRenderer() { CookieNames = new List <string> { "key", "key1" }, OutputFormat = AspNetRequestLayoutOutputFormat.Json }; var httpContextAccessorMock = CreateHttpContextAccessorMockWithCookie(cookie); layoutRender.HttpContextAccessor = httpContextAccessorMock; var result = layoutRender.Render(LogEventInfo.CreateNullEvent()); Assert.Equal(expectedResult, result); }
public void CommaSeperatedCookieNamesTest_Multiple_Cookie_Values_Flat_Formatting() { // Arrange var expectedResult = "key=TEST&Key1=TEST1"; var cookie = new HttpCookie("key", "TEST") { ["Key1"] = "TEST1" }; var layoutRender = new AspNetRequestCookieLayoutRenderer() { CookieNames = new List <string> { "key", "key1" } }; var httpContextAccessorMock = CreateHttpContextAccessorMockWithCookie(cookie); layoutRender.HttpContextAccessor = httpContextAccessorMock; // Act var result = layoutRender.Render(LogEventInfo.CreateNullEvent()); // Assert Assert.Equal(expectedResult, result); }
public void KeyFoundRendersVakue_Cookie_Mulitple_Cookies_Cookie_Items_Json_Formatting() { var expectedResult = "{\"key=TEST&Key1=TEST1\"},{\"key2=Test&key3=Test456\"}"; var httpContext = Substitute.For <HttpContextBase>(); var cookie = new HttpCookie("key", "TEST"); cookie["Key1"] = "TEST1"; var cookies = new HttpCookieCollection(); cookies.Add(cookie); cookie = new HttpCookie("key2", "Test"); cookie["key3"] = "Test456"; cookies.Add(cookie); httpContext.Request.Cookies.Returns(cookies); var renderer = new AspNetRequestCookieLayoutRenderer(); renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext); renderer.CookieNames = new List <string> { "key", "key2" }; renderer.OutputFormat = AspNetRequestLayoutOutputFormat.Json; string result = renderer.Render(new LogEventInfo()); Assert.Equal(expectedResult, result); }
public void NullKeyRendersEmptyString() { var httpContext = Substitute.For <HttpContextBase>(); var renderer = new AspNetRequestCookieLayoutRenderer(); renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext); renderer.CookieNames = null; string result = renderer.Render(new LogEventInfo()); Assert.Empty(result); }
public void KeyNotFoundRendersEmptyString_Json_Formatting() { var httpContext = Substitute.For <HttpContextBase>(); httpContext.Request.Cookies.Returns(new HttpCookieCollection { new HttpCookie("key1", "TEST") }); var renderer = new AspNetRequestCookieLayoutRenderer(); renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext); renderer.CookieNames = new List <string> { "key" }; renderer.OutputFormat = AspNetRequestLayoutOutputFormat.Json; string result = renderer.Render(new LogEventInfo()); Assert.Empty(result); }
public void KeyFoundRendersValue_Single_Item_Flat_Formatting() { var expectedResult = "key=TEST"; var httpContext = Substitute.For <HttpContextBase>(); var cookie = new HttpCookie("key", "TEST"); var cookies = new HttpCookieCollection(); cookies.Add(cookie); httpContext.Request.Cookies.Returns(cookies); var renderer = new AspNetRequestCookieLayoutRenderer(); renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext); renderer.CookieNames = new List <string> { "key" }; string result = renderer.Render(new LogEventInfo()); Assert.Equal(expectedResult, result); }
/// <summary> /// Create cookie renderer with mockup http context /// </summary> /// <param name="addKey">add key1 to cookie 1</param> /// <param name="addCookie2">add 2nd cookie</param> /// <returns></returns> private static AspNetRequestCookieLayoutRenderer CreateRenderer(bool addKey = true, bool addCookie2 = false) { var httpContext = Substitute.For <HttpContextBase>(); var cookie1 = new HttpCookie("key", "TEST"); if (addKey) { cookie1["Key1"] = "TEST1"; } var cookies = new HttpCookieCollection { cookie1 }; var cookieNames = new List <string> { "key" }; if (addCookie2) { var cookie2 = new HttpCookie("key2", "Test"); cookie2["key3"] = "Test456"; cookies.Add(cookie2); cookieNames.Add("key2"); } httpContext.Request.Cookies.Returns(cookies); var renderer = new AspNetRequestCookieLayoutRenderer(); renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext); renderer.CookieNames = cookieNames; return(renderer); }
/// <summary> /// Create cookie renderer with mocked HTTP context /// </summary> /// <param name="addSecondCookie">Add second cookie</param> /// <param name="addMultiValueCookieKey">Make cookie multi-value by adding a second value to it</param> /// <returns>Created cookie layout renderer</returns> /// <remarks> /// The parameter <paramref name="addMultiValueCookieKey"/> allows creation of multi-valued cookies with the same key, /// as provided in the HttpCookie API for backwards compatibility with classic ASP. /// This is not supported in ASP.NET Core. For further details, see: /// https://docs.microsoft.com/en-us/dotnet/api/system.web.httpcookie.item?view=netframework-4.7.1#System_Web_HttpCookie_Item_System_String_ /// https://stackoverflow.com/a/43831482/6651 /// https://github.com/aspnet/HttpAbstractions/issues/831 /// </remarks> private AspNetRequestCookieLayoutRenderer CreateRenderer(bool addSecondCookie = true, bool addMultiValueCookieKey = false) { var cookieNames = new List <string>(); #if ASP_NET_CORE var httpContext = HttpContext; #else var httpContext = Substitute.For <HttpContextBase>(); #endif #if ASP_NET_CORE void AddCookie(string key, string result) { cookieNames.Add(key); var newCookieValues = new [] { $"{key}={result}" }; if (!httpContext.Request.Headers.TryGetValue("Cookie", out var cookieHeaderValues)) { cookieHeaderValues = new StringValues(newCookieValues); } else { cookieHeaderValues = new StringValues(cookieHeaderValues.ToArray().Union(newCookieValues).ToArray()); } httpContext.Request.Headers["Cookie"] = cookieHeaderValues; } AddCookie("key", "TEST"); if (addSecondCookie) { AddCookie("Key1", "TEST1"); } if (addMultiValueCookieKey) { throw new NotSupportedException("Multi-valued cookie keys are not supported in ASP.NET Core"); } #else var cookie1 = new HttpCookie("key", "TEST"); var cookies = new HttpCookieCollection { cookie1 }; cookieNames.Add("key"); if (addSecondCookie) { var cookie2 = new HttpCookie("Key1", "TEST1"); cookies.Add(cookie2); cookieNames.Add("Key1"); } if (addMultiValueCookieKey) { var multiValueCookie = new HttpCookie("key2", "Test"); multiValueCookie["key3"] = "Test456"; cookies.Add(multiValueCookie); cookieNames.Add("key2"); } httpContext.Request.Cookies.Returns(cookies); #endif var renderer = new AspNetRequestCookieLayoutRenderer(); renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext); renderer.CookieNames = cookieNames; return(renderer); }
/// <summary> /// Create cookie renderer with mockup http context /// </summary> /// <param name="addKey">add key1 to cookie 1</param> /// <param name="addCookie2">add 2nd cookie</param> /// <returns></returns> private static AspNetRequestCookieLayoutRenderer CreateRenderer(bool addKey = true, bool addCookie2 = false) { var cookieNames = new List <string>(); var httpContext = Substitute.For <HttpContextBase>(); #if NETSTANDARD_1plus IRequestCookieCollection cookies = Substitute.For <IRequestCookieCollection>(); var cookieDict = new Dictionary <string, string>(); void AddCookie(string key, string result) { cookieNames.Add(key); cookies[key].Returns(result); cookieDict.Add(key, result); } AddCookie("key", "TEST"); if (addKey) { AddCookie("Key1", "TEST1"); } if (addCookie2) { AddCookie("key2", "Test"); AddCookie("key3", "Test456"); } cookies.Count.Returns(cookieDict.Count); cookies.TryGetValue("", out var _) .ReturnsForAnyArgs(callInfo => { var name = callInfo.Args().First()?.ToString(); var returnVal = cookieDict.TryGetValue(name, out var cookie); callInfo[1] = cookie; return(returnVal); }); #else var cookie1 = new HttpCookie("key", "TEST"); cookieNames.Add("key"); if (addKey) { cookie1["Key1"] = "TEST1"; } var cookies = new HttpCookieCollection { cookie1 }; if (addCookie2) { var cookie2 = new HttpCookie("key2", "Test"); cookie2["key3"] = "Test456"; cookies.Add(cookie2); cookieNames.Add("key2"); } #endif httpContext.Request.Cookies.Returns(cookies); var renderer = new AspNetRequestCookieLayoutRenderer(); renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext); renderer.CookieNames = cookieNames; return(renderer); }