From 957b19f5fa0284d0c1c399936f509c8a7d0c696d Mon Sep 17 00:00:00 2001 From: Julian Verdurmen <304NotModified@users.noreply.github.com> Date: Sat, 25 Apr 2020 14:30:39 +0200 Subject: [PATCH] Various code improvements (#553) * Improve code complexity * cleanup deadcode * readability --- .../AspNetLayoutMultiValueRendererBase.cs | 80 ++++++++++--------- .../AspNetRequestCookieLayoutRenderer.cs | 7 +- .../TestInvolvingAspNetHttpContext.cs | 30 +------ 3 files changed, 51 insertions(+), 66 deletions(-) diff --git a/src/Shared/LayoutRenderers/AspNetLayoutMultiValueRendererBase.cs b/src/Shared/LayoutRenderers/AspNetLayoutMultiValueRendererBase.cs index 0b903adb..e44f600d 100644 --- a/src/Shared/LayoutRenderers/AspNetLayoutMultiValueRendererBase.cs +++ b/src/Shared/LayoutRenderers/AspNetLayoutMultiValueRendererBase.cs @@ -98,48 +98,56 @@ private void SerializePairsJson(IEnumerable> pairs, var firstItem = true; var pairsList = pairs.ToList(); - if (pairsList.Count > 0) + if (pairsList.Count == 0) { - var addArray = pairsList.Count > (SingleAsArray | ValuesOnly ? 0 : 1); + return; + } - if (addArray) - { - builder.Append('['); - } + var addArray = pairsList.Count > (SingleAsArray || ValuesOnly ? 0 : 1); - foreach (var kpv in pairsList) - { - var key = kpv.Key; - var value = kpv.Value; - if (!firstItem) - { - builder.Append(','); - } - - firstItem = false; - - if (!ValuesOnly) - { - // Quoted key - builder.Append('{'); - AppendQuoted(builder, key); - - builder.Append(':'); - } - - // Quoted value - AppendQuoted(builder, value); - - if (!ValuesOnly) - { - builder.Append('}'); - } - } + if (addArray) + { + builder.Append('['); + } - if (addArray) + foreach (var kpv in pairsList) + { + if (!firstItem) { - builder.Append(']'); + builder.Append(','); } + + SerializePairJson(builder, kpv); + + firstItem = false; + } + + if (addArray) + { + builder.Append(']'); + } + } + + private void SerializePairJson(StringBuilder builder, KeyValuePair kpv) + { + var key = kpv.Key; + var value = kpv.Value; + + if (!ValuesOnly) + { + // Quoted key + builder.Append('{'); + AppendQuoted(builder, key); + + builder.Append(':'); + } + + // Quoted value + AppendQuoted(builder, value); + + if (!ValuesOnly) + { + builder.Append('}'); } } diff --git a/src/Shared/LayoutRenderers/AspNetRequestCookieLayoutRenderer.cs b/src/Shared/LayoutRenderers/AspNetRequestCookieLayoutRenderer.cs index d661da6a..75511b22 100644 --- a/src/Shared/LayoutRenderers/AspNetRequestCookieLayoutRenderer.cs +++ b/src/Shared/LayoutRenderers/AspNetRequestCookieLayoutRenderer.cs @@ -82,7 +82,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) #if !ASP_NET_CORE private IEnumerable> GetCookieValues(HttpCookieCollection cookies, bool checkForExclude) { - var cookieNames = CookieNames?.Count > 0 ? CookieNames : cookies.Keys.Cast().ToList(); + var cookieNames = GetCookieNames(cookies); foreach (var cookieName in cookieNames) { if (checkForExclude && Exclude.Contains(cookieName)) @@ -115,6 +115,11 @@ private IEnumerable> GetCookieValues(HttpCookieColl } } } + + private List GetCookieNames(HttpCookieCollection cookies) + { + return CookieNames?.Count > 0 ? CookieNames : cookies.Keys.Cast().ToList(); + } #else private IEnumerable> GetCookieValues(IRequestCookieCollection cookies, bool checkForExclude) { diff --git a/tests/Shared/LayoutRenderers/TestInvolvingAspNetHttpContext.cs b/tests/Shared/LayoutRenderers/TestInvolvingAspNetHttpContext.cs index 4cd25d35..368b6d68 100644 --- a/tests/Shared/LayoutRenderers/TestInvolvingAspNetHttpContext.cs +++ b/tests/Shared/LayoutRenderers/TestInvolvingAspNetHttpContext.cs @@ -35,7 +35,7 @@ protected TestInvolvingAspNetHttpContext() HttpContext.Current = HttpContext; #endif } - + /// /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// @@ -73,28 +73,6 @@ protected virtual HttpRequest SetUpHttpRequest(Uri uri = null) return new HttpRequest("", uri.AbsoluteUri, uri.Query); } - protected void AddRequestHeader(HttpRequest request, string headerName, string headerValue) - { - // thanks http://stackoverflow.com/a/13307238 - var headers = request.Headers; - var t = headers.GetType(); - var item = new ArrayList(); - - t.InvokeMember("MakeReadWrite", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, - null, - headers, null); - t.InvokeMember("InvalidateCachedArrays", - BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, - null, headers, null); - item.Add(headerValue); - t.InvokeMember("BaseAdd", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, - headers, - new object[] { headerName, item }); - t.InvokeMember("MakeReadOnly", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, - null, - headers, null); - } - protected virtual HttpResponse SetUpHttpResponse() { var stringWriter = new StringWriter(); @@ -112,12 +90,6 @@ protected HttpContext SetUpFakeHttpContext() return context; } - protected void AddRequestHeader(HttpRequest request, string headerName, params string[] headerValues) - { - var headers = request.Headers; - headers.Add(headerName, headerValues); - } - protected virtual HttpRequest SetUpHttpRequest(HttpContext context) { var httpRequest = NSubstitute.Substitute.For();