internal static Pact DefinePact(Func <Request, Response> requestHandler = null) { var pact = new Pact(); pact .Given("A foo with id 1 exists") .UponReceiving("A get foo request") .With(new Request { Type = "Foo", Ids = { "1" } }) .WithRequestArrayMatchingRule(r => r.Ids, i => i.LengthMin(1).All().Regex("^[1-9][0-9]*$")) .WillRespondWith(requestHandler ?? ValidRequestHandler) .WithResponseArrayMatchingRule(r => r.Bars, b => b.LengthMax(0)) .WithResponseArrayMatchingRule(r => r.Foos, r => r.At(0).With(f => f.Size, s => s.TypeMin(0))) .WithResponseArrayMatchingRule(r => r.Foos, r => r.All().With(f => f.Id, i => i.Regex("^[1-9][0-9]*$"))); pact .Given("A bar with id 12 exists") .UponReceiving("A get bar request") .With(new Request { Type = "Bar", Ids = { "12" } }) .WithRequestArrayMatchingRule(r => r.Ids, i => i.LengthMin(1).All().Regex("^[1-9][0-9]*$")) .WillRespondWith(requestHandler ?? ValidRequestHandler) .WithResponseArrayMatchingRule(r => r.Foos, b => b.LengthMax(0)) .WithResponseArrayMatchingRule(r => r.Bars, r => r.At(0).With(f => f.Name, s => s.Type())) .WithResponseArrayMatchingRule(r => r.Bars, r => r.All().With(f => f.Id, i => i.Regex("^[1-9][0-9]*$"))); return(pact); }
public PrintWeatherForecastUseCaseTest() { pact = new Pact(); pact.Given("") .UponReceiving("A weather forecast request") .With(new HttpRequest { Method = System.Net.Http.HttpMethod.Get, Path = "weatherforecast/Munich/3" }) .WithRequestMatchingRule(r => r.Path, r => r.Regex(pathFormat.ToString())) .WillRespondWith(request => { var match = pathFormat.Match(request.Path); return(new HttpResponse { Status = System.Net.HttpStatusCode.OK, Body = new WeatherForecast { City = match.Groups[1].Value, Date = new DateTime(2020, 3, 24), Summary = "Sunny", TemperatureC = 24, TemperatureF = 75 } }); }) .WithResponseMatchingRule(r => ((WeatherForecast)r.Body), r => r.Type()); server = new HttpMockServer(pact, new JsonPayloadFormat()); server.Start(); }
static PactDefinition() { Pact = new Pact(); var pathFormat = new Regex("weatherforecast\\/([a-zA-Z]+)\\/(\\d+)"); Pact.Given("") .UponReceiving("A weather forecast request") .With(new HttpRequest { Method = System.Net.Http.HttpMethod.Get, Path = "weatherforecast/Munich/3" }) .WithRequestMatchingRule(r => r.Path, r => r.Regex(pathFormat.ToString())) .WillRespondWith(request => { var match = pathFormat.Match(request.Path); return(new HttpResponse { Status = System.Net.HttpStatusCode.OK, Body = new WeatherForecast { City = match.Groups[1].Value, Date = new DateTime(2020, 3, 24), Summary = "Sunny", TemperatureC = 24, TemperatureF = 75 } }); }) .WithResponseMatchingRule(r => ((WeatherForecast)r.Body), r => r.Type()); }