How to simulate disconnect? #177

Closed
opened 2025-12-29 08:23:28 +01:00 by adam · 2 comments
Owner

Originally created by @AlexanderDorofeev on GitHub (Jun 6, 2019).

Hi!
I'm trying to figure out how to close connection in the middle of a request.

server.Given(Request.Create().WithPath("/unstable").UsingGet())
                   .RespondWith(Response.Create()
                    .WithDelay(TimeSpan.FromSeconds(11))
                    .WithStatusCode(HttpStatusCode.OK));
var task = Task.Delay(TimeSpan.FromSeconds(3))
                    .ContinueWith(t =>
                    {
                      server.Dispose();
                    });

Request with such configuration will be successful. Am I missing something that will cause a connection to terminate?
Thank you

Originally created by @AlexanderDorofeev on GitHub (Jun 6, 2019). Hi! I'm trying to figure out how to close connection in the middle of a request. ``` var server = FluentMockServer.Start(); server.Given(Request.Create().WithPath("/unstable").UsingGet()) .RespondWith(Response.Create() .WithDelay(TimeSpan.FromSeconds(11)) .WithStatusCode(HttpStatusCode.OK)); var task = Task.Delay(TimeSpan.FromSeconds(3)) .ContinueWith(t => { server.Dispose(); }); ``` Request with such configuration will be successful. Am I missing something that will cause a connection to terminate? Thank you
adam added the question label 2025-12-29 08:23:28 +01:00
adam closed this issue 2025-12-29 08:23:28 +01:00
Author
Owner

@StefH commented on GitHub (Jun 7, 2019):

You could maybe also throw an exception?

server
                .Given(Request.Create().WithPath("/myendpoint").UsingAnyMethod())
                .RespondWith(Response.Create()
                    .WithStatusCode(500)
                    .WithBody(requestMessage =>
                    {
                        throw new Exception();
                    })
);
@StefH commented on GitHub (Jun 7, 2019): You could maybe also throw an exception? ``` c# server .Given(Request.Create().WithPath("/myendpoint").UsingAnyMethod()) .RespondWith(Response.Create() .WithStatusCode(500) .WithBody(requestMessage => { throw new Exception(); }) ); ```
Author
Owner

@AlexanderDorofeev commented on GitHub (Jun 14, 2019):

Thank you. This worked. But I've discovered, that some retries are done by-default in HttpClient. So, if sombody like me, is testing against custom retry policies, then there should be a delay to exhaust standard retry. This delay works for my case

server.Given(Request.Create().WithPath("/unstable").UsingGet())
         .RespondWith(Response.Create()
          .WithDelay(TimeSpan.FromSeconds(1))
           .WithBody((r)=>throw new ApplicationException()));
                    
 var task = Task.Delay(TimeSpan.FromSeconds(5))
              .ContinueWith(t =>
              {
                   server.Dispose();
                   server = FluentMockServer.Start(urls);
                   server.Given(Request.Create().WithPath("/unstable").UsingGet())
                    .RespondWith(Response.Create().WithStatusCode(HttpStatusCode.OK));
                 });
@AlexanderDorofeev commented on GitHub (Jun 14, 2019): Thank you. This worked. But I've discovered, that some retries are done by-default in HttpClient. So, if sombody like me, is testing against custom retry policies, then there should be a delay to exhaust standard retry. This delay works for my case ``` server.Given(Request.Create().WithPath("/unstable").UsingGet()) .RespondWith(Response.Create() .WithDelay(TimeSpan.FromSeconds(1)) .WithBody((r)=>throw new ApplicationException())); var task = Task.Delay(TimeSpan.FromSeconds(5)) .ContinueWith(t => { server.Dispose(); server = FluentMockServer.Start(urls); server.Given(Request.Create().WithPath("/unstable").UsingGet()) .RespondWith(Response.Create().WithStatusCode(HttpStatusCode.OK)); }); ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#177