public JToken SerializeRequest(object request) { var httpRequest = (HttpRequest)request; var body = httpRequest.Body; httpRequest.Body = null; var json = JObject.FromObject(httpRequest); if (body != null) { json["body"] = payloadFormat.Serialize(body); } return(json); }
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 async Task <object> Respond(object requestObject) { var request = requestObject as HttpRequest; var requestMessage = new HttpRequestMessage(); requestMessage.Method = request.Method; requestMessage.RequestUri = new Uri(request.Path + (string.IsNullOrEmpty(request.Query) ? "" : ("?" + request.Query)), UriKind.Relative); if (request.Headers != null) { foreach (var header in request.Headers) { requestMessage.Headers.Add(header.Key, header.Value); } } if (!ReferenceEquals(null, request.Body)) { var memoryStream = new MemoryStream(); payloadFormat.Serialize(request.Body, memoryStream); memoryStream.Position = 0; requestMessage.Content = new StreamContent(memoryStream); requestMessage.Content.Headers.Add("content-type", payloadFormat.MimeType); } var responseMessage = await httpClient.SendAsync(requestMessage); var response = new HttpResponse(); response.Status = responseMessage.StatusCode; foreach (var header in responseMessage.Headers) { response.Headers = response.Headers ?? new Dictionary <string, string>(); if (header.Value.Count() != 1) { // TODO: we need to support multi-valued headers in the future throw new NotSupportedException("Currently, Impact does not yet support multi-valued http headers. The provider API responded with multiple values for the header \"" + header.Key + "\""); } response.Headers.Add(header.Key, header.Value.Single()); } if (responseMessage.Content != null) { var bodyStream = await responseMessage.Content.ReadAsStreamAsync(); response.Body = payloadFormat.Deserialize(bodyStream); } return(response); }
public JToken SerializeResponse(object response) { var httpResponse = (HttpResponse)response; var body = httpResponse.Body; httpResponse.Body = null; var json = JObject.FromObject(httpResponse); if (body != null) { json["body"] = payloadFormat.Serialize(body); } return(json); }
public JToken SerializeResponse(object response) { return(payloadFormat.Serialize(response)); }
public JToken SerializeRequest(object request) { return(payloadFormat.Serialize(request)); }