< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Web.WebFaultException
Assembly: CoreWCF.WebHttp
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.WebHttp/src/CoreWCF/Web/WebFaultException .cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 64
Coverable lines: 64
Total lines: 160
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 57
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%110%
.ctor(...)100%110%
GetObjectData(...)100%110%
GetFaultCode(...)0%220%
GetDefaultReason(...)0%55550%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.WebHttp/src/CoreWCF/Web/WebFaultException .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.Collections.Generic;
 6using System.Net;
 7using System.Runtime.Serialization;
 8using System.Security;
 9using System.Security.Permissions;
 10
 11namespace CoreWCF.Web
 12{
 13    [Serializable]
 14    public class WebFaultException : FaultException, IWebFaultException
 15    {
 16        internal const string WebFaultCodeNamespace = "http://schemas.microsoft.com/2009/WebFault";
 17
 18        public WebFaultException(HttpStatusCode statusCode)
 019            : base(GetDefaultReason(statusCode), GetFaultCode(statusCode))
 20        {
 021            StatusCode = statusCode;
 022        }
 23
 24        protected WebFaultException(SerializationInfo info, StreamingContext context)
 025            : base(info, context)
 26        {
 027            StatusCode = (HttpStatusCode)info.GetValue("statusCode", typeof(HttpStatusCode));
 028        }
 29
 030        public HttpStatusCode StatusCode { get; private set; }
 31
 032        Type IWebFaultException.DetailType => null;
 33
 034        object IWebFaultException.DetailObject => null;
 35
 036        Type[] IWebFaultException.KnownTypes => null;
 37
 38        public override void GetObjectData(SerializationInfo info, StreamingContext context)
 39        {
 040            base.GetObjectData(info, context);
 41
 042            info.AddValue("statusCode", StatusCode);
 043        }
 44
 45        internal static FaultCode GetFaultCode(HttpStatusCode statusCode)
 46        {
 047            if ((int)statusCode >= (int)HttpStatusCode.InternalServerError)
 48            {
 049                return FaultCode.CreateReceiverFaultCode(statusCode.ToString(), WebFaultCodeNamespace);
 50            }
 51            else
 52            {
 053                return FaultCode.CreateSenderFaultCode(statusCode.ToString(), WebFaultCodeNamespace);
 54            }
 55        }
 56
 57        // These reasons come from section 6.1.1 of http://www.ietf.org/rfc/rfc2616.txt
 58        internal static string GetDefaultReason(HttpStatusCode statusCode)
 59        {
 060            switch ((int)statusCode)
 61            {
 062                case 100: return "Continue";
 063                case 101: return "Switching Protocols";
 064                case 200: return "OK";
 065                case 201: return "Created";
 066                case 202: return "Accepted";
 067                case 203: return "Non-Authoritative Information";
 068                case 204: return "No Content";
 069                case 205: return "Reset Content";
 070                case 206: return "Partial Content";
 071                case 300: return "Multiple Choices";
 072                case 301: return "Moved Permanently";
 073                case 302: return "Found";
 074                case 303: return "See Other";
 075                case 304: return "Not Modified";
 076                case 305: return "Use Proxy";
 077                case 307: return "Temporary Redirect";
 078                case 400: return "Bad Request";
 079                case 401: return "Unauthorized";
 080                case 402: return "Payment Required";
 081                case 403: return "Forbidden";
 082                case 404: return "Not Found";
 083                case 405: return "Method Not Allowed";
 084                case 406: return "Not Acceptable";
 085                case 407: return "Proxy Authentication Required";
 086                case 408: return "Request Time-out";
 087                case 409: return "Conflict";
 088                case 410: return "Gone";
 089                case 411: return "Length Required";
 090                case 412: return "Precondition Failed";
 091                case 413: return "Request Entity Too Large";
 092                case 414: return "Request-URI Too Large";
 093                case 415: return "Unsupported Media Type";
 094                case 416: return "Requested range not satisfiable";
 095                case 417: return "Expectation Failed";
 096                case 500: return "Internal Server Error";
 097                case 501: return "Not Implemented";
 098                case 502: return "Bad Gateway";
 099                case 503: return "Service Unavailable";
 0100                case 504: return "Gateway Time-out";
 0101                case 505: return "HTTP Version not supported";
 102                default:
 103                    {
 0104                        int errorClass = ((int)statusCode) / 100;
 105                        switch (errorClass)
 106                        {
 0107                            case 1: return "Informational";
 0108                            case 2: return "Success";
 0109                            case 3: return "Redirection";
 0110                            case 4: return "Client Error";
 0111                            case 5: return "Server Error";
 0112                            default: return null;
 113                        }
 114                    }
 115            }
 116        }
 117    }
 118
 119    [Serializable]
 120    public class WebFaultException<T> : FaultException<T>, IWebFaultException
 121    {
 122        private Type[] _knownTypes;
 123
 124        public WebFaultException(T detail, HttpStatusCode statusCode)
 125            : base(detail, WebFaultException.GetDefaultReason(statusCode), WebFaultException.GetFaultCode(statusCode))
 126        {
 127            StatusCode = statusCode;
 128        }
 129
 130        public WebFaultException(T detail, HttpStatusCode statusCode, IEnumerable<Type> knownTypes)
 131            : base(detail, WebFaultException.GetDefaultReason(statusCode), WebFaultException.GetFaultCode(statusCode))
 132        {
 133            StatusCode = statusCode;
 134            _knownTypes = (knownTypes == null) ? null : new List<Type>(knownTypes).ToArray();
 135        }
 136
 137        protected WebFaultException(SerializationInfo info, StreamingContext context)
 138            : base(info, context)
 139        {
 140            StatusCode = (HttpStatusCode)info.GetValue("statusCode", typeof(HttpStatusCode));
 141            _knownTypes = (Type[])info.GetValue("knownTypes", typeof(Type[]));
 142        }
 143
 144        public HttpStatusCode StatusCode { get; private set; }
 145
 146        Type IWebFaultException.DetailType => typeof(T);
 147
 148        object IWebFaultException.DetailObject => Detail;
 149
 150        Type[] IWebFaultException.KnownTypes => _knownTypes;
 151
 152        public override void GetObjectData(SerializationInfo info, StreamingContext context)
 153        {
 154            base.GetObjectData(info, context);
 155
 156            info.AddValue("statusCode", StatusCode);
 157            info.AddValue("knownTypes", _knownTypes);
 158        }
 159    }
 160}