public async Task OnRequest(string path) { var request = new Core.Transport.Http.HttpRequest { Method = new HttpMethod(Request.Method), Path = Request.Path.Value, Query = Request.QueryString.Value, Body = payloadFormat.Deserialize(Request.Body) }; if (Request.Headers.Any()) { request.Headers = request.Headers ?? new Dictionary <string, string>(); foreach (var header in Request.Headers) { request.Headers[header.Key] = header.Value; } } Core.Transport.Http.HttpResponse response; try { var interaction = pact.GetMatchingInteraction(request, transportMatchers); if (request.Body is JObject jsonBody && interaction.RequestType != typeof(JObject)) { request.Body = payloadFormat.Deserialize(jsonBody, interaction.RequestType); } response = (Core.Transport.Http.HttpResponse)interaction.Respond(request, transportMatchers); } catch (Exception ex) { Response.StatusCode = 404; var bytes = Encoding.UTF8.GetBytes("Getting a response from the pact failed for this request. Exception: " + Environment.NewLine + ex); await Response.Body.WriteAsync(bytes, 0, bytes.Length); return; } Response.StatusCode = (int)response.Status; if (response.Headers != null) { foreach (var header in response.Headers) { Response.Headers.Add(header.Key, header.Value); } } if (response.Body != null) { if (!string.IsNullOrEmpty(payloadFormat.MimeType)) { Response.ContentType = payloadFormat.MimeType; } payloadFormat.Serialize(response.Body, Response.Body); } }
public TResponse SendRequest <TRequest, TResponse>(TRequest request) { var interaction = pact.GetMatchingInteraction(request, matchers); return((TResponse)interaction.Respond(request, matchers)); }