public virtual bool Matches(IPactRequestDefinition request) { if (request == null) { throw new ArgumentNullException(nameof(request)); } if (Template.HttpVerb != request.HttpVerb) { return(false); } if (!CompareAcceptEmptyAsNull(Template.RequestPath, request.RequestPath)) { return(false); } if (!CompareAcceptEmptyAsNull(Template.Query, request.Query)) { return(false); } foreach (var header in Template.RequestHeaders) { if (request.RequestHeaders[header.Key]?.Equals(header.Value) != true) { return(false); } } var bodyComparison = new BodyComparer(Template, Config); return(bodyComparison.Matches(request)); }
IPactResponseDefinition IPactResponder.Respond(IPactRequestDefinition request) { if (request == null) { throw new ArgumentNullException(nameof(request)); } if (!_handlers.Any()) { var msg = $"Request received to {request.RequestPath}, but no pacts are registered"; _configuration.LogSafe(LogLevel.Error, msg); throw new InvalidOperationException(msg); } var result = _handlers .Select(h => h.Respond(request)) .Where(r => r != null) .FirstOrDefault(); if (result == null) { _configuration.LogSafe(LogLevel.Error, $"No match for {request.RequestPath}."); result = new UnknownResponse(_handlers.Select(h => h.DiffGram(request)).Where(d => d != null).ToArray()); } var config = _handlers.Select(h => h.Configuration).FirstOrDefault(l => l != null); config.LogSafe(LogLevel.Verbose, () => $"Replied to {new Rendering.PactRequestJsonRenderer(request)} with {new Rendering.PactResponseJsonRenderer(result)}"); return(result); }
public override JObject DiffGram(IPactRequestDefinition request) { if (Matches(request)) { return(null); } var expected = Template.RequestBody?.Render(); var actual = request.RequestBody?.Render(); requestMap = new JsonNodeMap(actual, Config); if (expected == null || actual == null) { return(GetDiff(expected, actual)); } var diff = new JObject(); switch (expected) { case JObject o: { DiffAsObject(o, actual as JObject, diff); break; } case JArray a: { DiffAsArray(a, actual as JArray, diff); break; } default: throw new NotImplementedException("Unknown type " + expected.GetType()); } return(diff); }
public IPactResponseDefinition Respond(IPactRequestDefinition request) { if (matcher.Matches(request)) { Configuration.LogSafe(LogLevel.Info, $"Request received at {request.RequestPath} matching expectation."); matches++; return(pact); } return(null); }
public virtual JObject DiffGram(IPactRequestDefinition request) { if (request == null) { throw new ArgumentNullException(nameof(request)); } dynamic diff = new JObject(); if (Template.HttpVerb != request.HttpVerb) { diff.Add("HttpVerb", GetDiff(Template.HttpVerb, request.HttpVerb)); } if (!CompareAcceptEmptyAsNull(Template.RequestPath, request.RequestPath)) { diff.Add("Path", GetDiff(Template.RequestPath, request.RequestPath)); } if (!CompareAcceptEmptyAsNull(Template.Query, request.Query)) { diff.Add("Query", GetDiff(Template.Query, request.Query)); } var headers = Template.RequestHeaders .Where(expected => true != request.RequestHeaders[expected.Key]?.Equals(expected.Value)).ToList(); if (headers.Any()) { dynamic headersDiff = new JObject(); foreach (var header in headers) { headersDiff.Add(header.Key, GetDiff(header.Value, request.RequestHeaders[header.Key])); } diff.Add("headers", headersDiff); } var bodyComparison = new BodyComparer(Template, Config); var bodies = bodyComparison.DiffGram(request); if (bodies != null) { diff.Add("body", bodies); } return(diff); }
public override bool Matches(IPactRequestDefinition request) { var expected = Template.RequestBody?.Render(); var actual = request.RequestBody?.Render(); requestMap = new JsonNodeMap(actual, Config); if (expected == null && actual == null) { return(true); } if (expected == null || actual == null) { return(false); } switch (expected) { case JObject o: return(CompareAsObject(o, actual as JObject)); case JArray a: return(CompareAsArray(a, actual as JArray)); default: throw new NotImplementedException("Unknown type " + expected.GetType()); } }
public PactRequestJsonRenderer(IPactRequestDefinition pact) { _pact = pact; }
public BodyComparer(IPactRequestDefinition template, IConsumerConfiguration config) : base(template, config) { templateMap = new JsonNodeMap(template.RequestBody?.Render(), config); }
public PactComparer(IPactRequestDefinition template, IConsumerConfiguration config) { Template = template ?? throw new ArgumentNullException(nameof(template)); Config = config; }
public JObject DiffGram(IPactRequestDefinition request) => matcher.DiffGram(request);