diff --git a/samples/OrdersApi/Consumer.Tests/OrdersClientTests.cs b/samples/OrdersApi/Consumer.Tests/OrdersClientTests.cs index a9127c1b..c99642cf 100644 --- a/samples/OrdersApi/Consumer.Tests/OrdersClientTests.cs +++ b/samples/OrdersApi/Consumer.Tests/OrdersClientTests.cs @@ -51,7 +51,7 @@ public async Task GetOrderAsync_WhenCalled_ReturnsOrder() this.pact .UponReceiving("a request for an order by ID") - .Given("an order with ID {id} exists", new Dictionary { ["id"] = "1" }) + .Given("an order with ID {id} exists", new Dictionary { ["id"] = 1 }) .WithRequest(HttpMethod.Get, "/api/orders/1") .WithHeader("Accept", "application/json") .WillRespond() @@ -121,7 +121,7 @@ public async Task UpdateOrderAsync_WhenCalled_UpdatesOrder() { this.pact .UponReceiving("a request to update the status of an order") - .Given("an order with ID {id} exists", new Dictionary { ["id"] = "1" }) + .Given("an order with ID {id} exists", new Dictionary { ["id"] = 1 }) .WithRequest(HttpMethod.Put, "/api/orders/1/status") .WithJsonBody(Match.Regex(OrderStatus.Fulfilling.ToString(), string.Join("|", Enum.GetNames()))) .WillRespond() diff --git a/src/PactNet.Abstractions/IMessageBuilder.cs b/src/PactNet.Abstractions/IMessageBuilder.cs index 22ddacf7..2bbc6e8d 100644 --- a/src/PactNet.Abstractions/IMessageBuilder.cs +++ b/src/PactNet.Abstractions/IMessageBuilder.cs @@ -21,7 +21,7 @@ public interface IMessageBuilderV3 /// Provider state description /// Provider state parameters /// Fluent builder - IMessageBuilderV3 Given(string providerState, IDictionary parameters); + IMessageBuilderV3 Given(string providerState, IDictionary parameters); /// /// Set the metadata @@ -65,7 +65,7 @@ public interface IMessageBuilderV4 /// Provider state description /// Provider state parameters /// Fluent builder - IMessageBuilderV4 Given(string providerState, IDictionary parameters); + IMessageBuilderV4 Given(string providerState, IDictionary parameters); /// /// Set the metadata diff --git a/src/PactNet.Abstractions/IRequestBuilder.cs b/src/PactNet.Abstractions/IRequestBuilder.cs index 357ea8ab..07edb8d4 100644 --- a/src/PactNet.Abstractions/IRequestBuilder.cs +++ b/src/PactNet.Abstractions/IRequestBuilder.cs @@ -123,7 +123,7 @@ public interface IRequestBuilderV3 /// Provider state description /// Provider state parameters /// Fluent builder - IRequestBuilderV3 Given(string providerState, IDictionary parameters); + IRequestBuilderV3 Given(string providerState, IDictionary parameters); /// /// Set the request @@ -242,7 +242,7 @@ public interface IRequestBuilderV4 /// Provider state description /// Provider state parameters /// Fluent builder - IRequestBuilderV4 Given(string providerState, IDictionary parameters); + IRequestBuilderV4 Given(string providerState, IDictionary parameters); /// /// Set the request diff --git a/src/PactNet.Abstractions/ProviderState.cs b/src/PactNet.Abstractions/ProviderState.cs index cded228e..648a53ca 100644 --- a/src/PactNet.Abstractions/ProviderState.cs +++ b/src/PactNet.Abstractions/ProviderState.cs @@ -15,6 +15,6 @@ public class ProviderState /// /// State parameters /// - public IDictionary Params { get; set; } + public IDictionary Params { get; set; } } } diff --git a/src/PactNet/Drivers/HttpInteractionDriver.cs b/src/PactNet/Drivers/HttpInteractionDriver.cs index 087148ca..f27c9374 100644 --- a/src/PactNet/Drivers/HttpInteractionDriver.cs +++ b/src/PactNet/Drivers/HttpInteractionDriver.cs @@ -1,7 +1,6 @@ using System; using PactNet.Interop; -using Newtonsoft.Json; -using Newtonsoft.Json.Serialization; +using System.Text.Json.Serialization; namespace PactNet.Drivers { /// @@ -38,9 +37,9 @@ public void Given(string description) /// Parameter name /// Parameter value /// Success - public void GivenWithParam(string description, string name, string value) + public void GivenWithParam(string description, string name, object value) { - var jsonValue = JsonConvert.SerializeObject(value); + var jsonValue = System.Text.Json.JsonSerializer.Serialize(value); NativeInterop.GivenWithParam(this.interaction, description, name, jsonValue).CheckInteropSuccess(); } diff --git a/src/PactNet/Drivers/IProviderStateDriver.cs b/src/PactNet/Drivers/IProviderStateDriver.cs index 4c992d07..1b53794d 100644 --- a/src/PactNet/Drivers/IProviderStateDriver.cs +++ b/src/PactNet/Drivers/IProviderStateDriver.cs @@ -17,6 +17,6 @@ internal interface IProviderStateDriver /// Provider state description /// Parameter name /// Parameter value - void GivenWithParam(string description, string name, string value); + void GivenWithParam(string description, string name, object value); } } diff --git a/src/PactNet/Drivers/MessageInteractionDriver.cs b/src/PactNet/Drivers/MessageInteractionDriver.cs index 46d2b923..08dead9e 100644 --- a/src/PactNet/Drivers/MessageInteractionDriver.cs +++ b/src/PactNet/Drivers/MessageInteractionDriver.cs @@ -1,8 +1,7 @@ using System; using System.Runtime.InteropServices; using PactNet.Interop; -using Newtonsoft.Json; -using Newtonsoft.Json.Serialization; +using System.Text.Json.Serialization; namespace PactNet.Drivers { /// @@ -35,9 +34,9 @@ public void Given(string description) /// Provider state description /// Parameter name /// Parameter value - public void GivenWithParam(string description, string name, string value) + public void GivenWithParam(string description, string name, object value) { - var jsonValue = JsonConvert.SerializeObject(value); + var jsonValue = System.Text.Json.JsonSerializer.Serialize(value); NativeInterop.GivenWithParam(this.interaction, description, name, jsonValue).CheckInteropSuccess(); } diff --git a/src/PactNet/MessageBuilder.cs b/src/PactNet/MessageBuilder.cs index 83c7cffe..f505b434 100644 --- a/src/PactNet/MessageBuilder.cs +++ b/src/PactNet/MessageBuilder.cs @@ -35,7 +35,7 @@ IMessageBuilderV3 IMessageBuilderV3.Given(string providerState) => Given(providerState); /// - IMessageBuilderV3 IMessageBuilderV3.Given(string providerState, IDictionary parameters) + IMessageBuilderV3 IMessageBuilderV3.Given(string providerState, IDictionary parameters) => Given(providerState, parameters); /// @@ -59,7 +59,7 @@ IMessageBuilderV4 IMessageBuilderV4.Given(string providerState) => Given(providerState); /// - IMessageBuilderV4 IMessageBuilderV4.Given(string providerState, IDictionary parameters) + IMessageBuilderV4 IMessageBuilderV4.Given(string providerState, IDictionary parameters) => Given(providerState, parameters); /// @@ -96,7 +96,7 @@ internal MessageBuilder Given(string providerState) /// Provider state description /// Provider state parameters /// Fluent builder - internal MessageBuilder Given(string providerState, IDictionary parameters) + internal MessageBuilder Given(string providerState, IDictionary parameters) { foreach (var param in parameters) { diff --git a/src/PactNet/RequestBuilder.cs b/src/PactNet/RequestBuilder.cs index 50dd4e89..fc90bdf0 100644 --- a/src/PactNet/RequestBuilder.cs +++ b/src/PactNet/RequestBuilder.cs @@ -158,7 +158,7 @@ IRequestBuilderV3 IRequestBuilderV3.Given(string providerState) /// Provider state description /// Provider state parameters /// Flient builder - IRequestBuilderV3 IRequestBuilderV3.Given(string providerState, IDictionary parameters) + IRequestBuilderV3 IRequestBuilderV3.Given(string providerState, IDictionary parameters) => this.Given(providerState, parameters); /// @@ -287,7 +287,7 @@ IRequestBuilderV4 IRequestBuilderV4.Given(string providerState) /// Provider state description /// Provider state parameters /// Flient builder - IRequestBuilderV4 IRequestBuilderV4.Given(string providerState, IDictionary parameters) + IRequestBuilderV4 IRequestBuilderV4.Given(string providerState, IDictionary parameters) => this.Given(providerState, parameters); /// @@ -417,7 +417,7 @@ internal RequestBuilder Given(string providerState) /// Provider state description /// Provider state parameters /// Fluent builder - internal RequestBuilder Given(string providerState, IDictionary parameters) + internal RequestBuilder Given(string providerState, IDictionary parameters) { foreach (var param in parameters) { diff --git a/tests/PactNet.Tests/Drivers/FfiIntegrationTests.cs b/tests/PactNet.Tests/Drivers/FfiIntegrationTests.cs index 7904797b..57b725d1 100644 --- a/tests/PactNet.Tests/Drivers/FfiIntegrationTests.cs +++ b/tests/PactNet.Tests/Drivers/FfiIntegrationTests.cs @@ -145,7 +145,7 @@ public async Task HttpInteraction_NumericProviderStateParameter_CreatesPactFile( IHttpInteractionDriver interaction = pact.NewHttpInteraction("an interaction with a numeric provider state param"); - interaction.GivenWithParam("state with param", "issue", "449"); + interaction.GivenWithParam("state with param", "issue", 449); interaction.WithRequest("GET", "/path"); interaction.WithResponseStatus((ushort)HttpStatusCode.OK); @@ -184,5 +184,59 @@ public async Task HttpInteraction_NumericProviderStateParameter_CreatesPactFile( string expectedPactContent = File.ReadAllText("data/v3-server-numeric-provider-state-param.json").TrimEnd(); pactContents.Should().Be(expectedPactContent); } + + [Fact] + [Trait("issue", "449")] + public async Task HttpInteraction_StringProviderStateParameter_CreatesPactFile() + { + var driver = new PactDriver(); + + try + { + IHttpPactDriver pact = driver.NewHttpPact("NativeDriverTests-Consumer-StringProviderStateParam", + "NativeDriverTests-Provider", + PactSpecification.V3); + + IHttpInteractionDriver interaction = pact.NewHttpInteraction("an interaction with a string provider state param"); + + interaction.GivenWithParam("state with param", "issue", "449"); + interaction.WithRequest("GET", "/path"); + + interaction.WithResponseStatus((ushort)HttpStatusCode.OK); + interaction.WithResponseBody("application/json", @"{""foo"":42}"); + + using IMockServerDriver mockServer = pact.CreateMockServer("127.0.0.1", null, false); + + var client = new HttpClient { BaseAddress = mockServer.Uri }; + + HttpResponseMessage result = await client.GetAsync("/path"); + result.StatusCode.Should().Be(HttpStatusCode.OK); + + string content = await result.Content.ReadAsStringAsync(); + content.Should().Be(@"{""foo"":42}"); + + mockServer.MockServerMismatches().Should().Be("[]"); + + string logs = mockServer.MockServerLogs(); + logs.Should().NotBeEmpty(); + + this.output.WriteLine("Mock Server Logs"); + this.output.WriteLine("----------------"); + this.output.WriteLine(logs); + + pact.WritePactFile(Environment.CurrentDirectory); + } + finally + { + this.WriteDriverLogs(driver); + } + + var file = new FileInfo("NativeDriverTests-Consumer-StringProviderStateParam-NativeDriverTests-Provider.json"); + file.Exists.Should().BeTrue(); + + string pactContents = File.ReadAllText(file.FullName).TrimEnd(); + string expectedPactContent = File.ReadAllText("data/v3-server-string-provider-state-param.json").TrimEnd(); + pactContents.Should().Be(expectedPactContent); + } } } diff --git a/tests/PactNet.Tests/MessageBuilderTests.cs b/tests/PactNet.Tests/MessageBuilderTests.cs index 4fa72c5c..4b1a13b1 100644 --- a/tests/PactNet.Tests/MessageBuilderTests.cs +++ b/tests/PactNet.Tests/MessageBuilderTests.cs @@ -45,7 +45,7 @@ public void Given_WhenCalled_AddsProviderState() public void Given_WithParams_AddsProviderState() { this.builder.Given("provider state", - new Dictionary + new Dictionary { ["foo"] = "bar", ["baz"] = "bash", diff --git a/tests/PactNet.Tests/PactExtensionsTests.cs b/tests/PactNet.Tests/PactExtensionsTests.cs index 160205b9..713ede2f 100644 --- a/tests/PactNet.Tests/PactExtensionsTests.cs +++ b/tests/PactNet.Tests/PactExtensionsTests.cs @@ -113,7 +113,7 @@ public async Task WithHttpInteractions_V3_CreatesExpectedPactFile() builder.UponReceiving("a sample request") .Given("a provider state") .Given("another provider state") - .Given("a provider state with params", new Dictionary + .Given("a provider state with params", new Dictionary { ["foo"] = "bar", ["baz"] = "bash" @@ -150,7 +150,7 @@ public async Task WithHttpInteractions_V4_CreatesExpectedPactFile() builder.UponReceiving("a sample request") .Given("a provider state") .Given("another provider state") - .Given("a provider state with params", new Dictionary + .Given("a provider state with params", new Dictionary { ["foo"] = "bar", ["baz"] = "bash" @@ -189,7 +189,7 @@ public void WithMessageInteractions_V3_CreatesExpectedPactFile() .ExpectsToReceive("a sample request") .Given("a provider state") .Given("another provider state") - .Given("a provider state with params", new Dictionary + .Given("a provider state with params", new Dictionary { ["foo"] = "bar", ["baz"] = "bash" @@ -215,7 +215,7 @@ public void WithMessageInteractions_V4_CreatesExpectedPactFile() .ExpectsToReceive("a sample request") .Given("a provider state") .Given("another provider state") - .Given("a provider state with params", new Dictionary + .Given("a provider state with params", new Dictionary { ["foo"] = "bar", ["baz"] = "bash" @@ -241,7 +241,7 @@ public async Task CombinedHttpAndMessageInteractions_v4_CreatesExpectedPactFile( http.UponReceiving("a HTTP request") .Given("a provider state") .Given("another provider state") - .Given("a provider state with params", new Dictionary + .Given("a provider state with params", new Dictionary { ["foo"] = "bar", ["baz"] = "bash" @@ -271,7 +271,7 @@ await http.VerifyAsync(async ctx => .ExpectsToReceive("a message") .Given("a provider state") .Given("another provider state") - .Given("a provider state with params", new Dictionary + .Given("a provider state with params", new Dictionary { ["foo"] = "bar", ["baz"] = "bash" diff --git a/tests/PactNet.Tests/RequestBuilderTests.cs b/tests/PactNet.Tests/RequestBuilderTests.cs index d4be9624..7464c0c1 100644 --- a/tests/PactNet.Tests/RequestBuilderTests.cs +++ b/tests/PactNet.Tests/RequestBuilderTests.cs @@ -39,7 +39,7 @@ public void Given_WhenCalled_AddsProviderState() public void Given_WithParams_AddsProviderState() { this.builder.Given("provider state", - new Dictionary + new Dictionary { ["foo"] = "bar", ["baz"] = "bash", diff --git a/tests/PactNet.Tests/data/v3-server-numeric-provider-state-param.json b/tests/PactNet.Tests/data/v3-server-numeric-provider-state-param.json index ac457848..798ea60f 100644 --- a/tests/PactNet.Tests/data/v3-server-numeric-provider-state-param.json +++ b/tests/PactNet.Tests/data/v3-server-numeric-provider-state-param.json @@ -9,7 +9,7 @@ { "name": "state with param", "params": { - "issue": "449" + "issue": 449 } } ], @@ -30,8 +30,8 @@ ], "metadata": { "pactRust": { - "ffi": "0.4.0", - "models": "1.0.4" + "ffi": "0.4.16", + "models": "1.1.19" }, "pactSpecification": { "version": "3.0.0" diff --git a/tests/PactNet.Tests/data/v3-server-string-provider-state-param.json b/tests/PactNet.Tests/data/v3-server-string-provider-state-param.json new file mode 100644 index 00000000..d441d7ca --- /dev/null +++ b/tests/PactNet.Tests/data/v3-server-string-provider-state-param.json @@ -0,0 +1,43 @@ +{ + "consumer": { + "name": "NativeDriverTests-Consumer-StringProviderStateParam" + }, + "interactions": [ + { + "description": "an interaction with a string provider state param", + "providerStates": [ + { + "name": "state with param", + "params": { + "issue": "449" + } + } + ], + "request": { + "method": "GET", + "path": "/path" + }, + "response": { + "body": { + "foo": 42 + }, + "headers": { + "Content-Type": "application/json" + }, + "status": 200 + } + } + ], + "metadata": { + "pactRust": { + "ffi": "0.4.16", + "models": "1.1.19" + }, + "pactSpecification": { + "version": "3.0.0" + } + }, + "provider": { + "name": "NativeDriverTests-Provider" + } +} \ No newline at end of file