Skip to content

Commit

Permalink
Simplify permissive dictionary to a specialized string dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
natenho committed Aug 1, 2019
1 parent db3373f commit 0bd21ca
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 52 deletions.
30 changes: 0 additions & 30 deletions src/Mockaco/Extensions/PermissiveDictionaryExtensions.cs

This file was deleted.

27 changes: 27 additions & 0 deletions src/Mockaco/Extensions/StringDictionaryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Mockaco;

namespace System.Collections.Generic
{
public static class StringDictionaryExtensions
{
public static StringDictionary ToStringDictionary<TSource>(
this IEnumerable<TSource> source,
Func<TSource, string> keySelector,
Func<TSource, string> elementSelector)
{
var dictionary = new StringDictionary();

if (source == null)
{
return dictionary;
}

foreach (var item in source)
{
dictionary.Add(keySelector(item), elementSelector(item));
}

return dictionary;
}
}
}
24 changes: 11 additions & 13 deletions src/Mockaco/Scripting/ScriptContext.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
using Bogus;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Internal;
using Mockaco.Routing;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Mockaco
{
public class ScriptContext : IScriptContext
{
private Uri _uri;
private PermissiveDictionary<string, string> _queryDictionary = new PermissiveDictionary<string, string>();
private PermissiveDictionary<string, string> _headersDictionary = new PermissiveDictionary<string, string>();
private PermissiveDictionary<string, string> _routeDictionary = new PermissiveDictionary<string, string>();
private StringDictionary _queryDictionary = new StringDictionary();
private StringDictionary _headersDictionary = new StringDictionary();
private StringDictionary _routeDictionary = new StringDictionary();
private JObject _parsedBody;

public Faker Faker { get; }
Expand All @@ -30,19 +28,19 @@ public ScriptContext()

Request = new ScriptContextRequest(
default,
new PermissiveDictionary<string, string>(),
new PermissiveDictionary<string, string>(),
new PermissiveDictionary<string, string>(),
new StringDictionary(),
new StringDictionary(),
new StringDictionary(),
new JObject());

Response = new ScriptContextResponse(new PermissiveDictionary<string, string>(), new JObject());
Response = new ScriptContextResponse(new StringDictionary(), new JObject());
}

public void AttachHttpContext(HttpContext httpContext)
{
_uri = httpContext.Request.GetUri();
_queryDictionary = httpContext.Request.Query.ToPermissiveDictionary(k => k.Key, v => v.Value.ToString());
_headersDictionary = httpContext.Request.Headers.ToPermissiveDictionary(k => k.Key, v => v.Value.ToString());
_queryDictionary = httpContext.Request.Query.ToStringDictionary(k => k.Key, v => v.Value.ToString());
_headersDictionary = httpContext.Request.Headers.ToStringDictionary(k => k.Key, v => v.Value.ToString());
_parsedBody = ParseBody(httpContext.Request);

Request = new ScriptContextRequest(url:_uri, route:_routeDictionary, query:_queryDictionary, header:_headersDictionary, body:_parsedBody);
Expand All @@ -51,14 +49,14 @@ public void AttachHttpContext(HttpContext httpContext)
public void AttachRoute(HttpContext httpContext, Route route)
{
_routeDictionary = httpContext.Request.GetRouteData(route)
.ToPermissiveDictionary(k => k.Key, v => v.Value.ToString());
.ToStringDictionary(k => k.Key, v => v.Value.ToString());

Request = new ScriptContextRequest(url:_uri, route:_routeDictionary, query:_queryDictionary, header:_headersDictionary, body:_parsedBody);
}

public void AttachResponse(IHeaderDictionary headers, JToken body)
{
Response = new ScriptContextResponse(headers.ToPermissiveDictionary(k => k.Key, v => v.Value.ToString()), body);
Response = new ScriptContextResponse(headers.ToStringDictionary(k => k.Key, v => v.Value.ToString()), body);
}

private static JObject ParseBody(HttpRequest httpRequest)
Expand Down
4 changes: 1 addition & 3 deletions src/Mockaco/Scripting/ScriptContextResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ public class ScriptContextResponse

public JToken Body { get; }

public ScriptContextResponse(
PermissiveDictionary<string, string> header,
JToken body)
public ScriptContextResponse(StringDictionary header, JToken body)
{
Header = header;
Body = body;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

namespace Mockaco
{
public class PermissiveDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IReadOnlyDictionary<TKey, TValue>
public class StringDictionary : Dictionary<string, string>, IReadOnlyDictionary<string, string>
{
public new TValue this[TKey key]
public new string this[string key]
{
get
{
if (TryGetValue(key, out TValue value))
if (TryGetValue(key, out string value))
{
return value;
}
else
{
return default(TValue);
return default;
}
}
set
Expand All @@ -23,12 +23,12 @@ public class PermissiveDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IRea
}
}

public new void Add(TKey key, TValue value)
public new void Add(string key, string value)
{
Replace(key, value);
}

public void Replace(TKey key, TValue value)
public void Replace(string key, string value)
{
if (ContainsKey(key))
{
Expand Down

0 comments on commit 0bd21ca

Please sign in to comment.