< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Web.WebOperationContext
Assembly: CoreWCF.WebHttp
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.WebHttp/src/CoreWCF/Web/WebOperationContext.cs
Line coverage
9%
Covered lines: 13
Uncovered lines: 125
Coverable lines: 138
Total lines: 343
Line coverage: 9.4%
Branch coverage
10%
Covered branches: 6
Total branches: 60
Branch coverage: 10%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.WebHttp/src/CoreWCF/Web/WebOperationContext.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System;
 5using System.IO;
 6using System.Runtime.Serialization.Json;
 7using System.Text;
 8using System.Xml;
 9using System.Xml.Linq;
 10using System.Xml.Serialization;
 11using CoreWCF.Channels;
 12using CoreWCF.Description;
 13using CoreWCF.Dispatcher;
 14
 15namespace CoreWCF.Web
 16{
 17    public class WebOperationContext : IExtension<OperationContext>
 18    {
 019        internal static readonly string s_defaultTextMediaType = "text/plain";
 020        internal static readonly string s_defaultJsonMediaType = JsonGlobals.ApplicationJsonMediaType;
 021        internal static readonly string s_defaultXmlMediaType = "application/xml";
 022        internal static readonly string s_defaultAtomMediaType = "application/atom+xml";
 023        internal static readonly string s_defaultStreamMediaType = WebHttpBehavior.s_defaultStreamContentType;
 24
 25        private readonly OperationContext _operationContext;
 26
 3427        public WebOperationContext(OperationContext operationContext)
 28        {
 3429            _operationContext = operationContext ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nam
 30
 3431            if (operationContext.Extensions.Find<WebOperationContext>() == null)
 32            {
 3433                operationContext.Extensions.Add(this);
 34            }
 3435        }
 36
 37        public static WebOperationContext Current
 38        {
 39            get
 40            {
 5641                if (OperationContext.Current == null)
 42                {
 043                    return null;
 44                }
 45
 5646                WebOperationContext existing = OperationContext.Current.Extensions.Find<WebOperationContext>();
 5647                if (existing != null)
 48                {
 2249                    return existing;
 50                }
 51
 3452                return new WebOperationContext(OperationContext.Current);
 53            }
 54        }
 55
 1156        public IncomingWebRequestContext IncomingRequest => new IncomingWebRequestContext(_operationContext);
 57
 4158        public OutgoingWebResponseContext OutgoingResponse => new OutgoingWebResponseContext(_operationContext);
 59
 60        public void Attach(OperationContext owner)
 61        {
 3462        }
 63
 64        public void Detach(OperationContext owner)
 65        {
 066        }
 67
 68        public Message CreateJsonResponse<T>(T instance)
 69        {
 070            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
 71
 072            return CreateJsonResponse(instance, serializer);
 73        }
 74
 75        public Message CreateJsonResponse<T>(T instance, DataContractJsonSerializer serializer)
 76        {
 077            if (serializer == null)
 78            {
 079                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(serializer));
 80            }
 81
 082            Message message = Message.CreateMessage(MessageVersion.None, null, instance, serializer);
 083            message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.JsonProperty);
 084            AddContentType(s_defaultJsonMediaType, OutgoingResponse.BindingWriteEncoding);
 85
 086            return message;
 87        }
 88
 89        public Message CreateXmlResponse<T>(T instance)
 90        {
 091            System.Runtime.Serialization.DataContractSerializer serializer = new System.Runtime.Serialization.DataContra
 92
 093            return CreateXmlResponse(instance, serializer);
 94        }
 95
 96        public Message CreateXmlResponse<T>(T instance, System.Runtime.Serialization.XmlObjectSerializer serializer)
 97        {
 098            if (serializer == null)
 99            {
 0100                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(serializer));
 101            }
 102
 0103            Message message = Message.CreateMessage(MessageVersion.None, null, instance, serializer);
 0104            message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.XmlProperty);
 0105            AddContentType(s_defaultXmlMediaType, OutgoingResponse.BindingWriteEncoding);
 106
 0107            return message;
 108        }
 109
 110        public Message CreateXmlResponse<T>(T instance, XmlSerializer serializer)
 111        {
 0112            if (serializer == null)
 113            {
 0114                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(serializer));
 115            }
 116
 0117            Message message = Message.CreateMessage(MessageVersion.None, null, new XmlSerializerBodyWriter(instance, ser
 0118            message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.XmlProperty);
 0119            AddContentType(s_defaultXmlMediaType, OutgoingResponse.BindingWriteEncoding);
 120
 0121            return message;
 122        }
 123
 124        public Message CreateXmlResponse(XDocument document)
 125        {
 0126            if (document == null)
 127            {
 0128                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(document));
 129            }
 130
 131            Message message;
 0132            if (document.FirstNode == null)
 133            {
 0134                message = Message.CreateMessage(MessageVersion.None, null);
 135            }
 136            else
 137            {
 0138                message = Message.CreateMessage(MessageVersion.None, (string)null, document.CreateReader());
 139            }
 140
 0141            message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.XmlProperty);
 0142            AddContentType(s_defaultXmlMediaType, OutgoingResponse.BindingWriteEncoding);
 143
 0144            return message;
 145        }
 146
 147        public Message CreateXmlResponse(XElement element)
 148        {
 0149            if (element == null)
 150            {
 0151                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(element));
 152            }
 153
 0154            Message message = Message.CreateMessage(MessageVersion.None, null, element.CreateReader());
 0155            message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.XmlProperty);
 0156            AddContentType(s_defaultXmlMediaType, OutgoingResponse.BindingWriteEncoding);
 157
 0158            return message;
 159        }
 160
 0161        public Message CreateTextResponse(string text) => CreateTextResponse(text, s_defaultTextMediaType, Encoding.UTF8
 162
 0163        public Message CreateTextResponse(string text, string contentType) => CreateTextResponse(text, contentType, Enco
 164
 165        public Message CreateTextResponse(string text, string contentType, Encoding encoding)
 166        {
 0167            if (text == null)
 168            {
 0169                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(text));
 170            }
 171
 0172            if (contentType == null)
 173            {
 0174                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(contentType));
 175            }
 176
 0177            if (encoding == null)
 178            {
 0179                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(encoding));
 180            }
 181
 0182            Message message = new HttpStreamMessage(ActionOfStreamBodyWriter.CreateStreamBodyWriter((stream) =>
 0183            {
 0184                byte[] preamble = encoding.GetPreamble();
 0185                if (preamble.Length > 0)
 0186                {
 0187                    stream.Write(preamble, 0, preamble.Length);
 0188                }
 0189                byte[] bytes = encoding.GetBytes(text);
 0190                stream.Write(bytes, 0, bytes.Length);
 0191                stream.Flush();
 0192            }));
 0193            message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.RawProperty);
 0194            AddContentType(contentType, null);
 195
 0196            return message;
 197        }
 198
 199        public Message CreateTextResponse(Action<TextWriter> textWriter, string contentType)
 200        {
 0201            Encoding encoding = OutgoingResponse.BindingWriteEncoding;
 0202            if (encoding == null)
 203            {
 0204                encoding = Encoding.UTF8;
 205            }
 206
 0207            return CreateTextResponse(textWriter, contentType, encoding);
 208        }
 209
 210        public Message CreateTextResponse(Action<TextWriter> textWriter, string contentType, Encoding encoding)
 211        {
 0212            if (textWriter == null)
 213            {
 0214                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(textWriter));
 215            }
 216
 0217            if (contentType == null)
 218            {
 0219                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(contentType));
 220            }
 221
 0222            if (encoding == null)
 223            {
 0224                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(encoding));
 225            }
 226
 0227            Message message = new HttpStreamMessage(ActionOfStreamBodyWriter.CreateStreamBodyWriter((stream) =>
 0228            {
 0229                using (TextWriter writer = new StreamWriter(stream, encoding))
 0230                {
 0231                    textWriter(writer);
 0232                }
 0233            }));
 0234            message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.RawProperty);
 0235            AddContentType(contentType, null);
 236
 0237            return message;
 238        }
 239
 240        public Message CreateStreamResponse(Stream stream, string contentType)
 241        {
 0242            if (stream == null)
 243            {
 0244                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(stream));
 245            }
 246
 0247            if (contentType == null)
 248            {
 0249                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(contentType));
 250            }
 251
 0252            Message message = ByteStreamMessage.CreateMessage(stream);
 0253            message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.RawProperty);
 0254            AddContentType(contentType, null);
 255
 0256            return message;
 257        }
 258
 259        public Message CreateStreamResponse(StreamBodyWriter bodyWriter, string contentType)
 260        {
 0261            if (bodyWriter == null)
 262            {
 0263                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(bodyWriter));
 264            }
 265
 0266            if (contentType == null)
 267            {
 0268                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(contentType));
 269            }
 270
 0271            Message message = new HttpStreamMessage(bodyWriter);
 0272            message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.RawProperty);
 0273            AddContentType(contentType, null);
 274
 0275            return message;
 276        }
 277
 278        public Message CreateStreamResponse(Action<Stream> streamWriter, string contentType)
 279        {
 0280            if (streamWriter == null)
 281            {
 0282                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(streamWriter));
 283            }
 284
 0285            if (contentType == null)
 286            {
 0287                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(contentType));
 288            }
 289
 0290            Message message = new HttpStreamMessage(ActionOfStreamBodyWriter.CreateStreamBodyWriter(streamWriter));
 0291            message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.RawProperty);
 0292            AddContentType(contentType, null);
 293
 0294            return message;
 295        }
 296
 297        public UriTemplate GetUriTemplate(string operationName)
 298        {
 0299            if (string.IsNullOrEmpty(operationName))
 300            {
 0301                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(operationName));
 302            }
 303
 0304            if (!(OperationContext.Current.EndpointDispatcher.DispatchRuntime.OperationSelector is WebHttpDispatchOperat
 305            {
 0306                throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format(SR.O
 307            }
 308
 0309            return selector.GetUriTemplate(operationName);
 310        }
 311
 312        private void AddContentType(string contentType, Encoding encoding)
 313        {
 0314            if (string.IsNullOrEmpty(OutgoingResponse.ContentType))
 315            {
 0316                if (encoding != null)
 317                {
 0318                    contentType = WebMessageEncoderFactory.GetContentType(contentType, encoding);
 319                }
 320
 0321                OutgoingResponse.ContentType = contentType;
 322            }
 0323        }
 324
 325        internal class XmlSerializerBodyWriter : BodyWriter
 326        {
 327            private readonly object _instance;
 328            private readonly XmlSerializer _serializer;
 329
 330            public XmlSerializerBodyWriter(object instance, XmlSerializer serializer)
 0331                : base(false)
 332            {
 0333                _instance = instance ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(instance
 0334                _serializer = serializer ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(seri
 0335            }
 336
 337            protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
 338            {
 0339                _serializer.Serialize(writer, _instance);
 0340            }
 341        }
 342    }
 343}