Skip to content

Commit

Permalink
Add in the ability to abort connections
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Morris-Hill committed Oct 18, 2023
1 parent 6cd9753 commit d0e5a4c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
6 changes: 6 additions & 0 deletions FluentSim/FluentConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class FluentConfigurator : RouteConfigurer
private List<ReceivedRequest> ReceivedRequests = new List<ReceivedRequest>();
public byte[] BinaryOutput = null;
public Dictionary<string, string> QueryParameters = new Dictionary<string, string>();
public bool ShouldImmediatelyDisconnect = false;

public FluentConfigurator(string path, HttpVerb get, JsonSerializerSettings jsonConverter)
{
Expand Down Expand Up @@ -160,6 +161,11 @@ public IRouteHistory History()
return new RouteHistory(ReceivedRequests.AsReadOnly());
}

public void ImmediatelyAborts()
{
ShouldImmediatelyDisconnect = true;
}

private class RouteHistory : IRouteHistory
{
public RouteHistory(IReadOnlyList<ReceivedRequest> requests)
Expand Down
6 changes: 6 additions & 0 deletions FluentSim/FluentSimulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ private void TryToProcessRequest(IAsyncResult ar)
return;
}

if (matchingRoute.ShouldImmediatelyDisconnect)
{
response.Abort();
return;
}

matchingRoute.AddReceivedRequest(receivedRequest);
matchingRoute.WaitUntilReadyToRespond();

Expand Down
1 change: 1 addition & 0 deletions FluentSim/RouteConfigurer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ public interface RouteConfigurer
RouteConfigurer Resume();
RouteConfigurer WithCookie(Cookie cookie);
IRouteHistory History();
void ImmediatelyAborts();
}
}
16 changes: 14 additions & 2 deletions FluentSimTests/FluentSimTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,13 @@ public void CanMakeGetRequest()
resp.StatusCode.ShouldEqual(HttpStatusCode.OK);
}

private static IRestResponse MakeGetRequest(string path)
private static IRestResponse MakeGetRequest(string path, int timeout=50000)
{
var request = new RestRequest(path, Method.GET);
var client = new RestClient(BaseAddress);
var client = new RestClient(BaseAddress)
{
Timeout = timeout
};
var resp = client.Execute(request);
return resp;
}
Expand Down Expand Up @@ -197,6 +200,15 @@ public void CanRespondWithCookies()
cookie.Value.ShouldEqual("VALTEST");
}

[Test]
public void CanImmediatelyAbortConnection()
{

Sim.Get("/test").ImmediatelyAborts();
var resp = MakeGetRequest("/test", 100);
resp.StatusCode.ShouldEqual((HttpStatusCode)0);
}

[Test]
public void CanReturnSerialisedObjects()
{
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ You can test how your code handles slow server replies.
simulator.Get("/my/route").Responds("Final output").Delay(TimeSpan.FromSeconds(30));
```

## Aborted connections
You can tell the API to abort the connection (interally uses `HttpListenerResponse.Abort()`) to simulate a network disconnection. Note: if using restsharp this will cause a response timeout, so if you have a long timeout set it may appear to hang. The RestSharp status code will be `0`.
```c#
simulator.Post("/authenticate").ImmediatelyAborts();
```

## Indefinitely suspend responses at runtime
You can check that your webpage correctly displays loading messages or spinners.

Expand Down

0 comments on commit d0e5a4c

Please sign in to comment.