Skip to content

Commit e71a4c9

Browse files
committed
Update tests after connection refactoring.
1 parent a1e4e04 commit e71a4c9

7 files changed

+282
-24
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using Moq;
3+
using Renci.SshNet.Connection;
4+
using System;
5+
6+
namespace Renci.SshNet.Tests.Classes
7+
{
8+
[TestClass]
9+
public class ServiceFactoryTest_CreateConnector
10+
{
11+
private ServiceFactory _serviceFactory;
12+
private Mock<IConnectionInfo> _connectionInfoMock;
13+
14+
[TestInitialize]
15+
public void Setup()
16+
{
17+
_serviceFactory = new ServiceFactory();
18+
_connectionInfoMock = new Mock<IConnectionInfo>(MockBehavior.Strict);
19+
}
20+
21+
[TestMethod]
22+
public void ConnectionInfoIsNull()
23+
{
24+
const IConnectionInfo connectionInfo = null;
25+
26+
try
27+
{
28+
_serviceFactory.CreateConnector(connectionInfo);
29+
Assert.Fail();
30+
}
31+
catch (ArgumentNullException ex)
32+
{
33+
Assert.IsNull(ex.InnerException);
34+
Assert.AreEqual("connectionInfo", ex.ParamName);
35+
}
36+
}
37+
38+
[TestMethod]
39+
public void ProxyType_Http()
40+
{
41+
_connectionInfoMock.Setup(p => p.ProxyType).Returns(ProxyTypes.Http);
42+
43+
var actual = _serviceFactory.CreateConnector(_connectionInfoMock.Object);
44+
45+
Assert.IsNotNull(actual);
46+
Assert.AreEqual(typeof(HttpConnector), actual.GetType());
47+
48+
_connectionInfoMock.Verify(p => p.ProxyType, Times.Once);
49+
}
50+
51+
[TestMethod]
52+
public void ProxyType_None()
53+
{
54+
_connectionInfoMock.Setup(p => p.ProxyType).Returns(ProxyTypes.None);
55+
56+
var actual = _serviceFactory.CreateConnector(_connectionInfoMock.Object);
57+
58+
Assert.IsNotNull(actual);
59+
Assert.AreEqual(typeof(DirectConnector), actual.GetType());
60+
61+
_connectionInfoMock.Verify(p => p.ProxyType, Times.Once);
62+
}
63+
64+
[TestMethod]
65+
public void ProxyType_Socks4()
66+
{
67+
_connectionInfoMock.Setup(p => p.ProxyType).Returns(ProxyTypes.Socks4);
68+
69+
var actual = _serviceFactory.CreateConnector(_connectionInfoMock.Object);
70+
71+
Assert.IsNotNull(actual);
72+
Assert.AreEqual(typeof(Socks4Connector), actual.GetType());
73+
74+
_connectionInfoMock.Verify(p => p.ProxyType, Times.Once);
75+
}
76+
77+
[TestMethod]
78+
public void ProxyType_Socks5()
79+
{
80+
_connectionInfoMock.Setup(p => p.ProxyType).Returns(ProxyTypes.Socks5);
81+
82+
var actual = _serviceFactory.CreateConnector(_connectionInfoMock.Object);
83+
84+
Assert.IsNotNull(actual);
85+
Assert.AreEqual(typeof(Socks5Connector), actual.GetType());
86+
87+
_connectionInfoMock.Verify(p => p.ProxyType, Times.Once);
88+
}
89+
}
90+
}

src/Renci.SshNet.Tests/Classes/SessionTest.HttpProxy.cs

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.VisualStudio.TestTools.UnitTesting;
66
using Renci.SshNet.Common;
77
using Renci.SshNet.Tests.Common;
8+
using Renci.SshNet.Connection;
89

910
namespace Renci.SshNet.Tests.Classes
1011
{
@@ -21,8 +22,15 @@ public void ConnectShouldThrowProxyExceptionWhenHttpProxyResponseDoesNotContainS
2122
proxyStub.Responses.Add(Encoding.ASCII.GetBytes("Whatever\r\n"));
2223
proxyStub.Start();
2324

24-
using (var session = new Session(CreateConnectionInfoWithProxy(proxyEndPoint, serverEndPoint, "anon"), _serviceFactoryMock.Object))
25+
var connectionInfo = CreateConnectionInfoWithHttpProxy(proxyEndPoint, serverEndPoint, "anon");
26+
27+
using (var session = new Session(connectionInfo, _serviceFactoryMock.Object))
2528
{
29+
_serviceFactoryMock.Setup(p => p.CreateConnector(connectionInfo))
30+
.Returns(_connectorMock.Object);
31+
_connectorMock.Setup(p => p.Connect(connectionInfo))
32+
.Returns<IConnectionInfo>(c => new HttpConnector().Connect(c));
33+
2634
try
2735
{
2836
session.Connect();
@@ -48,8 +56,15 @@ public void ConnectShouldThrowProxyExceptionWhenHttpProxyReturnsHttpStatusOtherT
4856
proxyStub.Responses.Add(Encoding.ASCII.GetBytes("HTTP/1.0 501 Custom\r\n"));
4957
proxyStub.Start();
5058

51-
using (var session = new Session(CreateConnectionInfoWithProxy(proxyEndPoint, serverEndPoint, "anon"), _serviceFactoryMock.Object))
59+
var connectionInfo = CreateConnectionInfoWithHttpProxy(proxyEndPoint, serverEndPoint, "anon");
60+
61+
using (var session = new Session(connectionInfo, _serviceFactoryMock.Object))
5262
{
63+
_serviceFactoryMock.Setup(p => p.CreateConnector(connectionInfo))
64+
.Returns(_connectorMock.Object);
65+
_connectorMock.Setup(p => p.Connect(connectionInfo))
66+
.Returns<IConnectionInfo>(c => new HttpConnector().Connect(c));
67+
5368
try
5469
{
5570
session.Connect();
@@ -78,8 +93,15 @@ public void ConnectShouldSkipHeadersWhenHttpProxyReturnsHttpStatus200()
7893
proxyStub.Responses.Add(Encoding.ASCII.GetBytes("SSH-666-SshStub"));
7994
proxyStub.Start();
8095

81-
using (var session = new Session(CreateConnectionInfoWithProxy(proxyEndPoint, serverEndPoint, "anon"), _serviceFactoryMock.Object))
96+
var connectionInfo = CreateConnectionInfoWithHttpProxy(proxyEndPoint, serverEndPoint, "anon");
97+
98+
using (var session = new Session(connectionInfo, _serviceFactoryMock.Object))
8299
{
100+
_serviceFactoryMock.Setup(p => p.CreateConnector(connectionInfo))
101+
.Returns(_connectorMock.Object);
102+
_connectorMock.Setup(p => p.Connect(connectionInfo))
103+
.Returns<IConnectionInfo>(c => new HttpConnector().Connect(c));
104+
83105
try
84106
{
85107
session.Connect();
@@ -110,8 +132,15 @@ public void ConnectShouldSkipContentWhenHttpProxyReturnsHttpStatus200()
110132
proxyStub.Responses.Add(Encoding.ASCII.GetBytes("SSH-666-SshStub"));
111133
proxyStub.Start();
112134

113-
using (var session = new Session(CreateConnectionInfoWithProxy(proxyEndPoint, serverEndPoint, "anon"), _serviceFactoryMock.Object))
135+
var connectionInfo = CreateConnectionInfoWithHttpProxy(proxyEndPoint, serverEndPoint, "anon");
136+
137+
using (var session = new Session(connectionInfo, _serviceFactoryMock.Object))
114138
{
139+
_serviceFactoryMock.Setup(p => p.CreateConnector(connectionInfo))
140+
.Returns(_connectorMock.Object);
141+
_connectorMock.Setup(p => p.Connect(connectionInfo))
142+
.Returns<IConnectionInfo>(c => new HttpConnector().Connect(c));
143+
115144
try
116145
{
117146
session.Connect();
@@ -137,8 +166,15 @@ public void ConnectShouldWriteConnectMethodToHttpProxy()
137166
proxyStub.Responses.Add(Encoding.ASCII.GetBytes("HTTP/1.0 501 Custom\r\n"));
138167
proxyStub.Start();
139168

140-
using (var session = new Session(CreateConnectionInfoWithProxy(proxyEndPoint, serverEndPoint, "anon"), _serviceFactoryMock.Object))
169+
var connectionInfo = CreateConnectionInfoWithHttpProxy(proxyEndPoint, serverEndPoint, "anon");
170+
171+
using (var session = new Session(connectionInfo, _serviceFactoryMock.Object))
141172
{
173+
_serviceFactoryMock.Setup(p => p.CreateConnector(connectionInfo))
174+
.Returns(_connectorMock.Object);
175+
_connectorMock.Setup(p => p.Connect(connectionInfo))
176+
.Returns<IConnectionInfo>(c => new HttpConnector().Connect(c));
177+
142178
try
143179
{
144180
session.Connect();
@@ -164,9 +200,15 @@ public void ConnectShouldWriteProxyAuthorizationToHttpProxyWhenProxyUserNameIsNo
164200
proxyStub.Responses.Add(Encoding.ASCII.GetBytes("HTTP/1.0 501 Custom\r\n"));
165201
proxyStub.Start();
166202

167-
var connectionInfo = CreateConnectionInfoWithProxy(proxyEndPoint, serverEndPoint, "anon");
203+
var connectionInfo = CreateConnectionInfoWithHttpProxy(proxyEndPoint, serverEndPoint, "anon");
204+
168205
using (var session = new Session(connectionInfo, _serviceFactoryMock.Object))
169206
{
207+
_serviceFactoryMock.Setup(p => p.CreateConnector(connectionInfo))
208+
.Returns(_connectorMock.Object);
209+
_connectorMock.Setup(p => p.Connect(connectionInfo))
210+
.Returns<IConnectionInfo>(c => new HttpConnector().Connect(c));
211+
170212
try
171213
{
172214
session.Connect();
@@ -193,9 +235,15 @@ public void ConnectShouldNotWriteProxyAuthorizationToHttpProxyWhenProxyUserNameI
193235
proxyStub.Responses.Add(Encoding.ASCII.GetBytes("HTTP/1.0 501 Custom\r\n"));
194236
proxyStub.Start();
195237

196-
var connectionInfo = CreateConnectionInfoWithProxy(proxyEndPoint, serverEndPoint, string.Empty);
238+
var connectionInfo = CreateConnectionInfoWithHttpProxy(proxyEndPoint, serverEndPoint, string.Empty);
239+
197240
using (var session = new Session(connectionInfo, _serviceFactoryMock.Object))
198241
{
242+
_serviceFactoryMock.Setup(p => p.CreateConnector(connectionInfo))
243+
.Returns(_connectorMock.Object);
244+
_connectorMock.Setup(p => p.Connect(connectionInfo))
245+
.Returns<IConnectionInfo>(c => new HttpConnector().Connect(c));
246+
199247
try
200248
{
201249
session.Connect();
@@ -221,9 +269,14 @@ public void ConnectShouldNotWriteProxyAuthorizationToHttpProxyWhenProxyUserNameI
221269
proxyStub.Responses.Add(Encoding.ASCII.GetBytes("HTTP/1.0 501 Custom\r\n"));
222270
proxyStub.Start();
223271

224-
var connectionInfo = CreateConnectionInfoWithProxy(proxyEndPoint, serverEndPoint, null);
272+
var connectionInfo = CreateConnectionInfoWithHttpProxy(proxyEndPoint, serverEndPoint, null);
225273
using (var session = new Session(connectionInfo, _serviceFactoryMock.Object))
226274
{
275+
_serviceFactoryMock.Setup(p => p.CreateConnector(connectionInfo))
276+
.Returns(_connectorMock.Object);
277+
_connectorMock.Setup(p => p.Connect(connectionInfo))
278+
.Returns<IConnectionInfo>(c => new HttpConnector().Connect(c));
279+
227280
try
228281
{
229282
session.Connect();
@@ -238,7 +291,7 @@ public void ConnectShouldNotWriteProxyAuthorizationToHttpProxyWhenProxyUserNameI
238291
}
239292
}
240293

241-
private static ConnectionInfo CreateConnectionInfoWithProxy(IPEndPoint proxyEndPoint, IPEndPoint serverEndPoint, string proxyUserName)
294+
private static ConnectionInfo CreateConnectionInfoWithHttpProxy(IPEndPoint proxyEndPoint, IPEndPoint serverEndPoint, string proxyUserName)
242295
{
243296
return new ConnectionInfo(
244297
serverEndPoint.Address.ToString(),

0 commit comments

Comments
 (0)