Skip to content

Commit

Permalink
Various code improvements (#553)
Browse files Browse the repository at this point in the history
* Improve code complexity

* cleanup deadcode

* readability
  • Loading branch information
304NotModified authored Apr 25, 2020
1 parent fbd4044 commit 957b19f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 66 deletions.
80 changes: 44 additions & 36 deletions src/Shared/LayoutRenderers/AspNetLayoutMultiValueRendererBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,48 +98,56 @@ private void SerializePairsJson(IEnumerable<KeyValuePair<string, string>> 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<string, string> 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('}');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
#if !ASP_NET_CORE
private IEnumerable<KeyValuePair<string, string>> GetCookieValues(HttpCookieCollection cookies, bool checkForExclude)
{
var cookieNames = CookieNames?.Count > 0 ? CookieNames : cookies.Keys.Cast<string>().ToList();
var cookieNames = GetCookieNames(cookies);
foreach (var cookieName in cookieNames)
{
if (checkForExclude && Exclude.Contains(cookieName))
Expand Down Expand Up @@ -115,6 +115,11 @@ private IEnumerable<KeyValuePair<string, string>> GetCookieValues(HttpCookieColl
}
}
}

private List<string> GetCookieNames(HttpCookieCollection cookies)
{
return CookieNames?.Count > 0 ? CookieNames : cookies.Keys.Cast<string>().ToList();
}
#else
private IEnumerable<KeyValuePair<string, string>> GetCookieValues(IRequestCookieCollection cookies, bool checkForExclude)
{
Expand Down
30 changes: 1 addition & 29 deletions tests/Shared/LayoutRenderers/TestInvolvingAspNetHttpContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected TestInvolvingAspNetHttpContext()
HttpContext.Current = HttpContext;
#endif
}

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
Expand Down Expand Up @@ -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();
Expand All @@ -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<HttpRequest>();
Expand Down

0 comments on commit 957b19f

Please sign in to comment.