< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Dispatcher.HttpUnhandledOperationInvoker
Assembly: CoreWCF.WebHttp
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.WebHttp/src/CoreWCF/Dispatcher/HttpUnhandledOperationInvoker.cs
Line coverage
2%
Covered lines: 1
Uncovered lines: 43
Coverable lines: 44
Total lines: 122
Line coverage: 2.2%
Branch coverage
0%
Covered branches: 0
Total branches: 24
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
AllocateInputs()100%110%
InvokeAsync(...)0%24240%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.WebHttp/src/CoreWCF/Dispatcher/HttpUnhandledOperationInvoker.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.Diagnostics;
 6using System.IO;
 7using System.Net;
 8using System.Threading.Tasks;
 9using System.Xml.Linq;
 10using CoreWCF.Channels;
 11using CoreWCF.Web;
 12
 13namespace CoreWCF.Dispatcher
 14{
 15    internal class HttpUnhandledOperationInvoker : IOperationInvoker
 16    {
 17        private const string HtmlContentType = "text/html; charset=UTF-8";
 18        private const string HtmlMediaType = "text/html";
 19
 020        public bool IsSynchronous => true;
 21
 022        public object[] AllocateInputs() => new object[1];
 23
 3724        public Uri HelpUri { get; set; }
 25
 26        public ValueTask<(object returnValue, object[] outputs)> InvokeAsync(object instance, object[] inputs)
 27        {
 028            if (!(inputs[0] is Message message))
 29            {
 030                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
 031                    SR.Format(SR.HttpUnhandledOperationInvokerCalledWithoutMessage)));
 32            }
 33
 34            // We might be here because we desire a redirect...
 035            Uri newLocation = null;
 036            Uri to = message.Headers.To;
 037            if (message.Properties.ContainsKey(WebHttpDispatchOperationSelector.RedirectPropertyName))
 38            {
 039                newLocation = message.Properties[WebHttpDispatchOperationSelector.RedirectPropertyName] as Uri;
 40            }
 41
 042            if (newLocation != null && to != null)
 43            {
 44                // ...redirect
 045                Message redirectResult = WebOperationContext.Current.CreateStreamResponse(s => HelpHtmlBuilder.CreateTra
 046                WebOperationContext.Current.OutgoingResponse.Location = newLocation.AbsoluteUri;
 047                WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.TemporaryRedirect;
 048                WebOperationContext.Current.OutgoingResponse.ContentType = HtmlContentType;
 49
 50                // Note that no exception is thrown along this path, even if the debugger is attached
 51                //if (DiagnosticUtility.ShouldTraceInformation)
 52                //{
 53                //    TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.WebRequestRedirect,
 54                //        SR2.GetString(SR2.TraceCodeWebRequestRedirect, to, newLocation));
 55                //}
 056                return new ValueTask<(object, object[])>((redirectResult, (object[])null));
 57            }
 58
 59            // otherwise we are here to issue either a 404 or a 405
 060            bool uriMatched = false;
 061            if (message.Properties.ContainsKey(WebHttpDispatchOperationSelector.HttpOperationSelectorUriMatchedPropertyN
 62            {
 063                uriMatched = (bool)message.Properties[WebHttpDispatchOperationSelector.HttpOperationSelectorUriMatchedPr
 64            }
 65
 066            Message result = null;
 067            Uri helpUri = HelpUri != null ? UriTemplate.RewriteUri(HelpUri, WebOperationContext.Current.IncomingRequest.
 068            if (uriMatched)
 69            {
 070                WebHttpDispatchOperationSelectorData allowedMethodsData = null;
 071                if (message.Properties.TryGetValue(WebHttpDispatchOperationSelector.HttpOperationSelectorDataPropertyNam
 72                {
 073                    WebOperationContext.Current.OutgoingResponse.Headers[HttpResponseHeader.Allow] = allowedMethodsData.
 74                }
 075                result = WebOperationContext.Current.CreateStreamResponse(s => HelpHtmlBuilder.CreateMethodNotAllowedPag
 76            }
 77            else
 78            {
 079                result = WebOperationContext.Current.CreateStreamResponse(s => HelpHtmlBuilder.CreateEndpointNotFound(he
 80            }
 81
 082            WebOperationContext.Current.OutgoingResponse.StatusCode = uriMatched ? HttpStatusCode.MethodNotAllowed : Htt
 083            WebOperationContext.Current.OutgoingResponse.ContentType = HtmlContentType;
 84
 85            try
 86            {
 087                if (!uriMatched)
 88                {
 089                    if (Debugger.IsAttached)
 90                    {
 091                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.For
 092                            OperationContext.Current.IncomingMessageHeaders.To)));
 93                    }
 94                    else
 95                    {
 096                        DiagnosticUtility.TraceHandledException(new InvalidOperationException(SR.Format(SR.WebRequestDid
 097                                OperationContext.Current.IncomingMessageHeaders.To)), TraceEventType.Warning);
 98                    }
 99                }
 100                else
 101                {
 0102                    if (Debugger.IsAttached)
 103                    {
 0104                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.For
 0105                            WebOperationContext.Current.IncomingRequest.Method, OperationContext.Current.IncomingMessage
 106                    }
 107                    else
 108                    {
 0109                        DiagnosticUtility.TraceHandledException(new InvalidOperationException(SR.Format(SR.WebRequestDid
 0110                                WebOperationContext.Current.IncomingRequest.Method, OperationContext.Current.IncomingMes
 111                    }
 112                }
 0113            }
 0114            catch (InvalidOperationException)
 115            {
 116                // catch the exception - its only used for tracing
 0117            }
 118
 0119            return new ValueTask<(object, object[])>((result, (object[])null));
 120        }
 121    }
 122}