Skip to content

Commit

Permalink
Simplified tests, added lock around possible concurrent list access, …
Browse files Browse the repository at this point in the history
…added nuspec file
  • Loading branch information
paulmorrishill committed Jan 13, 2020
1 parent 5e9f8e6 commit 6cd9753
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
17 changes: 9 additions & 8 deletions FluentSim/FluentSimulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ public class FluentSimulator
private string Address;
private List<FluentConfigurator> ConfiguredRoutes = new List<FluentConfigurator>();
private HttpListener HttpListener;
private List<Exception> ListenerExceptions = new List<Exception>();
private readonly List<Exception> ListenerExceptions = new List<Exception>();
public object ListenerExceptionsLock = new object();

private JsonSerializerSettings JsonSerializer;
public List<ReceivedRequest> IncomingRequests = new List<ReceivedRequest>();
public IReadOnlyList<ReceivedRequest> ReceivedRequests => IncomingRequests.AsReadOnly();
private bool CorsEnabled { get; set; }

public FluentSimulator(string address)
{
Address = address;
Expand All @@ -33,7 +35,6 @@ public FluentSimulator(string address, JsonSerializerSettings serializer)
JsonSerializer = serializer;
}


public void Start()
{
HttpListener = new HttpListener();
Expand All @@ -50,7 +51,8 @@ private void ProcessRequest(IAsyncResult ar)
}
catch (Exception e)
{
ListenerExceptions.Add(e);
lock (ListenerExceptionsLock)
ListenerExceptions.Add(e);
}
}

Expand Down Expand Up @@ -149,8 +151,9 @@ private RouteConfigurer InitialiseRoute(string path, HttpVerb verb)

public void Stop()
{
if (ListenerExceptions.Any())
throw new SimulatorException(ListenerExceptions);
lock(ListenerExceptionsLock)
if (ListenerExceptions.Any())
throw new SimulatorException(ListenerExceptions);
HttpListener.Stop();
}

Expand Down Expand Up @@ -184,11 +187,9 @@ public RouteConfigurer Put(string routePath)
return InitialiseRoute(routePath, HttpVerb.Put);
}


public void EnableCors()
{
CorsEnabled = true;
}

}
}
19 changes: 19 additions & 0 deletions FluentSim/FluentSimulator.nuspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<!-- Required elements-->
<id>FluentSimulator</id>
<version>$version$</version>
<description>FluentSimulator is a .Net HTTP API simulator library that is extremely versitile and unassuming.</description>
<authors>Paul Morris-Hill</authors>
<projectUrl>https://github.com/paulmorrishill/FluentSimulator</projectUrl>
<licenseUrl>https://github.com/paulmorrishill/FluentSimulator/blob/master/LICENSE</licenseUrl>
<dependencies>
<dependency id="Newtonsoft.Json" version="10.0.2" />
</dependencies>
</metadata>
<files>
<file src="bin\Release\FluentSim.dll" target="lib\FluentSim.dll"></file>
<file src="bin\Release\FluentSim.pdb" target="lib\FluentSim.pdb"></file>
</files>
</package>
12 changes: 6 additions & 6 deletions FluentSimTests/FluentSimTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -401,7 +402,7 @@ public void SequentialSetupsOverwriteThePreviousSetups()
}

[Test]
public async Task CanMakeGetRequestWithQueryString()
public void CanMakeGetRequestWithQueryString()
{
var queryString = "/test?key=value";
Sim.Get("/test").WithParameter("key", "value").Responds("OK");
Expand All @@ -410,7 +411,7 @@ public async Task CanMakeGetRequestWithQueryString()
}

[Test]
public async Task CanMakeGetRequestWithMultipleQueryStringsOutOfOrder()
public void CanMakeGetRequestWithMultipleQueryStringsOutOfOrder()
{
var queryString = "/test?key=value&key1=value1&key2=value2";
Sim.Get("/test")
Expand All @@ -423,7 +424,7 @@ public async Task CanMakeGetRequestWithMultipleQueryStringsOutOfOrder()
}

[Test]
public async Task GivenTheQueryStringIsLongerThanWhatWasExpected_Fails()
public void GivenTheQueryStringIsLongerThanWhatWasExpected_Fails()
{
var queryString = "/test?key=value&key1=value1&key2=value2";
Sim.Get("/test")
Expand All @@ -435,7 +436,7 @@ public async Task GivenTheQueryStringIsLongerThanWhatWasExpected_Fails()
}

[Test]
public async Task GivenTheExpectedParametersAreLongerThanTheQueryString_Fails()
public void GivenTheExpectedParametersAreLongerThanTheQueryString_Fails()
{
var queryString = "/test?key=value";
Sim.Get("/test")
Expand All @@ -451,7 +452,7 @@ public async Task GivenTheExpectedParametersAreLongerThanTheQueryString_Fails()
[TestCase("value%20here", "value here")]
[TestCase("value+here", "value here")]
[TestCase("Some%25%5E%26*(value", "Some%^&*(value")]
public async Task GivenTheQueryParamsHaveUrlEncodedCharactersItComparesOnUrlDecodedValues(string encoded, string decoded)
public void GivenTheQueryParamsHaveUrlEncodedCharactersItComparesOnUrlDecodedValues(string encoded, string decoded)
{
var queryString = "/test?key=" + encoded;
Sim.Get("/test")
Expand All @@ -461,7 +462,6 @@ public async Task GivenTheQueryParamsHaveUrlEncodedCharactersItComparesOnUrlDeco
result.StatusCode.ShouldEqual(HttpStatusCode.OK);
}


private class AllFieldsReplacementConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
Expand Down

0 comments on commit 6cd9753

Please sign in to comment.