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);
            }
        }
Пример #2
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, Route = "{*route}")] HttpRequest req,
            string route)
        {
            var requestBody = new MemoryStream();
            await req.Body.CopyToAsync(requestBody);

            requestBody.Seek(0, SeekOrigin.Begin);
            var request = new Core.Transport.Http.HttpRequest
            {
                Method = new HttpMethod(req.Method),
                Path   = route,
                Query  = req.QueryString.Value,
                Body   = PactDefinition.PayloadFormat.Deserialize(requestBody)
            };

            if (req.Headers.Any())
            {
                request.Headers = request.Headers ?? new Dictionary <string, string>();
                foreach (var header in req.Headers)
                {
                    request.Headers[header.Key] = header.Value;
                }
            }

            Core.Transport.Http.HttpResponse response;
            try
            {
                var interaction = PactDefinition.Pact.GetMatchingInteraction(request, PactDefinition.TransportMatchers);
                if (request.Body is JObject jsonBody && interaction.RequestType != typeof(JObject))
                {
                    request.Body = PactDefinition.PayloadFormat.Deserialize(jsonBody, interaction.RequestType);
                }

                response = (Core.Transport.Http.HttpResponse)interaction.Respond(request, PactDefinition.TransportMatchers);
            }
            catch (Exception ex)
            {
                return(new NotFoundObjectResult("Getting a response from the pact failed for this request. Exception: " + Environment.NewLine + ex));
            }

            if (response.Headers != null)
            {
                foreach (var header in response.Headers)
                {
                    req.HttpContext.Response.Headers.Add(header.Key, header.Value);
                }
            }

            if (response.Body != null)
            {
                var memoryStream = new MemoryStream();
                PactDefinition.PayloadFormat.Serialize(response.Body, memoryStream);

                return(new FileContentResult(memoryStream.ToArray(),
                                             string.IsNullOrEmpty(PactDefinition.PayloadFormat.MimeType)
                    ? "application/octet-stream"
                    :  PactDefinition.PayloadFormat.MimeType));
            }
            else
            {
                return(new StatusCodeResult((int)response.Status));
            }
        }