Ejemplo n.º 1
0
        public async Task ProcessResponseAsync([NotNull] IOwinResponse response, [NotNull] Rule rule)
        {
            if (response == null)
            {
                throw new ArgumentNullException("response");
            }
            if (rule == null)
            {
                throw new ArgumentNullException("rule");
            }

            if (!string.IsNullOrEmpty(rule.ResponsePath))
            {
                if (rule.Response != null)
                {
                    throw new ImpostorSettingsException(string.Format("Rule must not have both response and response path set at the same time (rule '{0}').", rule.RequestUrlPath));
                }

                var responseAction = await ReadResponseAsync(rule.ResponsePath);

                responseAction(response);
                return;
            }

            if (rule.Response == null)
            {
                throw new ImpostorSettingsException(string.Format("Rule must have either response or response path set (rule '{0}').", rule.RequestUrlPath));
            }

            response.StatusCode = rule.Response.StatusCode;
            if (!string.IsNullOrEmpty(rule.Response.ContentType))
            {
                response.ContentType = rule.Response.ContentType;
            }

            if (string.IsNullOrEmpty(rule.Response.ContentPath))
            {
                return;
            }

            using (var bodyStream = _ioFactory.CreateReadStream(rule.Response.ContentPath)) {
                response.ContentLength = bodyStream.Length;
                await bodyStream.CopyToAsync(response.Body);
            }
        }