From 31e911ca9a64d262f6db853583441fd22f76a1d3 Mon Sep 17 00:00:00 2001 From: Renato Lima Date: Sun, 10 Mar 2024 01:58:18 -0200 Subject: [PATCH 1/4] Simplify request data access --- src/Mockaco.AspNetCore/Common/StringDictionary.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Mockaco.AspNetCore/Common/StringDictionary.cs b/src/Mockaco.AspNetCore/Common/StringDictionary.cs index 0912f02..7a28004 100644 --- a/src/Mockaco.AspNetCore/Common/StringDictionary.cs +++ b/src/Mockaco.AspNetCore/Common/StringDictionary.cs @@ -12,10 +12,8 @@ public class StringDictionary : Dictionary, IReadOnlyDictionary< { return value; } - else - { - return default; - } + + return string.Empty; } set { From 797b82ab475551bb69ee062045b9860f85f5fb32 Mon Sep 17 00:00:00 2001 From: Renato Lima Date: Sun, 10 Mar 2024 01:59:44 -0200 Subject: [PATCH 2/4] Adjust unit test project name and add ScriptDictionary tests --- src/Mockaco.AspNetCore/InternalsVisibleTo.cs | 2 +- .../Common/StringDictionaryTest.cs | 73 ++++++++++++++++++ .../Middlewares/CallbackMiddlewareTest.cs | 0 .../ErrorHandlingMiddlewareTest.cs | 0 .../RequestMatchingMiddlewareTest.cs | 0 .../ResponseDelayMiddlewareTest.cs | 0 .../ResponseMockingMiddlewareTest.cs | 0 .../Mockaco.AspNetCore.Tests.csproj} | 4 +- .../Request/JsonRequestBodyStrategyTest.cs | 0 .../BinaryResponseBodyStrategyTest.cs | 0 .../DefaultResponseBodyStrategyTest.cs | 0 .../Response/JsonResponseBodyStrategyTest.cs | 0 .../Response/ResponseBodyFactoryTest.cs | 0 .../Response/XmlResponseBodyStrategyTest.cs | 0 .../Templating/Response/mockaco.jpg | Bin .../Scripting/LocalizedFakerFactoryTest.cs | 0 .../Scripting/ScriptRunnerFactoryTest.cs | 0 .../Templating/TemplateTransformerTest.cs | 0 .../Transforms_Plain_Json_Template.json | 0 .../Transforms_Scripted_Json_Template.json | 0 .../TextFileDataAttribute.cs | 0 .../Usings.cs | 0 22 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 test/Mockaco.AspNetCore.Tests/Common/StringDictionaryTest.cs rename test/{Mockaco.Tests => Mockaco.AspNetCore.Tests}/Middlewares/CallbackMiddlewareTest.cs (100%) rename test/{Mockaco.Tests => Mockaco.AspNetCore.Tests}/Middlewares/ErrorHandlingMiddlewareTest.cs (100%) rename test/{Mockaco.Tests => Mockaco.AspNetCore.Tests}/Middlewares/RequestMatchingMiddlewareTest.cs (100%) rename test/{Mockaco.Tests => Mockaco.AspNetCore.Tests}/Middlewares/ResponseDelayMiddlewareTest.cs (100%) rename test/{Mockaco.Tests => Mockaco.AspNetCore.Tests}/Middlewares/ResponseMockingMiddlewareTest.cs (100%) rename test/{Mockaco.Tests/Mockaco.Tests.csproj => Mockaco.AspNetCore.Tests/Mockaco.AspNetCore.Tests.csproj} (96%) rename test/{Mockaco.Tests => Mockaco.AspNetCore.Tests}/Templating/Request/JsonRequestBodyStrategyTest.cs (100%) rename test/{Mockaco.Tests => Mockaco.AspNetCore.Tests}/Templating/Response/BinaryResponseBodyStrategyTest.cs (100%) rename test/{Mockaco.Tests => Mockaco.AspNetCore.Tests}/Templating/Response/DefaultResponseBodyStrategyTest.cs (100%) rename test/{Mockaco.Tests => Mockaco.AspNetCore.Tests}/Templating/Response/JsonResponseBodyStrategyTest.cs (100%) rename test/{Mockaco.Tests => Mockaco.AspNetCore.Tests}/Templating/Response/ResponseBodyFactoryTest.cs (100%) rename test/{Mockaco.Tests => Mockaco.AspNetCore.Tests}/Templating/Response/XmlResponseBodyStrategyTest.cs (100%) rename test/{Mockaco.Tests => Mockaco.AspNetCore.Tests}/Templating/Response/mockaco.jpg (100%) rename test/{Mockaco.Tests => Mockaco.AspNetCore.Tests}/Templating/Scripting/LocalizedFakerFactoryTest.cs (100%) rename test/{Mockaco.Tests => Mockaco.AspNetCore.Tests}/Templating/Scripting/ScriptRunnerFactoryTest.cs (100%) rename test/{Mockaco.Tests => Mockaco.AspNetCore.Tests}/Templating/TemplateTransformerTest.cs (100%) rename test/{Mockaco.Tests => Mockaco.AspNetCore.Tests}/Templating/Transforms_Plain_Json_Template.json (100%) rename test/{Mockaco.Tests => Mockaco.AspNetCore.Tests}/Templating/Transforms_Scripted_Json_Template.json (100%) rename test/{Mockaco.Tests => Mockaco.AspNetCore.Tests}/TextFileDataAttribute.cs (100%) rename test/{Mockaco.Tests => Mockaco.AspNetCore.Tests}/Usings.cs (100%) diff --git a/src/Mockaco.AspNetCore/InternalsVisibleTo.cs b/src/Mockaco.AspNetCore/InternalsVisibleTo.cs index d6d1d59..b392891 100644 --- a/src/Mockaco.AspNetCore/InternalsVisibleTo.cs +++ b/src/Mockaco.AspNetCore/InternalsVisibleTo.cs @@ -1,4 +1,4 @@ using System.Runtime.CompilerServices; -[assembly: InternalsVisibleTo("Mockaco.Tests")] +[assembly: InternalsVisibleTo("Mockaco.AspNetCore.Tests")] [assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")] diff --git a/test/Mockaco.AspNetCore.Tests/Common/StringDictionaryTest.cs b/test/Mockaco.AspNetCore.Tests/Common/StringDictionaryTest.cs new file mode 100644 index 0000000..616b143 --- /dev/null +++ b/test/Mockaco.AspNetCore.Tests/Common/StringDictionaryTest.cs @@ -0,0 +1,73 @@ +using Xunit; +using Mockaco; +using FluentAssertions; + +public class StringDictionaryTests +{ + [Fact] + public void Add_WhenKeyNotExists_AddsKeyValue() + { + var dictionary = new StringDictionary(); + var key = "testKey"; + var value = "testValue"; + dictionary.Add(key, value); + dictionary.ContainsKey(key).Should().BeTrue(); + dictionary[key].Should().Be(value); + } + + [Fact] + public void Add_WhenKeyExists_ReplacesValue() + { + var dictionary = new StringDictionary(); + var key = "testKey"; + var initialValue = "initialValue"; + var newValue = "newValue"; + dictionary.Add(key, initialValue); + dictionary.Add(key, newValue); + dictionary.ContainsKey(key).Should().BeTrue(); + dictionary[key].Should().Be(newValue); + } + + [Fact] + public void Replace_WhenKeyNotExists_AddsKeyValue() + { + var dictionary = new StringDictionary(); + var key = "testKey"; + var value = "testValue"; + dictionary.Replace(key, value); + dictionary.ContainsKey(key).Should().BeTrue(); + dictionary[key].Should().Be(value); + } + + [Fact] + public void Replace_WhenKeyExists_ReplacesValue() + { + var dictionary = new StringDictionary(); + var key = "testKey"; + var initialValue = "initialValue"; + var newValue = "newValue"; + dictionary.Add(key, initialValue); + dictionary.Replace(key, newValue); + dictionary.ContainsKey(key).Should().BeTrue(); + dictionary[key].Should().Be(newValue); + } + + [Fact] + public void Indexer_Get_WhenKeyNotExists_ReturnsEmptyString() + { + var dictionary = new StringDictionary(); + var key = "testKey"; + var value = dictionary[key]; + value.Should().BeEmpty(); + } + + [Fact] + public void Indexer_Set_UpdatesValue() + { + var dictionary = new StringDictionary(); + var key = "testKey"; + var value = "testValue"; + dictionary[key] = value; + dictionary[key].Should().Be(value); + } +} diff --git a/test/Mockaco.Tests/Middlewares/CallbackMiddlewareTest.cs b/test/Mockaco.AspNetCore.Tests/Middlewares/CallbackMiddlewareTest.cs similarity index 100% rename from test/Mockaco.Tests/Middlewares/CallbackMiddlewareTest.cs rename to test/Mockaco.AspNetCore.Tests/Middlewares/CallbackMiddlewareTest.cs diff --git a/test/Mockaco.Tests/Middlewares/ErrorHandlingMiddlewareTest.cs b/test/Mockaco.AspNetCore.Tests/Middlewares/ErrorHandlingMiddlewareTest.cs similarity index 100% rename from test/Mockaco.Tests/Middlewares/ErrorHandlingMiddlewareTest.cs rename to test/Mockaco.AspNetCore.Tests/Middlewares/ErrorHandlingMiddlewareTest.cs diff --git a/test/Mockaco.Tests/Middlewares/RequestMatchingMiddlewareTest.cs b/test/Mockaco.AspNetCore.Tests/Middlewares/RequestMatchingMiddlewareTest.cs similarity index 100% rename from test/Mockaco.Tests/Middlewares/RequestMatchingMiddlewareTest.cs rename to test/Mockaco.AspNetCore.Tests/Middlewares/RequestMatchingMiddlewareTest.cs diff --git a/test/Mockaco.Tests/Middlewares/ResponseDelayMiddlewareTest.cs b/test/Mockaco.AspNetCore.Tests/Middlewares/ResponseDelayMiddlewareTest.cs similarity index 100% rename from test/Mockaco.Tests/Middlewares/ResponseDelayMiddlewareTest.cs rename to test/Mockaco.AspNetCore.Tests/Middlewares/ResponseDelayMiddlewareTest.cs diff --git a/test/Mockaco.Tests/Middlewares/ResponseMockingMiddlewareTest.cs b/test/Mockaco.AspNetCore.Tests/Middlewares/ResponseMockingMiddlewareTest.cs similarity index 100% rename from test/Mockaco.Tests/Middlewares/ResponseMockingMiddlewareTest.cs rename to test/Mockaco.AspNetCore.Tests/Middlewares/ResponseMockingMiddlewareTest.cs diff --git a/test/Mockaco.Tests/Mockaco.Tests.csproj b/test/Mockaco.AspNetCore.Tests/Mockaco.AspNetCore.Tests.csproj similarity index 96% rename from test/Mockaco.Tests/Mockaco.Tests.csproj rename to test/Mockaco.AspNetCore.Tests/Mockaco.AspNetCore.Tests.csproj index 422ea78..2965923 100644 --- a/test/Mockaco.Tests/Mockaco.Tests.csproj +++ b/test/Mockaco.AspNetCore.Tests/Mockaco.AspNetCore.Tests.csproj @@ -2,14 +2,14 @@ net6.0 - Mockaco.Tests + Mockaco.AspNetCore.Tests enable enable false true - + diff --git a/test/Mockaco.Tests/Templating/Request/JsonRequestBodyStrategyTest.cs b/test/Mockaco.AspNetCore.Tests/Templating/Request/JsonRequestBodyStrategyTest.cs similarity index 100% rename from test/Mockaco.Tests/Templating/Request/JsonRequestBodyStrategyTest.cs rename to test/Mockaco.AspNetCore.Tests/Templating/Request/JsonRequestBodyStrategyTest.cs diff --git a/test/Mockaco.Tests/Templating/Response/BinaryResponseBodyStrategyTest.cs b/test/Mockaco.AspNetCore.Tests/Templating/Response/BinaryResponseBodyStrategyTest.cs similarity index 100% rename from test/Mockaco.Tests/Templating/Response/BinaryResponseBodyStrategyTest.cs rename to test/Mockaco.AspNetCore.Tests/Templating/Response/BinaryResponseBodyStrategyTest.cs diff --git a/test/Mockaco.Tests/Templating/Response/DefaultResponseBodyStrategyTest.cs b/test/Mockaco.AspNetCore.Tests/Templating/Response/DefaultResponseBodyStrategyTest.cs similarity index 100% rename from test/Mockaco.Tests/Templating/Response/DefaultResponseBodyStrategyTest.cs rename to test/Mockaco.AspNetCore.Tests/Templating/Response/DefaultResponseBodyStrategyTest.cs diff --git a/test/Mockaco.Tests/Templating/Response/JsonResponseBodyStrategyTest.cs b/test/Mockaco.AspNetCore.Tests/Templating/Response/JsonResponseBodyStrategyTest.cs similarity index 100% rename from test/Mockaco.Tests/Templating/Response/JsonResponseBodyStrategyTest.cs rename to test/Mockaco.AspNetCore.Tests/Templating/Response/JsonResponseBodyStrategyTest.cs diff --git a/test/Mockaco.Tests/Templating/Response/ResponseBodyFactoryTest.cs b/test/Mockaco.AspNetCore.Tests/Templating/Response/ResponseBodyFactoryTest.cs similarity index 100% rename from test/Mockaco.Tests/Templating/Response/ResponseBodyFactoryTest.cs rename to test/Mockaco.AspNetCore.Tests/Templating/Response/ResponseBodyFactoryTest.cs diff --git a/test/Mockaco.Tests/Templating/Response/XmlResponseBodyStrategyTest.cs b/test/Mockaco.AspNetCore.Tests/Templating/Response/XmlResponseBodyStrategyTest.cs similarity index 100% rename from test/Mockaco.Tests/Templating/Response/XmlResponseBodyStrategyTest.cs rename to test/Mockaco.AspNetCore.Tests/Templating/Response/XmlResponseBodyStrategyTest.cs diff --git a/test/Mockaco.Tests/Templating/Response/mockaco.jpg b/test/Mockaco.AspNetCore.Tests/Templating/Response/mockaco.jpg similarity index 100% rename from test/Mockaco.Tests/Templating/Response/mockaco.jpg rename to test/Mockaco.AspNetCore.Tests/Templating/Response/mockaco.jpg diff --git a/test/Mockaco.Tests/Templating/Scripting/LocalizedFakerFactoryTest.cs b/test/Mockaco.AspNetCore.Tests/Templating/Scripting/LocalizedFakerFactoryTest.cs similarity index 100% rename from test/Mockaco.Tests/Templating/Scripting/LocalizedFakerFactoryTest.cs rename to test/Mockaco.AspNetCore.Tests/Templating/Scripting/LocalizedFakerFactoryTest.cs diff --git a/test/Mockaco.Tests/Templating/Scripting/ScriptRunnerFactoryTest.cs b/test/Mockaco.AspNetCore.Tests/Templating/Scripting/ScriptRunnerFactoryTest.cs similarity index 100% rename from test/Mockaco.Tests/Templating/Scripting/ScriptRunnerFactoryTest.cs rename to test/Mockaco.AspNetCore.Tests/Templating/Scripting/ScriptRunnerFactoryTest.cs diff --git a/test/Mockaco.Tests/Templating/TemplateTransformerTest.cs b/test/Mockaco.AspNetCore.Tests/Templating/TemplateTransformerTest.cs similarity index 100% rename from test/Mockaco.Tests/Templating/TemplateTransformerTest.cs rename to test/Mockaco.AspNetCore.Tests/Templating/TemplateTransformerTest.cs diff --git a/test/Mockaco.Tests/Templating/Transforms_Plain_Json_Template.json b/test/Mockaco.AspNetCore.Tests/Templating/Transforms_Plain_Json_Template.json similarity index 100% rename from test/Mockaco.Tests/Templating/Transforms_Plain_Json_Template.json rename to test/Mockaco.AspNetCore.Tests/Templating/Transforms_Plain_Json_Template.json diff --git a/test/Mockaco.Tests/Templating/Transforms_Scripted_Json_Template.json b/test/Mockaco.AspNetCore.Tests/Templating/Transforms_Scripted_Json_Template.json similarity index 100% rename from test/Mockaco.Tests/Templating/Transforms_Scripted_Json_Template.json rename to test/Mockaco.AspNetCore.Tests/Templating/Transforms_Scripted_Json_Template.json diff --git a/test/Mockaco.Tests/TextFileDataAttribute.cs b/test/Mockaco.AspNetCore.Tests/TextFileDataAttribute.cs similarity index 100% rename from test/Mockaco.Tests/TextFileDataAttribute.cs rename to test/Mockaco.AspNetCore.Tests/TextFileDataAttribute.cs diff --git a/test/Mockaco.Tests/Usings.cs b/test/Mockaco.AspNetCore.Tests/Usings.cs similarity index 100% rename from test/Mockaco.Tests/Usings.cs rename to test/Mockaco.AspNetCore.Tests/Usings.cs From 87b38ee5d4b35db6f498ecc8e47a8807a22bd516 Mon Sep 17 00:00:00 2001 From: Renato Lima Date: Sun, 10 Mar 2024 02:00:15 -0200 Subject: [PATCH 3/4] Update docs with the request access simplification --- .../reference/request/condition-attribute.md | 2 +- website/docs/scripting/index.md | 4 +--- website/docs/scripting/request-data.md | 20 +++++++++++++------ 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/website/docs/reference/request/condition-attribute.md b/website/docs/reference/request/condition-attribute.md index c175570..e9d4855 100644 --- a/website/docs/reference/request/condition-attribute.md +++ b/website/docs/reference/request/condition-attribute.md @@ -30,7 +30,7 @@ The condition can also be used to match based on query parameters: "request": { "method": "GET", "route": "any/{myVar}", - "condition": "<#= Request.Query["foo"]?.ToString() == "bar" #>" + "condition": "<#= Request.Query["foo"] == "bar" #>" }, "response": { "body": "Hello!" diff --git a/website/docs/scripting/index.md b/website/docs/scripting/index.md index 531708b..3e056bd 100644 --- a/website/docs/scripting/index.md +++ b/website/docs/scripting/index.md @@ -1,6 +1,6 @@ # Scripting -Every part of the mock file is scriptable, so you can add code to programatically generate parts of the template. +Every part of the mock file is scriptable, so you can add code to programmatically generate parts of the template. Use C# code surrounded by ```<#=``` and ```#>```. @@ -11,14 +11,12 @@ Although it pays off, in the other hand, the invalid JSON file may be a little h ::: - The mock code and generation will run for each request. It's possible to access request data within the response template. In the same way, response data can be used within the callback request template. The scripts are compiled and executed via [Roslyn](https://github.com/dotnet/roslyn/wiki/Scripting-API-Samples). - ### Example ```json { diff --git a/website/docs/scripting/request-data.md b/website/docs/scripting/request-data.md index e2256e6..699779c 100644 --- a/website/docs/scripting/request-data.md +++ b/website/docs/scripting/request-data.md @@ -15,12 +15,20 @@ There is a ```Request``` object available to access request data. "response": { "status": "OK", "body": { - "url": "<#= Request.Url?.ToString() #>", - "customerId": "<#= Request.Route["id"]?.ToString() #>", - "acceptHeader": "<#= Request.Header["Content-Type"]?.ToString() #>", - "queryString": "<#= Request.Query["dummy"]?.ToString() #>", - "requestBodyAttribute": "<#= Request.Body["address"]?[0]?.ToString() #>" + "url": "<#= Request.Url #>", + "customerId": "<#= Request.Route["id"] #>", + "acceptHeader": "<#= Request.Header["Content-Type"] #>", + "queryString": "<#= Request.Query["dummy"] #>", + "requestBodyAttribute": "<#= Request.Body["address"]?[0] #>" } } } -``` \ No newline at end of file +``` + +The Request object has the following properties: + +- `Url`: An instance of [`Uri`](https://learn.microsoft.com/dotnet/api/system.uri) class containing request URL data. +- `Route`: A string dictionary containing route parameters. +- `Header`: A string dictionary containing request headers. +- `Query`: A string dictionary containing query parameters. +- `Body`: A [JToken](https://www.newtonsoft.com/json/help/html/t_newtonsoft_json_linq_jtoken.htm) object containing request body data. \ No newline at end of file From bbc799ed1db79b7a33925fc41a4c6ab70b23d0bd Mon Sep 17 00:00:00 2001 From: Renato Lima Date: Sun, 10 Mar 2024 02:07:20 -0200 Subject: [PATCH 4/4] Fix tests path --- .github/workflows/main-release.yml | 2 +- Mockaco.sln | 2 +- appveyor.yml | 2 +- checkBuild.ps1 | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/main-release.yml b/.github/workflows/main-release.yml index 96a5848..5373ec7 100644 --- a/.github/workflows/main-release.yml +++ b/.github/workflows/main-release.yml @@ -37,7 +37,7 @@ jobs: - name: Build run: dotnet build --configuration Release --verbosity normal src/Mockaco/Mockaco.csproj - name: Test - run: dotnet test --configuration Release --verbosity normal test/Mockaco.Tests/Mockaco.Tests.csproj + run: dotnet test --configuration Release --verbosity normal test/Mockaco.AspNetCore.Tests/Mockaco.AspNetCore.Tests.csproj - name: Bump version and push tag if: "github.ref == 'refs/heads/master' && !contains(github.event.head_commit.message, 'skip-release')" id: tag_version diff --git a/Mockaco.sln b/Mockaco.sln index 9298c93..932cbd4 100644 --- a/Mockaco.sln +++ b/Mockaco.sln @@ -10,7 +10,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution README.md = README.md EndProjectSection EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mockaco.Tests", "test\Mockaco.Tests\Mockaco.Tests.csproj", "{EE57B1B4-29D2-4AE3-8F23-5E622302C30F}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mockaco.Tests", "test\Mockaco.AspNetCore.Tests\Mockaco.AspNetCore.Tests.csproj", "{EE57B1B4-29D2-4AE3-8F23-5E622302C30F}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mockaco.AspNetCore", "src\Mockaco.AspNetCore\Mockaco.AspNetCore.csproj", "{7766C592-9887-4162-8B9C-51003ED30335}" EndProject diff --git a/appveyor.yml b/appveyor.yml index f8ab836..a155932 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -18,7 +18,7 @@ build_script: dotnet restore ./src/Mockaco/Mockaco.csproj --verbosity m dotnet publish ./src/Mockaco/Mockaco.csproj test_script: -- cmd: dotnet test .\test\Mockaco.Tests\Mockaco.Tests.csproj +- cmd: dotnet test .\test\Mockaco.AspNetCore.Tests\Mockaco.AspNetCore.Tests.csproj artifacts: - path: src\Mockaco\bin\Release\net5.0\publish\ name: Mockaco Web Site diff --git a/checkBuild.ps1 b/checkBuild.ps1 index 0f412c2..5098809 100644 --- a/checkBuild.ps1 +++ b/checkBuild.ps1 @@ -1,6 +1,6 @@ dotnet restore --verbosity normal dotnet build --configuration Release --verbosity normal .\src\Mockaco\Mockaco.csproj -dotnet test --configuration Release --verbosity normal .\test\Mockaco.Tests\Mockaco.Tests.csproj +dotnet test --configuration Release --verbosity normal .\test\Mockaco.AspNetCore.Tests\Mockaco.AspNetCore.Tests.csproj dotnet pack --configuration Nuget --output ./nupkg docker build -f ./src/Mockaco/Docker/Dockerfile -t mockaco:local .