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
+
4
+ using System ;
5
+ using System . Net ;
6
+ using System . Threading ;
7
+ using System . Threading . Tasks ;
8
+ using Microsoft . AspNetCore . Connections ;
9
+ using Microsoft . AspNetCore . Connections . Features ;
10
+ using Microsoft . AspNetCore . Server . Kestrel . Core . Internal . Infrastructure ;
11
+ using Microsoft . AspNetCore . InternalTesting ;
12
+ using Moq ;
13
+ using Xunit ;
14
+
15
+ namespace Microsoft . AspNetCore . Server . Kestrel . Core . Tests ;
16
+
17
+ public class TransportConnectionFeatureCollectionTests
18
+ {
19
+ [ Fact ]
20
+ public void IConnectionEndPointFeature_IsAvailableInFeatureCollection ( )
21
+ {
22
+ var serviceContext = new TestServiceContext ( ) ;
23
+ var connection = new Mock < DefaultConnectionContext > { CallBase = true } . Object ;
24
+ var transportConnectionManager = new TransportConnectionManager ( serviceContext . ConnectionManager ) ;
25
+ var kestrelConnection = CreateKestrelConnection ( serviceContext , connection , transportConnectionManager ) ;
26
+
27
+ var endpointFeature = kestrelConnection . TransportConnection . Features . Get < IConnectionEndPointFeature > ( ) ;
28
+
29
+ Assert . NotNull ( endpointFeature ) ;
30
+ }
31
+
32
+ [ Fact ]
33
+ public void IConnectionEndPointFeature_ReturnsCorrectLocalEndPoint ( )
34
+ {
35
+ var serviceContext = new TestServiceContext ( ) ;
36
+ var connection = new Mock < DefaultConnectionContext > { CallBase = true } . Object ;
37
+ var expectedLocalEndPoint = new IPEndPoint ( IPAddress . Parse ( "127.0.0.1" ) , 8080 ) ;
38
+ connection . LocalEndPoint = expectedLocalEndPoint ;
39
+ var transportConnectionManager = new TransportConnectionManager ( serviceContext . ConnectionManager ) ;
40
+ var kestrelConnection = CreateKestrelConnection ( serviceContext , connection , transportConnectionManager ) ;
41
+
42
+ var endpointFeature = kestrelConnection . TransportConnection . Features . Get < IConnectionEndPointFeature > ( ) ;
43
+
44
+ Assert . NotNull ( endpointFeature ) ;
45
+ Assert . Equal ( expectedLocalEndPoint , endpointFeature . LocalEndPoint ) ;
46
+ }
47
+
48
+ [ Fact ]
49
+ public void IConnectionEndPointFeature_ReturnsCorrectRemoteEndPoint ( )
50
+ {
51
+ var serviceContext = new TestServiceContext ( ) ;
52
+ var connection = new Mock < DefaultConnectionContext > { CallBase = true } . Object ;
53
+ var expectedRemoteEndPoint = new IPEndPoint ( IPAddress . Parse ( "192.168.1.100" ) , 54321 ) ;
54
+ connection . RemoteEndPoint = expectedRemoteEndPoint ;
55
+ var transportConnectionManager = new TransportConnectionManager ( serviceContext . ConnectionManager ) ;
56
+ var kestrelConnection = CreateKestrelConnection ( serviceContext , connection , transportConnectionManager ) ;
57
+
58
+ var endpointFeature = kestrelConnection . TransportConnection . Features . Get < IConnectionEndPointFeature > ( ) ;
59
+
60
+ Assert . NotNull ( endpointFeature ) ;
61
+ Assert . Equal ( expectedRemoteEndPoint , endpointFeature . RemoteEndPoint ) ;
62
+ }
63
+
64
+ [ Fact ]
65
+ public void IConnectionEndPointFeature_AllowsSettingLocalEndPoint ( )
66
+ {
67
+ var serviceContext = new TestServiceContext ( ) ;
68
+ var connection = new Mock < DefaultConnectionContext > { CallBase = true } . Object ;
69
+ var transportConnectionManager = new TransportConnectionManager ( serviceContext . ConnectionManager ) ;
70
+ var kestrelConnection = CreateKestrelConnection ( serviceContext , connection , transportConnectionManager ) ;
71
+ var newLocalEndPoint = new IPEndPoint ( IPAddress . Parse ( "127.0.0.1" ) , 9090 ) ;
72
+
73
+ var endpointFeature = kestrelConnection . TransportConnection . Features . Get < IConnectionEndPointFeature > ( ) ;
74
+ endpointFeature . LocalEndPoint = newLocalEndPoint ;
75
+
76
+ Assert . Equal ( newLocalEndPoint , kestrelConnection . TransportConnection . LocalEndPoint ) ;
77
+ Assert . Equal ( newLocalEndPoint , endpointFeature . LocalEndPoint ) ;
78
+ }
79
+
80
+ [ Fact ]
81
+ public void IConnectionEndPointFeature_AllowsSettingRemoteEndPoint ( )
82
+ {
83
+ var serviceContext = new TestServiceContext ( ) ;
84
+ var connection = new Mock < DefaultConnectionContext > { CallBase = true } . Object ;
85
+ var transportConnectionManager = new TransportConnectionManager ( serviceContext . ConnectionManager ) ;
86
+ var kestrelConnection = CreateKestrelConnection ( serviceContext , connection , transportConnectionManager ) ;
87
+ var newRemoteEndPoint = new IPEndPoint ( IPAddress . Parse ( "10.0.0.1" ) , 12345 ) ;
88
+
89
+ var endpointFeature = kestrelConnection . TransportConnection . Features . Get < IConnectionEndPointFeature > ( ) ;
90
+ endpointFeature . RemoteEndPoint = newRemoteEndPoint ;
91
+
92
+ Assert . Equal ( newRemoteEndPoint , kestrelConnection . TransportConnection . RemoteEndPoint ) ;
93
+ Assert . Equal ( newRemoteEndPoint , endpointFeature . RemoteEndPoint ) ;
94
+ }
95
+
96
+ private static KestrelConnection < ConnectionContext > CreateKestrelConnection ( TestServiceContext serviceContext , DefaultConnectionContext connection , TransportConnectionManager transportConnectionManager , Func < ConnectionContext , Task > connectionDelegate = null )
97
+ {
98
+ connectionDelegate ??= _ => Task . CompletedTask ;
99
+
100
+ return new KestrelConnection < ConnectionContext > (
101
+ id : 0 , serviceContext , transportConnectionManager , connectionDelegate , connection , serviceContext . Log , TestContextFactory . CreateMetricsContext ( connection ) ) ;
102
+ }
103
+ }
0 commit comments