Skip to content

Commit

Permalink
Fix issues and add docs for dynamic handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Morris-Hill committed Oct 18, 2023
1 parent 534f1aa commit fadfdf2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
22 changes: 9 additions & 13 deletions FluentSim/FluentConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@
namespace FluentSim
{
[DebuggerDisplay("{Description}")]
public class DefinedResponse
internal class DefinedResponse
{
public string Output = "";
private string Description { get; set; }
public void AddDescriptionPart(string part) => Description += " " + part;
public byte[] BinaryOutput = null;
public bool ShouldImmediatelyDisconnect = false;
public List<Action<HttpListenerContext>> ResponseModifiers = new List<Action<HttpListenerContext>>();

internal string GetBody()
public Func<ReceivedRequest, string> HandlerFunction { get; set; }

internal string GetBody(ReceivedRequest request)
{
if (HandlerFunction != null)
return HandlerFunction(request);
return Output;
}

Expand All @@ -43,7 +46,6 @@ public class FluentConfigurator : RouteConfigurer, RouteSequenceConfigurer
private DefinedResponse CurrentResponse = new DefinedResponse();
private int NextResponseIndex = 0;
private List<DefinedResponse> Responses;
private Func<ReceivedRequest, string> HandlerFunction { get; set; }

public FluentConfigurator(string path, HttpVerb get, JsonSerializerSettings jsonConverter)
{
Expand All @@ -66,7 +68,8 @@ public void AddReceivedRequest(ReceivedRequest request)

public RouteConfigurer IsHandledBy(Func<ReceivedRequest, string> generateOutput)
{
HandlerFunction = generateOutput;
CurrentResponse.AddDescriptionPart("Handled by function");
CurrentResponse.HandlerFunction = generateOutput;
return this;
}

Expand Down Expand Up @@ -134,13 +137,6 @@ public RouteConfigurer MatchingRegex()
return this;
}

internal string GetBody(ReceivedRequest request)
{
if (HandlerFunction != null)
return HandlerFunction(request);
return Output;
}

internal bool DoesRouteMatch(HttpListenerRequest contextRequest)
{
if (!Path.EndsWith("/") && !IsRegex) Path += "/";
Expand Down Expand Up @@ -232,7 +228,7 @@ public RouteHistory(IReadOnlyList<ReceivedRequest> requests)
public IReadOnlyList<ReceivedRequest> ReceivedRequests { get; }
}

public DefinedResponse GetNextDefinedResponse()
internal DefinedResponse GetNextDefinedResponse()
{
if(NextResponseIndex >= Responses.Count)
return Responses[Responses.Count - 1];
Expand Down
24 changes: 12 additions & 12 deletions FluentSimTests/FluentSimTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ public void CanCreateASequenceOfResponses()
.ThenResponds()
.WithCode(200);

MakePostRequest("/test", "").StatusCode.ShouldEqual(HttpStatusCode.BadRequest);
MakePostRequest("/test", "").StatusCode.ShouldEqual(HttpStatusCode.InternalServerError);
MakePostRequest("/test", "").StatusCode.ShouldEqual(HttpStatusCode.OK);
MakePostRequest("/test", "").StatusCode.ShouldBe(HttpStatusCode.BadRequest);
MakePostRequest("/test", "").StatusCode.ShouldBe(HttpStatusCode.InternalServerError);
MakePostRequest("/test", "").StatusCode.ShouldBe(HttpStatusCode.OK);
}

[Test]
Expand All @@ -199,10 +199,10 @@ public void GivenASequenceOfEventsItReusesTheLastResponse()
.ThenResponds()
.WithCode(500);

MakePostRequest("/test", "").StatusCode.ShouldEqual(HttpStatusCode.BadRequest);
MakePostRequest("/test", "").StatusCode.ShouldEqual(HttpStatusCode.InternalServerError);
MakePostRequest("/test", "").StatusCode.ShouldEqual(HttpStatusCode.InternalServerError);
MakePostRequest("/test", "").StatusCode.ShouldEqual(HttpStatusCode.InternalServerError);
MakePostRequest("/test", "").StatusCode.ShouldBe(HttpStatusCode.BadRequest);
MakePostRequest("/test", "").StatusCode.ShouldBe(HttpStatusCode.InternalServerError);
MakePostRequest("/test", "").StatusCode.ShouldBe(HttpStatusCode.InternalServerError);
MakePostRequest("/test", "").StatusCode.ShouldBe(HttpStatusCode.InternalServerError);
}

[Test]
Expand All @@ -212,8 +212,8 @@ public void CanOutputASequenceOfDifferentBodies()
.Responds("first")
.ThenResponds("second");

MakePostRequest("/test", "").Content.ShouldEqual("first");
MakePostRequest("/test", "").Content.ShouldEqual("second");
MakePostRequest("/test", "").Content.ShouldBe("first");
MakePostRequest("/test", "").Content.ShouldBe("second");
}

[Test]
Expand All @@ -223,9 +223,9 @@ public void CanResetRouteSequenceCount()
.Responds("first")
.ThenResponds("second");

MakePostRequest("/test", "").Content.ShouldEqual("first");
MakePostRequest("/test", "").Content.ShouldBe("first");
route.ResetCurrentResponseIndex();
MakePostRequest("/test", "").Content.ShouldEqual("first");
MakePostRequest("/test", "").Content.ShouldBe("first");
}

[Test]
Expand Down Expand Up @@ -256,7 +256,7 @@ public void CanImmediatelyAbortConnection()
{
Sim.Get("/test").ImmediatelyAborts();
var resp = MakeGetRequest("/test", 100);
resp.StatusCode.ShouldEqual((HttpStatusCode)0);
resp.StatusCode.ShouldBe((HttpStatusCode)0);
}

[Test]
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,21 @@ simulator.Post("/authenticate")
.WithCookie(new Cookie("Token", "ABCDEF"));
```

## Dynamic handlers
You can also use a dynamic handler to generate the response. This is useful for calculating responses based on the request data.

```c#
var counter = 0;
// Request is of type ReceivedRequest see below for more info
Sim.Post("/post").IsHandledBy(request => $"Counter: {counter++}");

var resp1 = MakePostRequest("/post", "BODY");
var resp2 = MakePostRequest("/post", "BODY");
resp1.Content.ShouldBe("Counter: 0");
resp2.Content.ShouldBe("Counter: 1");
```


## Slow responses
You can test how your code handles slow server replies.

Expand Down

0 comments on commit fadfdf2

Please sign in to comment.