Skip to content

Commit

Permalink
Merge pull request #123 from natenho/feature/simplify-script-string-m…
Browse files Browse the repository at this point in the history
…anipulation

Feature/simplify script string manipulation

+semver: feature
  • Loading branch information
natenho authored Mar 10, 2024
2 parents cd870c5 + bbc799e commit bceec67
Show file tree
Hide file tree
Showing 30 changed files with 98 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Mockaco.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion checkBuild.ps1
Original file line number Diff line number Diff line change
@@ -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 .

Expand Down
6 changes: 2 additions & 4 deletions src/Mockaco.AspNetCore/Common/StringDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ public class StringDictionary : Dictionary<string, string>, IReadOnlyDictionary<
{
return value;
}
else
{
return default;
}

return string.Empty;
}
set
{
Expand Down
2 changes: 1 addition & 1 deletion src/Mockaco.AspNetCore/InternalsVisibleTo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Mockaco.Tests")]
[assembly: InternalsVisibleTo("Mockaco.AspNetCore.Tests")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]
73 changes: 73 additions & 0 deletions test/Mockaco.AspNetCore.Tests/Common/StringDictionaryTest.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>Mockaco.Tests</RootNamespace>
<RootNamespace>Mockaco.AspNetCore.Tests</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.11.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.2" />
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion website/docs/reference/request/condition-attribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
Expand Down
4 changes: 1 addition & 3 deletions website/docs/scripting/index.md
Original file line number Diff line number Diff line change
@@ -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 ```#>```.

Expand All @@ -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
{
Expand Down
20 changes: 14 additions & 6 deletions website/docs/scripting/request-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -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] #>"
}
}
}
```
```

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.

0 comments on commit bceec67

Please sign in to comment.