< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Channels.ServerWebSocketTransportDuplexSessionChannel
Assembly: CoreWCF.Http
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Http/src/CoreWCF/Channels/ServerWebSocketTransportDuplexSessionChannel.cs
Line coverage
97%
Covered lines: 45
Uncovered lines: 1
Coverable lines: 46
Total lines: 151
Line coverage: 97.8%
Branch coverage
78%
Covered branches: 11
Total branches: 14
Branch coverage: 78.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%11100%
GetProperty()100%66100%
SetWebSocketInfo(...)50%2293.33%
OnClosed()100%11100%
OnOpenAsync(...)100%22100%
ProcessAuthentication()100%11100%
ShouldProcessAuthentication()50%44100%
.ctor(...)100%11100%
UpdateMessageProperties(...)100%11100%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Http/src/CoreWCF/Channels/ServerWebSocketTransportDuplexSessionChannel.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.Net.WebSockets;
 6using System.Threading;
 7using System.Threading.Tasks;
 8using CoreWCF.Runtime;
 9using CoreWCF.Security;
 10using Microsoft.AspNetCore.Http;
 11using Microsoft.Extensions.DependencyInjection;
 12
 13namespace CoreWCF.Channels
 14{
 15    internal class ServerWebSocketTransportDuplexSessionChannel : WebSocketTransportDuplexSessionChannel
 16    {
 17        private WebSocketContext _webSocketContext;
 18        private readonly HttpContext _httpContext;
 19        private readonly IHttpTransportFactorySettings _transportSettings;
 20        private readonly IServiceProvider _serviceProvider;
 21        private WebSocketMessageSource _webSocketMessageSource;
 22        private SessionOpenNotification _sessionOpenNotification;
 23
 24        public ServerWebSocketTransportDuplexSessionChannel(HttpContext httpContext, WebSocketContext webSocketContext, 
 1125            : base(settings, new EndpointAddress(localVia), localVia)
 26        {
 1127            _httpContext = httpContext;
 1128            _webSocketContext = webSocketContext;
 1129            _transportSettings = settings;
 1130            _serviceProvider = serviceProvider;
 1131        }
 32
 33        protected override bool IsStreamedOutput
 34        {
 1535            get { return TransferModeHelper.IsResponseStreamed(TransferMode); }
 36        }
 37
 38        public override T GetProperty<T>()
 39        {
 4540            if (typeof(T) == typeof(SessionOpenNotification))
 41            {
 1142                if (_sessionOpenNotification == null)
 43                {
 1144                    _sessionOpenNotification = new SessionOpenNotificationHelper(this);
 45                }
 46
 1147                return (T)(object)_sessionOpenNotification;
 48            }
 49
 3450            T service = _serviceProvider.GetService<T>();
 3451            if (service == null)
 52            {
 1253                service = base.GetProperty<T>();
 54            }
 55
 3456            return service;
 57        }
 58
 59        internal void SetWebSocketInfo(WebSocketContext webSocketContext, RemoteEndpointMessageProperty remoteEndpointMe
 60        {
 61            Fx.Assert(webSocketContext != null, "webSocketContext should not be null.");
 1162            ShouldDisposeWebSocketAfterClosed = shouldDisposeWebSocketAfterClosed;
 1163            _webSocketContext = webSocketContext;
 1164            WebSocket = webSocketContext.WebSocket;
 65
 1166            if (handshakeSecurityMessageProperty != null)
 67            {
 068                RemoteSecurity = handshakeSecurityMessageProperty;
 69            }
 70
 1171            bool inputUseStreaming = TransferModeHelper.IsRequestStreamed(TransferMode);
 1172            _webSocketMessageSource = new WebSocketMessageSource(
 1173                            this,
 1174                            _webSocketContext,
 1175                            inputUseStreaming,
 1176                            remoteEndpointMessageProperty,
 1177                            this,
 1178                            httpContext);
 79
 1180            SetMessageSource(_webSocketMessageSource);
 1181        }
 82
 83        protected override void OnClosed()
 84        {
 1185            base.OnClosed();
 1186        }
 87
 88        protected override Task OnOpenAsync(CancellationToken token)
 89        {
 1190            RemoteEndpointMessageProperty remoteEndpointMessageProperty = null;
 1191            if (_httpContext.Connection.RemoteIpAddress != null)
 92            {
 1193                remoteEndpointMessageProperty = new RemoteEndpointMessageProperty(_httpContext.Connection.RemoteIpAddres
 94            }
 95
 1196            SetWebSocketInfo(_webSocketContext, remoteEndpointMessageProperty, ProcessAuthentication(), true, _httpConte
 97            //if (TD.WebSocketConnectionAcceptedIsEnabled())
 98            //{
 99            //    TD.WebSocketConnectionAccepted(
 100            //        this._httpContext.EventTraceActivity,
 101            //        this.WebSocket != null ? this.WebSocket.GetHashCode() : -1);
 102            //}
 103
 11104            return Task.CompletedTask;
 105        }
 106
 107        private SecurityMessageProperty ProcessAuthentication()
 108        {
 11109            if (ShouldProcessAuthentication())
 110            {
 111                // TODO: Create SecurityMessageProperty further up stack
 11112                return null;
 113                //return this.ProcessRequiredAuthentication();
 114            }
 115            else
 116            {
 117                return null;
 118            }
 119        }
 120
 121        private bool ShouldProcessAuthentication()
 122        {
 123            Fx.Assert(_transportSettings != null, "IsAuthenticated should only be called if _transportSettings != null")
 124            Fx.Assert(_httpContext != null, "IsAuthenticated should only be called if _httpContext != null");
 11125            return _transportSettings.IsAuthenticationRequired || (_transportSettings.IsAuthenticationSupported && _http
 126        }
 127
 128        private class SessionOpenNotificationHelper : SessionOpenNotification
 129        {
 130            private readonly ServerWebSocketTransportDuplexSessionChannel _channel;
 131
 11132            public SessionOpenNotificationHelper(ServerWebSocketTransportDuplexSessionChannel channel)
 133            {
 11134                _channel = channel;
 11135            }
 136
 137            public override bool IsEnabled
 138            {
 139                get
 140                {
 11141                    return _channel.WebSocketSettings.CreateNotificationOnConnection;
 142                }
 143            }
 144
 145            public override void UpdateMessageProperties(MessageProperties inboundMessageProperties)
 146            {
 1147                _channel._webSocketMessageSource.UpdateOpenNotificationMessageProperties(inboundMessageProperties);
 1148            }
 149        }
 150    }
 151}