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);
            }
        }
Ejemplo n.º 2
0
        public object DeserializeRequest(JToken requestToken)
        {
            var requestObject = (JObject)requestToken;

            var body = requestObject["body"];

            requestObject.Remove("body");

            var request = JsonConvert.DeserializeObject <HttpRequest>(requestObject.ToString());

            if (body != null)
            {
                request.Body = payloadFormat.Deserialize(body, null);
            }

            return(request);
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
 public object DeserializeRequest(JToken request)
 {
     return(payloadFormat.Deserialize(request, null));
 }