Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ PublishProfiles/
*.pidb
*.userprefs
*DS_Store
*.ncrunchsolution
*.ncrunchsolution
.idea
210 changes: 112 additions & 98 deletions Toxiproxy.Net.Tests/ClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@
namespace Toxiproxy.Net.Tests
{
[Collection("Integration")]
public class ClientTests : ToxiproxyTestsBase
public class ClientTests : IDisposable
{
private readonly TestFixture _fixture;

public ClientTests()
{
_fixture = new TestFixture();
}

[Fact]
public void ErrorsAreThrownCorrectly()
{
var client = _connection.Client();
var client = _fixture.Client();

Assert.Throws<ToxiProxiException>(() =>
{
Expand All @@ -23,69 +30,69 @@ public void ErrorsAreThrownCorrectly()
public void CanFindNamedProxy()
{
// Create a proxy and add the proxy to the client
var client = _connection.Client();
client.Add(ProxyOne);
var client = _fixture.Client();
client.Add(TestProxy.One);

// Retrieve the proxy
var proxy = client.FindProxy("one");

// Check if it the corerct one
// Check if it the correct one
Assert.NotNull(proxy);
Assert.Equal(proxy.Name, ProxyOne.Name);
Assert.Equal(proxy.Upstream, ProxyOne.Upstream);
Assert.Equal(proxy.Name, TestProxy.One.Name);
Assert.Equal(proxy.Upstream, TestProxy.One.Upstream);
}

[Fact]
public void CanFindAllProxies()
{
// Create two proxies and add them to the client
var client = _connection.Client();
client.Add(ProxyOne);
client.Add(ProxyTwo);
var client = _fixture.Client();
client.Add(TestProxy.One);
client.Add(TestProxy.Two);

// Retrieve all the proxies
var all = client.All();

// Check if there are two proxies
Assert.Equal(2, all.Keys.Count);
// Check if contains the correct proxies
var containsProxyOne = all.Keys.Contains(ProxyOne.Name);
var containsProxyOne = all.Keys.Contains(TestProxy.One.Name);
Assert.True(containsProxyOne);
var containsProxyTwo = all.Keys.Contains(ProxyTwo.Name);
var containsProxyTwo = all.Keys.Contains(TestProxy.Two.Name);
Assert.True(containsProxyOne);
}

[Fact]
public void CanDeleteProxy()
{
// Add three proxies
var client = _connection.Client();
client.Add(ProxyOne);
client.Add(ProxyTwo);
client.Add(ProxyThree);
var client = _fixture.Client();
client.Add(TestProxy.One);
client.Add(TestProxy.Two);
client.Add(TestProxy.Three);

// Delete two proxies
client.Delete(ProxyOne);
client.Delete(ProxyTwo.Name);
client.Delete(TestProxy.One);
client.Delete(TestProxy.Two.Name);

// The client should contain only a proxy
var all = client.All();
Assert.Equal(1, all.Keys.Count);

// The single proxy in the collection should be the 3th proxy
var containsProxyThree = all.Keys.Contains(ProxyThree.Name);
var containsProxyThree = all.Keys.Contains(TestProxy.Three.Name);
Assert.True(containsProxyThree);
}

[Fact]
public void CanUpdateProxy()
{
// Add a proxy
var client = _connection.Client();
client.Add(ProxyOne);
var client = _fixture.Client();
client.Add(TestProxy.One);

// Retrieve the proxy and update the proxy
var proxyToUpdate = client.FindProxy(ProxyOne.Name);
var proxyToUpdate = client.FindProxy(TestProxy.One.Name);
proxyToUpdate.Enabled = false;
proxyToUpdate.Listen = "localhost:55555";
proxyToUpdate.Upstream = "google.com";
Expand All @@ -103,48 +110,48 @@ public void CanUpdateProxy()
public void ResetWorks()
{
// Add a disabled proxy
var client = _connection.Client();
client.Add(ProxyOne);
var client = _fixture.Client();
client.Add(TestProxy.One);

// Reset
client.Reset();

// Retrive the proxy
var proxyCopy = client.FindProxy(ProxyOne.Name);
// Retrieve the proxy
var proxyCopy = client.FindProxy(TestProxy.One.Name);

// The proxy should be enabled
Assert.Equal(proxyCopy.Enabled, true);
Assert.True(proxyCopy.Enabled);
}

[Fact]
public void CanNotAddANullProxy()
{
var client = _connection.Client();
var client = _fixture.Client();

Assert.Throws<ArgumentNullException>(() => client.Add(null));
}

[Fact]
public void CreateANewProxyShouldWork()
{
var client = _connection.Client();
var newProxy = client.Add(ProxyOne);
var client = _fixture.Client();
var newProxy = client.Add(TestProxy.One);

Assert.Equal(ProxyOne.Name, newProxy.Name);
Assert.Equal(ProxyOne.Enabled, newProxy.Enabled);
Assert.Equal(ProxyOne.Listen, newProxy.Listen);
Assert.Equal(ProxyOne.Upstream, newProxy.Upstream);
Assert.Equal(TestProxy.One.Name, newProxy.Name);
Assert.Equal(TestProxy.One.Enabled, newProxy.Enabled);
Assert.Equal(TestProxy.One.Listen, newProxy.Listen);
Assert.Equal(TestProxy.One.Upstream, newProxy.Upstream);
}

[Fact]
public void DeletingAProxyMoreThanOnceShouldThrowException()
{
// Add a proxy and check it exists
var client = _connection.Client();
client.Add(ProxyOne);
var proxy = client.FindProxy(ProxyOne.Name);
var client = _fixture.Client();
client.Add(TestProxy.One);
var proxy = client.FindProxy(TestProxy.One.Name);

// deleting is not idemnepotent and should throw exception
// deleting is not idempotent and should throw exception
proxy.Delete();
var exception = Assert.Throws<ToxiProxiException>(() => proxy.Delete());
Assert.Equal("Not found", exception.Message);
Expand All @@ -154,72 +161,79 @@ public void DeletingAProxyMoreThanOnceShouldThrowException()
public void DeletingAProxyWorks()
{
// Add a proxy and check it exists
var client = _connection.Client();
client.Add(ProxyOne);
var proxy = client.FindProxy(ProxyOne.Name);
var client = _fixture.Client();
client.Add(TestProxy.One);
var proxy = client.FindProxy(TestProxy.One.Name);

// delete
proxy.Delete();

// check it doesn't exists
Assert.Throws<ToxiProxiException>(() =>
{
client.FindProxy(ProxyOne.Name);
client.FindProxy(TestProxy.One.Name);
});
}

[Fact]
public void AddToxic_NullFields() {
// Create a proxy and add the proxy to the client
var client = _connection.Client();
client.Add( ProxyOne );

// Retrieve the proxy
var proxy = client.FindProxy( "one" );
var latencyToxic = new LatencyToxic();
latencyToxic.Attributes.Latency = 1000;

proxy.Add( latencyToxic );
proxy.Update();

var toxics = proxy.GetAllToxics();
Assert.Equal( 1, toxics.Count() );
var toxic = toxics.First();

Assert.Equal( 1, toxic.Toxicity );
Assert.Equal( ToxicDirection.DownStream, toxic.Stream );

//default pattern is <type>_<stream>
Assert.Equal( "latency_downstream", toxic.Name );
}

[Fact]
public void AddToxic_NonNullFields() {
// Create a proxy and add the proxy to the client
var client = _connection.Client();
client.Add( ProxyOne );

// Retrieve the proxy
var proxy = client.FindProxy( "one" );

var latencyToxic = new LatencyToxic();
latencyToxic.Attributes.Latency = 1000;
latencyToxic.Stream = ToxicDirection.UpStream;
latencyToxic.Name = "testName";
latencyToxic.Toxicity = 0.5;

proxy.Add( latencyToxic );
proxy.Update();

var toxics = proxy.GetAllToxics();
Assert.Equal( 1, toxics.Count() );
var toxic = toxics.First();

Assert.Equal( 0.5, toxic.Toxicity );
Assert.Equal( ToxicDirection.UpStream, toxic.Stream );

//default pattern is <type>_<stream>
Assert.Equal( "testName", toxic.Name );
}
}
}

[Fact]
public void AddToxic_NullFields() {
// Create a proxy and add the proxy to the client
var client = _fixture.Client();
client.Add(TestProxy.One);

// Retrieve the proxy
var proxy = client.FindProxy( "one" );
var latencyToxic = new LatencyToxic
{
Attributes = {Latency = 1000}
};

proxy.Add( latencyToxic );
proxy.Update();

var toxics = proxy.GetAllToxics();
Assert.True(toxics.Count() == 1);
var toxic = toxics.First();

Assert.True(toxic.Toxicity == 1);
Assert.Equal( ToxicDirection.DownStream, toxic.Stream );

//default pattern is <type>_<stream>
Assert.Equal( "latency_downstream", toxic.Name );
}

[Fact]
public void AddToxic_NonNullFields() {
// Create a proxy and add the proxy to the client
var client = _fixture.Client();
client.Add(TestProxy.One);

// Retrieve the proxy
var proxy = client.FindProxy( "one" );

var latencyToxic = new LatencyToxic
{
Attributes = {Latency = 1000},
Stream = ToxicDirection.UpStream,
Name = "testName",
Toxicity = 0.5
};

proxy.Add( latencyToxic );
proxy.Update();

var toxics = proxy.GetAllToxics();
Assert.True(toxics.Count() == 1);
var toxic = toxics.First();

Assert.Equal( 0.5, toxic.Toxicity);
Assert.Equal( ToxicDirection.UpStream, toxic.Stream );

//default pattern is <type>_<stream>
Assert.Equal( "testName", toxic.Name );
}

public void Dispose()
=> _fixture?.Dispose();
}
}
30 changes: 17 additions & 13 deletions Toxiproxy.Net.Tests/ConnectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,43 @@
namespace Toxiproxy.Net.Tests
{
[Collection("Integration")]
public class ConnectionTests : ToxiproxyTestsBase
public class ConnectionTests : IDisposable
{
private readonly TestFixture _fixture;

public ConnectionTests()
{
_fixture = new TestFixture();
}

[Fact]
public void ErrorThrownIfHostIsNullOrEmpty()
{
Assert.Throws<ArgumentNullException>(() =>
{
var connection = new Connection("");

});
Assert.Throws<ArgumentNullException>(() => { _ = new Connection(""); });

Assert.Throws<ArgumentNullException>(() =>
{
var connection = new Connection(null);
});
Assert.Throws<ArgumentNullException>(() => { _ = new Connection(null); });
}

[Fact]
public void DisposeEnablesAndResetsAllProxies()
{
_fixture.Client();
var connection = new Connection(resetAllToxicsAndProxiesOnClose: true);

var client = connection.Client();
client.Add(ProxyOne);
client.Add(TestProxy.One);

var proxy = client.FindProxy(ProxyOne.Name);
var proxy = client.FindProxy(TestProxy.One.Name);
proxy.Enabled = false;
proxy.Update();

connection.Dispose();

var proxyCopy = client.FindProxy(ProxyOne.Name);
var proxyCopy = client.FindProxy(TestProxy.One.Name);
Assert.True(proxyCopy.Enabled);
}

public void Dispose()
=> _fixture?.Dispose();
}
}
Loading