Skip to content

Commit

Permalink
chore(deps):pact-net 5/dotnet8
Browse files Browse the repository at this point in the history
  • Loading branch information
YOU54F committed Oct 8, 2024
1 parent 6c60c91 commit ffeeb5b
Show file tree
Hide file tree
Showing 15 changed files with 335 additions and 229 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ jobs:
- macos-latest
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Setup .NET Core SDK
uses: actions/setup-dotnet@v3
uses: actions/setup-dotnet@v4
with:
dotnet-version: "6.0.x" # runners already have .Net Framework installed as well
dotnet-version: "8.0.x" # runners already have .Net Framework installed as well

- name: Completed Solution - Consumer Restore
run: dotnet restore
Expand All @@ -47,7 +47,7 @@ jobs:
run: dotnet restore
working-directory: CompletedSolution/Provider/src
continue-on-error: true
- name: Completed Solution - Run Pact Consumer Tests
- name: Completed Solution - Run Pact Provider Tests
run: |
cd Provider/src && dotnet run & pid=$! && sleep 10
cd Provider/tests && dotnet test
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vscode/
bin/
obj/
obj/
pacts/
2 changes: 1 addition & 1 deletion CompletedSolution/Consumer/src/consumer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

</Project>
13 changes: 8 additions & 5 deletions CompletedSolution/Consumer/tests/tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>
Expand All @@ -11,11 +11,14 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="PactNet" Version="4.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="PactNet" Version="5.0.0" />
<PackageReference Include="PactNet.Output.Xunit" Version="1.0.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3"/>
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion CompletedSolution/Provider/src/provider.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
13 changes: 8 additions & 5 deletions CompletedSolution/Provider/tests/Middleware/ProviderState.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System.Collections.Generic;

namespace tests.Middleware
{
public class ProviderState
{
public string Consumer { get; set; }
public string State { get; set; }
}
/// <summary>
/// Provider state DTO
/// </summary>
/// <param name="State">State description</param>
/// <param name="Params">State parameters</param>
public record ProviderState(string State, IDictionary<string, object> Params);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,87 +6,101 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http;
using Newtonsoft.Json;
using System.Text.Json;

namespace tests.Middleware
{
public class ProviderStateMiddleware
{
private const string ConsumerName = "Consumer";

private static readonly JsonSerializerOptions Options = new()
{
PropertyNameCaseInsensitive = true
};

private readonly RequestDelegate _next;
private readonly IDictionary<string, Action> _providerStates;

private readonly IDictionary<string, Func<IDictionary<string, object>, Task>> _providerStates;

public ProviderStateMiddleware(RequestDelegate next)
{
_next = next;
_providerStates = new Dictionary<string, Action>
_providerStates = new Dictionary<string, Func<IDictionary<string, object>, Task>>
{
{
"There is no data",
RemoveAllData
},
{
"There is data",
AddData
}

["There is no data"] = RemoveAllData,
["There is data"] = AddData
};
}

private void RemoveAllData()
private async Task RemoveAllData(IDictionary<string, object> parameters)
{
string path = Path.Combine(Directory.GetCurrentDirectory(), @"../../../../../data");
var deletePath = Path.Combine(path, "somedata.txt");

if (File.Exists(deletePath))
{
File.Delete(deletePath);
await Task.Run(() => File.Delete(deletePath));
}
}

private void AddData()
private async Task AddData(IDictionary<string, object> parameters)
{
string path = Path.Combine(Directory.GetCurrentDirectory(), @"../../../../../data");
var writePath = Path.Combine(path, "somedata.txt");

if (!File.Exists(writePath))
{
File.Create(writePath);
using (var fileStream = new FileStream(writePath, FileMode.CreateNew))
{
await fileStream.FlushAsync();
}
}
}

public async Task Invoke(HttpContext context)
/// <summary>
/// Handle the request
/// </summary>
/// <param name="context">Request context</param>
/// <returns>Awaitable</returns>
public async Task InvokeAsync(HttpContext context)
{
if (context.Request.Path.StartsWithSegments("/provider-states"))
{
await this.HandleProviderStatesRequest(context);
await context.Response.WriteAsync(String.Empty);
}
else

if (!(context.Request.Path.Value?.StartsWith("/provider-states") ?? false))
{
await this._next(context);
await this._next.Invoke(context);
return;
}
}

context.Response.StatusCode = StatusCodes.Status200OK;

private async Task HandleProviderStatesRequest(HttpContext context)
{
context.Response.StatusCode = (int)HttpStatusCode.OK;

if (context.Request.Method.ToUpper() == HttpMethod.Post.ToString().ToUpper() &&
context.Request.Body != null)
if (context.Request.Method == HttpMethod.Post.ToString().ToUpper())
{
string jsonRequestBody = String.Empty;
string jsonRequestBody;

using (var reader = new StreamReader(context.Request.Body, Encoding.UTF8))
{
jsonRequestBody = await reader.ReadToEndAsync();
}

var providerState = JsonConvert.DeserializeObject<ProviderState>(jsonRequestBody);
try
{

ProviderState providerState = JsonSerializer.Deserialize<ProviderState>(jsonRequestBody, Options);

//A null or empty provider state key must be handled
if (providerState != null && !String.IsNullOrEmpty(providerState.State))
if (!string.IsNullOrEmpty(providerState?.State))
{
await this._providerStates[providerState.State].Invoke(providerState.Params);
}
}
catch (Exception e)
{
_providerStates[providerState.State].Invoke();
context.Response.StatusCode = StatusCodes.Status500InternalServerError;
await context.Response.WriteAsync("Failed to deserialise JSON provider state body:");
await context.Response.WriteAsync(jsonRequestBody);
await context.Response.WriteAsync(string.Empty);
await context.Response.WriteAsync(e.ToString());
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions CompletedSolution/Provider/tests/ProviderApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public void EnsureProviderApiHonoursPactWithConsumer()
};

//Act / Assert
IPactVerifier pactVerifier = new PactVerifier(config);
IPactVerifier pactVerifier = new PactVerifier("Provider", config);
var pactFile = new FileInfo(Path.Join("..", "..", "..", "..", "..", "pacts", "Consumer-Provider.json"));
pactVerifier.ServiceProvider("Provider", new Uri(_providerUri))
pactVerifier.WithHttpEndpoint(new Uri(_providerUri))
.WithFileSource(pactFile)
.WithProviderStateUrl(new Uri($"{_pactServiceUri}/provider-states"))
.Verify();
Expand Down
13 changes: 8 additions & 5 deletions CompletedSolution/Provider/tests/tests.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="PactNet" Version="4.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="PactNet" Version="5.0.0" />
<PackageReference Include="PactNet.Output.Xunit" Version="1.0.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3"/>
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
</Project>
Empty file.
2 changes: 1 addition & 1 deletion YourSolution/Consumer/src/consumer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

</Project>
6 changes: 1 addition & 5 deletions YourSolution/Provider/src/provider.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>

<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.1" />
</ItemGroup>

</Project>
Empty file removed YourSolution/data/somedata.txt
Empty file.
79 changes: 79 additions & 0 deletions pact-workshop-dotnet-core-v1.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.002.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "YourSolution", "YourSolution", "{81A71712-6486-4CCD-95D1-0EA508ECA0E4}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Provider", "Provider", "{CBFEEEC0-56DA-4CE2-961E-38662AEC4B55}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "provider", "YourSolution\Provider\src\provider.csproj", "{EE9477E9-AF89-4894-9FE2-A6E98B1E5410}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Consumer", "Consumer", "{B1A0A2D4-09E9-4065-9AF6-A35C677DDEBD}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "consumer", "YourSolution\Consumer\src\consumer.csproj", "{37463E67-0860-486E-BE33-A2E7165985A8}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "CompletedSolution", "CompletedSolution", "{BD799AAB-0C70-412B-896E-F67E4FD5B55A}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Provider", "Provider", "{D652BD7B-69A6-4DDE-850A-E869B7FACE3C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "tests", "CompletedSolution\Provider\tests\tests.csproj", "{4E9B9194-6856-4D40-88B1-9613FF9083F1}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "provider", "CompletedSolution\Provider\src\provider.csproj", "{1F1AD58E-6D74-46B1-BACA-1FF2DF72147A}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Consumer", "Consumer", "{42463238-2FED-4919-9428-C73EC43DDAEF}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "tests", "CompletedSolution\Consumer\tests\tests.csproj", "{B8A7696E-C2F0-4AB9-9462-22A164671349}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "consumer", "CompletedSolution\Consumer\src\consumer.csproj", "{CF1EBE16-9871-40C2-A668-145671963E25}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{EE9477E9-AF89-4894-9FE2-A6E98B1E5410}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EE9477E9-AF89-4894-9FE2-A6E98B1E5410}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EE9477E9-AF89-4894-9FE2-A6E98B1E5410}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EE9477E9-AF89-4894-9FE2-A6E98B1E5410}.Release|Any CPU.Build.0 = Release|Any CPU
{37463E67-0860-486E-BE33-A2E7165985A8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{37463E67-0860-486E-BE33-A2E7165985A8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{37463E67-0860-486E-BE33-A2E7165985A8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{37463E67-0860-486E-BE33-A2E7165985A8}.Release|Any CPU.Build.0 = Release|Any CPU
{4E9B9194-6856-4D40-88B1-9613FF9083F1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4E9B9194-6856-4D40-88B1-9613FF9083F1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4E9B9194-6856-4D40-88B1-9613FF9083F1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4E9B9194-6856-4D40-88B1-9613FF9083F1}.Release|Any CPU.Build.0 = Release|Any CPU
{1F1AD58E-6D74-46B1-BACA-1FF2DF72147A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1F1AD58E-6D74-46B1-BACA-1FF2DF72147A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1F1AD58E-6D74-46B1-BACA-1FF2DF72147A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1F1AD58E-6D74-46B1-BACA-1FF2DF72147A}.Release|Any CPU.Build.0 = Release|Any CPU
{B8A7696E-C2F0-4AB9-9462-22A164671349}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B8A7696E-C2F0-4AB9-9462-22A164671349}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B8A7696E-C2F0-4AB9-9462-22A164671349}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B8A7696E-C2F0-4AB9-9462-22A164671349}.Release|Any CPU.Build.0 = Release|Any CPU
{CF1EBE16-9871-40C2-A668-145671963E25}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CF1EBE16-9871-40C2-A668-145671963E25}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CF1EBE16-9871-40C2-A668-145671963E25}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CF1EBE16-9871-40C2-A668-145671963E25}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{CBFEEEC0-56DA-4CE2-961E-38662AEC4B55} = {81A71712-6486-4CCD-95D1-0EA508ECA0E4}
{EE9477E9-AF89-4894-9FE2-A6E98B1E5410} = {CBFEEEC0-56DA-4CE2-961E-38662AEC4B55}
{B1A0A2D4-09E9-4065-9AF6-A35C677DDEBD} = {81A71712-6486-4CCD-95D1-0EA508ECA0E4}
{37463E67-0860-486E-BE33-A2E7165985A8} = {B1A0A2D4-09E9-4065-9AF6-A35C677DDEBD}
{D652BD7B-69A6-4DDE-850A-E869B7FACE3C} = {BD799AAB-0C70-412B-896E-F67E4FD5B55A}
{4E9B9194-6856-4D40-88B1-9613FF9083F1} = {D652BD7B-69A6-4DDE-850A-E869B7FACE3C}
{1F1AD58E-6D74-46B1-BACA-1FF2DF72147A} = {D652BD7B-69A6-4DDE-850A-E869B7FACE3C}
{42463238-2FED-4919-9428-C73EC43DDAEF} = {BD799AAB-0C70-412B-896E-F67E4FD5B55A}
{B8A7696E-C2F0-4AB9-9462-22A164671349} = {42463238-2FED-4919-9428-C73EC43DDAEF}
{CF1EBE16-9871-40C2-A668-145671963E25} = {42463238-2FED-4919-9428-C73EC43DDAEF}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {CAC0998E-B485-4B2F-9698-458375FE1335}
EndGlobalSection
EndGlobal
Loading

0 comments on commit ffeeb5b

Please sign in to comment.