Пример #1
0
 internal Task WriteAsync(HttpContext context, ApiSimulatorSettings settings)
 {
     context.Response.StatusCode = StatusCode;
     WriteReasonPhrase(context);
     WriteHeaders(context);
     return(WriteBodyAsync(context, settings));
 }
Пример #2
0
        private Task WriteContentAsync(HttpContext context, ApiSimulatorSettings settings)
        {
            ILookup <string, bool> accepts = ((string)context.Request.Headers["Accept"] ?? "")
                                             .Split(',')
                                             .Where(accept => !string.IsNullOrWhiteSpace(accept))
                                             .Select(accept => accept.Split(';')[0].Trim())
                                             .Distinct()
                                             .ToLookup(key => key, _ => true, StringComparer.InvariantCultureIgnoreCase);

            var acceptsAny     = accepts["*/*"].Any();
            var acceptsJson    = acceptsAny || accepts[JsonEncoding].Any();
            var acceptsTextXml = accepts[AlternateXmlEncoding].Any();
            var acceptsXml     = acceptsAny || accepts[XmlEncoding].Any() || acceptsTextXml;

            byte[]   data;
            string   contentType;
            Encoding charset;

            if (settings.XmlSettings != null && ((acceptsXml && !acceptsJson) || settings.JsonSettings == null))
            {
                XmlWriterSettings xmlSettings = settings.XmlSettings;
                contentType = !acceptsTextXml ? XmlEncoding : AlternateXmlEncoding;
                charset     = _encoding ?? xmlSettings.Encoding ?? Encoding.UTF8;
                data        = SerializeXml(Content, xmlSettings, charset);
            }
            else if (settings.JsonSettings != null)
            {
                JsonSerializerOptions jsonSettings = settings.JsonSettings;
                contentType = JsonEncoding;
                charset     = _encoding ?? Encoding.UTF8;
                data        = SerializeJson(Content, jsonSettings, charset);
            }
            else
            {
                throw new FormatException(SR.Format(SR.ApiResponseNotFormatted));
            }

            context.SetApiSimulatorBodyEncoding(charset);

            context.Response.ContentType   = contentType + "; charset=" + charset.HeaderName;
            context.Response.ContentLength = data.Length;

            return(context.Response.Body.WriteAsync(data, 0, data.Length));
        }
Пример #3
0
 internal ApiSimulator(ApiBuilder builder)
 {
     Settings = new ApiSimulatorSettings(builder);
 }
Пример #4
0
 /// <summary>
 /// Writes the body to the response stream.
 /// </summary>
 /// <param name="context">The <see cref="HttpContext"/>.</param>
 /// <param name="settings">The <see cref="ApiSimulatorSettings"/>.</param>
 /// <returns>A task that represents the asynchronous write operation.</returns>
 protected override Task WriteAsync(HttpContext context, ApiSimulatorSettings settings)
 => Content is Stream stream
Пример #5
0
 /// <summary>
 /// Deserializes object from request <see cref="Body"/>.
 /// </summary>
 /// <typeparam name="T">The type of the object to deserialize.</typeparam>
 /// <param name="context">The <see cref="HttpContext"/>.</param>
 /// <param name="settings">The <see cref="ApiSimulatorSettings"/>.</param>
 /// <returns>A task that represents the asynchronous read operation.</returns>
 protected override Task <T> ReadAsync <T>(HttpContext context, ApiSimulatorSettings settings)
 => ReadStreamAsync <T>(context.Request.Body, context.Request, settings);
Пример #6
0
        private static async Task <T> ReadStreamAsync <T>(Stream body, HttpRequest request, ApiSimulatorSettings settings)
        {
            if (request.GetMediaType() == JsonEncoding)
            {
                using (var ms = new MemoryStream())
                {
                    await CopyBytesAsync(body, ms, request.ContentLength).ConfigureAwait(false);

                    ms.Position = 0;
                    Encoding charset = request.HttpContext.GetApiSimulatorBodyEncoding() ?? request.GetCharset();
                    return(await DeserializeJsonAsync <T>(ms, settings.JsonSettings, charset).ConfigureAwait(false));
                }
            }
            else if (new[] { XmlEncoding, AlternateXmlEncoding }.Contains(request.GetMediaType()))
            {
                using (var ms = new MemoryStream())
                {
                    await CopyBytesAsync(body, ms, request.ContentLength).ConfigureAwait(false);

                    ms.Position = 0;
                    Encoding charset = request.HttpContext.GetApiSimulatorBodyEncoding() ?? request.GetCharset() ?? Encoding.UTF8;
                    return(DeserializeXml <T>(ms, new XmlReaderSettings(), charset));
                }
            }
            else
            {
                throw new NotSupportedException(SR.Format(SR.ApiRequestFormatNotSupported, request.ContentType));
            }
        }
Пример #7
0
 private Task WriteBodyAsync(HttpContext context, ApiSimulatorSettings settings)
 => Body == null
         ? Task.CompletedTask
         : Body.InternalWriteAsync(context, settings);
Пример #8
0
 public ApiSimulatorOwinMiddleware(ApiSimulatorSettings settings, ILogger logger, Action <ApiCall> apiCall)
 {
     _settings = settings;
     _logger   = logger;
     _apiCall  = apiCall;
 }