-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProviderApiTests.cs
86 lines (75 loc) · 3.05 KB
/
ProviderApiTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;
using System.IO;
using System.Collections.Generic;
using PactNet.Infrastructure.Outputters;
using PactNet.Output.Xunit;
using PactNet.Verifier;
using PactNet;
using Xunit;
using Xunit.Abstractions;
using System.Text.Json;
using Products;
namespace tests;
public class ProviderApiTests : IDisposable
{
private ITestOutputHelper _outputHelper { get; }
private string pactUrl;
private readonly PactVerifier verifier;
public void Dispose()
{
// make sure you dispose the verifier to stop the internal messaging server
GC.SuppressFinalize(this);
this.verifier.Dispose();
}
private static readonly JsonSerializerOptions Options = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
PropertyNameCaseInsensitive = true
};
public ProviderApiTests(ITestOutputHelper output)
{
_outputHelper = output;
var config = new PactVerifierConfig
{
// NOTE: We default to using a ConsoleOutput,
// however xUnit 2 does not capture the console output,
// so a custom outputter is required.
Outputters = new List<IOutput>
{
new XunitOutput(_outputHelper),
new ConsoleOutput()
},
// Output verbose verification logs to the test output
LogLevel = PactLogLevel.Information,
};
string providerName = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("PACT_PROVIDER_NAME"))
? Environment.GetEnvironmentVariable("PACT_PROVIDER_NAME")
: "pactflow-example-provider-dotnet-kafka";
verifier = new PactVerifier(providerName, config);
pactUrl = Environment.GetEnvironmentVariable("PACT_URL") ?? Path.Combine(AppContext.BaseDirectory, "..","..","..","..","..","consumer-dotnet-kafka","tests","pacts","pactflow-example-consumer-dotnet-kafka-pactflow-example-provider-dotnet-kafka.json");
}
[Fact]
public void EnsureProviderApiHonoursPactWithConsumer()
{
// Arrange
verifier
.WithHttpEndpoint(new Uri("http://localhost:49152"))
.WithMessages(scenarios =>
{
scenarios
.Add("a product event update", builder =>
{
builder
.WithMetadata(new
{
ContentType = "application/json",
kafka_topic = "products"
})
.WithContent(() => new ProductEventProducer().CreateEvent(new Product("1", "food", "pancake", "1.0.0"), "UPDATED"));
});
}, Options)
.WithFileSource(new FileInfo(pactUrl))
// Act
.Verify();
}
}