< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Channels.AspNetCoreWebSocketContext
Assembly: CoreWCF.Http
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Http/src/CoreWCF/Channels/AspNetCoreWebSocketContext.cs
Line coverage
13%
Covered lines: 5
Uncovered lines: 32
Coverable lines: 37
Total lines: 131
Line coverage: 13.5%
Branch coverage
0%
Covered branches: 0
Total branches: 18
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%11100%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Http/src/CoreWCF/Channels/AspNetCoreWebSocketContext.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.Collections.Specialized;
 7using System.Net;
 8using System.Net.WebSockets;
 9using System.Security.Principal;
 10using CoreWCF.Runtime;
 11using Microsoft.AspNetCore.Http;
 12using Microsoft.AspNetCore.Http.Extensions;
 13
 14namespace CoreWCF.Channels
 15{
 16    internal class AspNetCoreWebSocketContext : WebSocketContext
 17    {
 18        private readonly HttpContext _httpContext;
 19        private readonly WebSocket _webSocket;
 20        private CookieCollection _cookieCollection;
 21        private NameValueCollection _headers;
 22        private bool? _isLocal;
 23        private Uri _requestUri;
 24
 1125        internal AspNetCoreWebSocketContext(HttpContext httpContext, WebSocket webSocket)
 26        {
 27            Fx.Assert(httpContext != null, "HttpContext can't be null");
 28            Fx.Assert(webSocket != null, "WebSocket can't be null");
 1129            _httpContext = httpContext;
 1130            _webSocket = webSocket;
 1131        }
 32
 33        public override CookieCollection CookieCollection
 34        {
 35            get
 36            {
 037                if (_cookieCollection == null)
 38                {
 039                    var cookieContainer = new CookieContainer();
 040                    foreach (KeyValuePair<string, string> item in _httpContext.Request.Cookies)
 41                    {
 042                        cookieContainer.SetCookies(RequestUri, item.Value);
 43                    }
 44
 045                    _cookieCollection = cookieContainer.GetCookies(RequestUri);
 46                }
 47
 048                return _cookieCollection;
 49            }
 50        }
 51
 52        public override NameValueCollection Headers
 53        {
 54            get
 55            {
 056                if (_headers == null)
 57                {
 058                    var headers = new NameValueCollection();
 059                    foreach (KeyValuePair<string, Microsoft.Extensions.Primitives.StringValues> header in _httpContext.R
 60                    {
 061                        headers.Add(header.Key, header.Value);
 62                    }
 063                    _headers = headers;
 64                }
 65
 066                return _headers;
 67            }
 68        }
 69
 070        public override bool IsAuthenticated => _httpContext.User.Identity.IsAuthenticated;
 71
 72        public override bool IsLocal
 73        {
 74            get
 75            {
 076                if (!_isLocal.HasValue)
 77                {
 078                    ConnectionInfo connection = _httpContext.Connection;
 079                    if (connection.RemoteIpAddress != null)
 80                    {
 081                        if (connection.RemoteIpAddress.Equals(connection.LocalIpAddress))
 82                        {
 083                            _isLocal = true;
 84                        }
 85                        else
 86                        {
 087                            _isLocal = IPAddress.IsLoopback(connection.RemoteIpAddress);
 88                        }
 89                    }
 090                    else if (connection.LocalIpAddress == null)
 91                    {
 092                        _isLocal = true;
 93                    }
 94                    else
 95                    {
 096                        _isLocal = false;
 97                    }
 98                }
 99
 0100                return _isLocal.Value;
 101            }
 102        }
 103
 0104        public override bool IsSecureConnection => throw new PlatformNotSupportedException(nameof(IsSecureConnection));
 105
 0106        public override string Origin => Headers["Origin"];
 107
 108        public override Uri RequestUri
 109        {
 110            get
 111            {
 0112                if (_requestUri == null)
 113                {
 0114                    _requestUri = new Uri(_httpContext.Request.GetEncodedUrl());
 115                }
 116
 0117                return _requestUri;
 118            }
 119        }
 120
 0121        public override string SecWebSocketKey => Headers["Sec-WebSocket-Key"];
 122
 0123        public override IEnumerable<string> SecWebSocketProtocols => _httpContext.WebSockets.WebSocketRequestedProtocols
 124
 0125        public override string SecWebSocketVersion => Headers["Sec-WebSocket-Version"];
 126
 0127        public override IPrincipal User => _httpContext.User;
 128
 22129        public override WebSocket WebSocket => _webSocket;
 130    }
 131}