diff --git a/src/Mockaco/Extensions/PermissiveDictionaryExtensions.cs b/src/Mockaco/Extensions/PermissiveDictionaryExtensions.cs deleted file mode 100644 index dcb83c9..0000000 --- a/src/Mockaco/Extensions/PermissiveDictionaryExtensions.cs +++ /dev/null @@ -1,30 +0,0 @@ -using Mockaco; -using System; -using System.Collections.Generic; -using System.Linq; - -namespace System.Collections.Generic -{ - public static class PermissiveDictionaryExtensions - { - public static PermissiveDictionary ToPermissiveDictionary( - this IEnumerable source, - Func keySelector, - Func elementSelector) - { - var dictionary = new PermissiveDictionary(); - - if(source == null) - { - return dictionary; - } - - foreach(var item in source) - { - dictionary.Add(keySelector(item), elementSelector(item)); - } - - return dictionary; - } - } -} diff --git a/src/Mockaco/Extensions/StringDictionaryExtensions.cs b/src/Mockaco/Extensions/StringDictionaryExtensions.cs new file mode 100644 index 0000000..34ad1e2 --- /dev/null +++ b/src/Mockaco/Extensions/StringDictionaryExtensions.cs @@ -0,0 +1,27 @@ +using Mockaco; + +namespace System.Collections.Generic +{ + public static class StringDictionaryExtensions + { + public static StringDictionary ToStringDictionary( + this IEnumerable source, + Func keySelector, + Func elementSelector) + { + var dictionary = new StringDictionary(); + + if (source == null) + { + return dictionary; + } + + foreach (var item in source) + { + dictionary.Add(keySelector(item), elementSelector(item)); + } + + return dictionary; + } + } +} diff --git a/src/Mockaco/Scripting/ScriptContext.cs b/src/Mockaco/Scripting/ScriptContext.cs index 2c9d703..201fb8a 100644 --- a/src/Mockaco/Scripting/ScriptContext.cs +++ b/src/Mockaco/Scripting/ScriptContext.cs @@ -1,11 +1,9 @@ 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 @@ -13,9 +11,9 @@ namespace Mockaco public class ScriptContext : IScriptContext { private Uri _uri; - private PermissiveDictionary _queryDictionary = new PermissiveDictionary(); - private PermissiveDictionary _headersDictionary = new PermissiveDictionary(); - private PermissiveDictionary _routeDictionary = new PermissiveDictionary(); + private StringDictionary _queryDictionary = new StringDictionary(); + private StringDictionary _headersDictionary = new StringDictionary(); + private StringDictionary _routeDictionary = new StringDictionary(); private JObject _parsedBody; public Faker Faker { get; } @@ -30,19 +28,19 @@ public ScriptContext() Request = new ScriptContextRequest( default, - new PermissiveDictionary(), - new PermissiveDictionary(), - new PermissiveDictionary(), + new StringDictionary(), + new StringDictionary(), + new StringDictionary(), new JObject()); - Response = new ScriptContextResponse(new PermissiveDictionary(), 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); @@ -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) diff --git a/src/Mockaco/Scripting/ScriptContextResponse.cs b/src/Mockaco/Scripting/ScriptContextResponse.cs index 110462f..da4df50 100644 --- a/src/Mockaco/Scripting/ScriptContextResponse.cs +++ b/src/Mockaco/Scripting/ScriptContextResponse.cs @@ -9,9 +9,7 @@ public class ScriptContextResponse public JToken Body { get; } - public ScriptContextResponse( - PermissiveDictionary header, - JToken body) + public ScriptContextResponse(StringDictionary header, JToken body) { Header = header; Body = body; diff --git a/src/Mockaco/Templating/PermissiveDictionary.cs b/src/Mockaco/Templating/StringDictionary.cs similarity index 60% rename from src/Mockaco/Templating/PermissiveDictionary.cs rename to src/Mockaco/Templating/StringDictionary.cs index 74d2e07..0912f02 100644 --- a/src/Mockaco/Templating/PermissiveDictionary.cs +++ b/src/Mockaco/Templating/StringDictionary.cs @@ -2,19 +2,19 @@ namespace Mockaco { - public class PermissiveDictionary : Dictionary, IReadOnlyDictionary + public class StringDictionary : Dictionary, IReadOnlyDictionary { - 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 @@ -23,12 +23,12 @@ public class PermissiveDictionary : Dictionary, 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)) {