Feature: Please provide support for Bad responses. #212

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

Originally created by @grzegorz-wolszczak on GitHub (Sep 10, 2019).

Hi.
I can see that WireMock-Net already supports delayed responses, but
it would be even more cool if WireMock-Net supported simulating bad responses as in
http://wiremock.org/docs/simulating-faults/

Originally created by @grzegorz-wolszczak on GitHub (Sep 10, 2019). Hi. I can see that WireMock-Net already supports delayed responses, but it would be even more cool if WireMock-Net supported simulating bad responses as in [http://wiremock.org/docs/simulating-faults/](http://wiremock.org/docs/simulating-faults/)
adam added the feature label 2025-12-29 08:24:01 +01:00
adam closed this issue 2025-12-29 08:24:01 +01:00
Author
Owner

@StefH commented on GitHub (Sep 16, 2019):

What kind of bad responses do you need?

@StefH commented on GitHub (Sep 16, 2019): What kind of bad responses do you need?
Author
Owner

@grzegorz-wolszczak commented on GitHub (Sep 18, 2019):

I was referring to the bad responses described as:

EMPTY_RESPONSE: Return a completely empty response.
MALFORMED_RESPONSE_CHUNK: Send an OK status header, then garbage, then close the connection.
RANDOM_DATA_THEN_CLOSE: Send garbage then close the connection.
CONNECTION_RESET_BY_PEER:

@grzegorz-wolszczak commented on GitHub (Sep 18, 2019): I was referring to the bad responses described as: EMPTY_RESPONSE: Return a completely empty response. MALFORMED_RESPONSE_CHUNK: Send an OK status header, then garbage, then close the connection. RANDOM_DATA_THEN_CLOSE: Send garbage then close the connection. CONNECTION_RESET_BY_PEER:
Author
Owner

@StefH commented on GitHub (Sep 23, 2019):

Something like:

server
    .Given(Request.Create().WithPath("/x")
    .RespondWith(Response.Create()
        .WithBody("OK")
        .WithFault(Faults.EMPTY_RESPONSE, 0.1)
    );

This will return an empty response 1 on 10 times.

This code will always return an empty response...

server
    .Given(Request.Create().WithPath("/x")
    .RespondWith(Response.Create()
        .WithFault(Faults.EMPTY_RESPONSE)
    );

Is this an idea?

@StefH commented on GitHub (Sep 23, 2019): Something like: ``` c# server .Given(Request.Create().WithPath("/x") .RespondWith(Response.Create() .WithBody("OK") .WithFault(Faults.EMPTY_RESPONSE, 0.1) ); ``` This will return an empty response 1 on 10 times. This code will always return an empty response... ``` c# server .Given(Request.Create().WithPath("/x") .RespondWith(Response.Create() .WithFault(Faults.EMPTY_RESPONSE) ); ``` Is this an idea?
Author
Owner

@grzegorz-wolszczak commented on GitHub (Sep 23, 2019):

Yea, that would be cool.
But the feature I would like the most (and might be most difficult to create) is to have response
CONNECTION_RESET_BY_PEER.

@grzegorz-wolszczak commented on GitHub (Sep 23, 2019): Yea, that would be cool. But the feature I would like the most (and might be most difficult to create) is to have response CONNECTION_RESET_BY_PEER.
Author
Owner

@StefH commented on GitHub (Sep 24, 2019):

About :
MALFORMED_RESPONSE_CHUNK: Send an OK status header, then garbage, then close the connection.

  • Sending 200 OK = easy
  • Garbage; just send random bytes = easy
  • then close the connection ; what should this do ? Is sending a 200 OK not enough to close a connection?
@StefH commented on GitHub (Sep 24, 2019): About : `MALFORMED_RESPONSE_CHUNK: Send an OK status header, then garbage, then close the connection.` - Sending 200 OK = easy - Garbage; just send random bytes = easy - then close the connection ; what should this do ? Is sending a 200 OK not enough to close a connection?
Author
Owner

@grzegorz-wolszczak commented on GitHub (Sep 25, 2019):

I agree with you that sending 200 OK and closing connection is enough.

@grzegorz-wolszczak commented on GitHub (Sep 25, 2019): I agree with you that sending 200 OK and closing connection is enough.
Author
Owner

@StefH commented on GitHub (Oct 11, 2019):

@grzegorz-wolszczak
A preview NuGet (from MygGet) --> WireMock.Net.1.0.32-ci-12002.nupkg can be tested.

@StefH commented on GitHub (Oct 11, 2019): @grzegorz-wolszczak A preview NuGet (from MygGet) --> `WireMock.Net.1.0.32-ci-12002.nupkg` can be tested.
Author
Owner

@StefH commented on GitHub (Oct 22, 2019):

@grzegorz-wolszczak Can you please test this preview and report back your findings?

@StefH commented on GitHub (Oct 22, 2019): @grzegorz-wolszczak Can you please test this preview and report back your findings?
Author
Owner

@grzegorz-wolszczak commented on GitHub (Oct 22, 2019):

Hi Stef
Sorry for not responding sooner.
I really couldn't find time for testing.
I will try to do as soon as possible.

@grzegorz-wolszczak commented on GitHub (Oct 22, 2019): Hi Stef Sorry for not responding sooner. I really couldn't find time for testing. I will try to do as soon as possible.
Author
Owner

@grzegorz-wolszczak commented on GitHub (Oct 23, 2019):

How should I test new library ? What new Api was introduced to handle cases like:
MALFORMED_RESPONSE_CHUNK
RANDOM_DATA_THEN_CLOSE
CONNECTION_RESET_BY_PEER

I was trying to find some methods on IResponseBuilder interface but I did not see anything that would map to the responses I've described in my feature request. I tried to grep your git commit history by the issue number (#343) but also could not find any. In which commit (or commits) did you introduce changes for this issue? I will look into it and figure out the rest.

@grzegorz-wolszczak commented on GitHub (Oct 23, 2019): How should I test new library ? What new Api was introduced to handle cases like: MALFORMED_RESPONSE_CHUNK RANDOM_DATA_THEN_CLOSE CONNECTION_RESET_BY_PEER I was trying to find some methods on IResponseBuilder interface but I did not see anything that would map to the responses I've described in my feature request. I tried to grep your git commit history by the issue number (#343) but also could not find any. In which commit (or commits) did you introduce changes for this issue? I will look into it and figure out the rest.
Author
Owner

@StefH commented on GitHub (Oct 23, 2019):

1]
This NuGet package on the MyGet feed should implement this fix:
https://www.myget.org/feed/wiremock-net/package/nuget/WireMock.Net/1.0.32-ci-12002

2]
Or you can clone this project and switch to branch https://github.com/WireMock-Net/WireMock.Net/tree/WithFault

3a]
New API is like:
https://github.com/WireMock-Net/WireMock.Net/blob/WithFault/test/WireMock.Net.Tests/ResponseBuilders/ResponseWithWithFaultTests.cs

3b]
Or via JSON admin interface :

{
    "Guid": "a51b78ac-1300-4125-aa97-d48953deef77",
    "Priority": 0,
    "Request": {
        "Path": {
            "Matchers": [
                {
                    "Name": "WildcardMatcher",
                    "Pattern": "/fault",
                    "IgnoreCase": false
                }
            ]
        },
        "Body": {}
    },
    "Response": {
        "StatusCode": 200,
        "BodyAsJson": {
            "x": "x"
        },
        "Fault": { "Type": "RANDOM_DATA_THEN_CLOSE", "Percentage": 0.999999 },
        "UseTransformer": false,
        "Headers": {
            "Content-Type": "application/json"
        }
    }
}
@StefH commented on GitHub (Oct 23, 2019): 1] This NuGet package on the MyGet feed should implement this fix: https://www.myget.org/feed/wiremock-net/package/nuget/WireMock.Net/1.0.32-ci-12002 2] Or you can clone this project and switch to branch https://github.com/WireMock-Net/WireMock.Net/tree/WithFault 3a] New API is like: https://github.com/WireMock-Net/WireMock.Net/blob/WithFault/test/WireMock.Net.Tests/ResponseBuilders/ResponseWithWithFaultTests.cs 3b] Or via JSON admin interface : ``` js { "Guid": "a51b78ac-1300-4125-aa97-d48953deef77", "Priority": 0, "Request": { "Path": { "Matchers": [ { "Name": "WildcardMatcher", "Pattern": "/fault", "IgnoreCase": false } ] }, "Body": {} }, "Response": { "StatusCode": 200, "BodyAsJson": { "x": "x" }, "Fault": { "Type": "RANDOM_DATA_THEN_CLOSE", "Percentage": 0.999999 }, "UseTransformer": false, "Headers": { "Content-Type": "application/json" } } } ```
Author
Owner

@grzegorz-wolszczak commented on GitHub (Oct 23, 2019):

Ok, now i See what the problem was, I have chosen wrong nuget version.

So I've tested
EMPTY_RESPONSE, and RANDOM_DATA_THEN_CLOSE and it works good (tested with 'curl')

but for FaultType.MALFORMED_RESPONSE_CHUNK

when request GET

I got:
Status code: 500Internal Server Error

and body

{
    "Status": "{\"ClassName\":\"System.NullReferenceException\",\"Message\":\"Object reference not set to an instance of an object.\",\"Data\":null,\"InnerException\":null,\"HelpURL\":null,\"StackTraceString\":\"   at WireMock.Owin.Mappers.OwinResponseMapper.<MapAsync>d__6.MoveNext()\\r\\n--- End of stack trace from previous location where exception was thrown ---\\r\\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\\r\\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\\r\\n   at WireMock.Owin.WireMockMiddleware.<InvokeInternal>d__7.MoveNext()\\r\\n--- End of stack trace from previous location where exception was thrown ---\\r\\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\\r\\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\\r\\n   at WireMock.Owin.GlobalExceptionMiddleware.<InvokeInternal>d__7.MoveNext()\",\"RemoteStackTraceString\":null,\"RemoteStackIndex\":0,\"ExceptionMethod\":\"8\\nMoveNext\\nWireMock.Net, Version=1.0.32.0, Culture=neutral, PublicKeyToken=c8d65537854e1f03\\nWireMock.Owin.Mappers.OwinResponseMapper+<MapAsync>d__6\\nVoid MoveNext()\",\"HResult\":-2147467261,\"Source\":\"WireMock.Net\",\"WatsonBuckets\":null}"
}

I guess this is intended ?

Also what about 'most interesting' fault type , which is CONNECTION_RESET_BY_PEER ? There was no enum value for it.

Apart from that, everything is great :)

@grzegorz-wolszczak commented on GitHub (Oct 23, 2019): Ok, now i See what the problem was, I have chosen wrong nuget version. So I've tested EMPTY_RESPONSE, and RANDOM_DATA_THEN_CLOSE and it works good (tested with 'curl') but for FaultType.MALFORMED_RESPONSE_CHUNK when request GET I got: Status code: 500Internal Server Error and body ``` { "Status": "{\"ClassName\":\"System.NullReferenceException\",\"Message\":\"Object reference not set to an instance of an object.\",\"Data\":null,\"InnerException\":null,\"HelpURL\":null,\"StackTraceString\":\" at WireMock.Owin.Mappers.OwinResponseMapper.<MapAsync>d__6.MoveNext()\\r\\n--- End of stack trace from previous location where exception was thrown ---\\r\\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\\r\\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\\r\\n at WireMock.Owin.WireMockMiddleware.<InvokeInternal>d__7.MoveNext()\\r\\n--- End of stack trace from previous location where exception was thrown ---\\r\\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\\r\\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\\r\\n at WireMock.Owin.GlobalExceptionMiddleware.<InvokeInternal>d__7.MoveNext()\",\"RemoteStackTraceString\":null,\"RemoteStackIndex\":0,\"ExceptionMethod\":\"8\\nMoveNext\\nWireMock.Net, Version=1.0.32.0, Culture=neutral, PublicKeyToken=c8d65537854e1f03\\nWireMock.Owin.Mappers.OwinResponseMapper+<MapAsync>d__6\\nVoid MoveNext()\",\"HResult\":-2147467261,\"Source\":\"WireMock.Net\",\"WatsonBuckets\":null}" } ``` I guess this is intended ? Also what about 'most interesting' fault type , which is CONNECTION_RESET_BY_PEER ? There was no enum value for it. Apart from that, everything is great :)
Author
Owner

@StefH commented on GitHub (Oct 23, 2019):

About malformed response , this should not throw a 500.
Can you provide your code or mapping json?

@StefH commented on GitHub (Oct 23, 2019): About malformed response , this should not throw a 500. Can you provide your code or mapping json?
Author
Owner

@StefH commented on GitHub (Oct 24, 2019):

I found the issue. Please try MyGet : WireMock.Net.1.0.32-ci-12060

@StefH commented on GitHub (Oct 24, 2019): I found the issue. Please try MyGet : WireMock.Net.1.0.32-ci-12060
Author
Owner

@grzegorz-wolszczak commented on GitHub (Oct 24, 2019):

The code I used was (simplified version I found on your's project wiki):

using System;
using Newtonsoft.Json;

using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;

namespace TestingWireMock
{
    class Program
    {
        static void Main(string[] args)
        {
            int port;
            if (args.Length == 0 || !int.TryParse(args[0], out port))
                port = 18080;

            var server = FluentMockServer.Start(port);
            Console.WriteLine("FluentMockServer running at {0}", string.Join(",", server.Ports));
            
            server
                .Given(Request.Create().WithPath(u => u.Contains("x")).UsingGet())
                .RespondWith(Response.Create()
                    .WithStatusCode(200)
                    .WithHeader("Content-Type", "application/json")
                    .WithBody(@"{ ""result"": ""/x with FUNC 200""}"));

            server
                .Given(Request.Create().WithPath("/*").UsingGet())
                .RespondWith(Response.Create().WithFault(FaultType.MALFORMED_RESPONSE_CHUNK));
                
            
            Console.WriteLine("Press any key to stop the server");
            Console.ReadKey();

            Console.WriteLine("Displaying all requests");
            var allRequests = server.LogEntries;
            Console.WriteLine(JsonConvert.SerializeObject(allRequests, Formatting.Indented));

            Console.WriteLine("Press any key to quit");
            Console.ReadKey();
        }
    }
}

I've used nuget version: WireMock.Net.1.0.32-ci-12002.nupkg

@grzegorz-wolszczak commented on GitHub (Oct 24, 2019): The code I used was (simplified version I found on your's project wiki): ``` c# using System; using Newtonsoft.Json; using WireMock.RequestBuilders; using WireMock.ResponseBuilders; using WireMock.Server; namespace TestingWireMock { class Program { static void Main(string[] args) { int port; if (args.Length == 0 || !int.TryParse(args[0], out port)) port = 18080; var server = FluentMockServer.Start(port); Console.WriteLine("FluentMockServer running at {0}", string.Join(",", server.Ports)); server .Given(Request.Create().WithPath(u => u.Contains("x")).UsingGet()) .RespondWith(Response.Create() .WithStatusCode(200) .WithHeader("Content-Type", "application/json") .WithBody(@"{ ""result"": ""/x with FUNC 200""}")); server .Given(Request.Create().WithPath("/*").UsingGet()) .RespondWith(Response.Create().WithFault(FaultType.MALFORMED_RESPONSE_CHUNK)); Console.WriteLine("Press any key to stop the server"); Console.ReadKey(); Console.WriteLine("Displaying all requests"); var allRequests = server.LogEntries; Console.WriteLine(JsonConvert.SerializeObject(allRequests, Formatting.Indented)); Console.WriteLine("Press any key to quit"); Console.ReadKey(); } } } ``` I've used nuget version: WireMock.Net.1.0.32-ci-12002.nupkg
Author
Owner

@StefH commented on GitHub (Oct 24, 2019):

Thanks.
Can you retry WireMock.Net.1.0.32-ci-12060?

@StefH commented on GitHub (Oct 24, 2019): Thanks. Can you retry WireMock.Net.1.0.32-ci-12060?
Author
Owner

@grzegorz-wolszczak commented on GitHub (Oct 24, 2019):

WireMock.Net.1.0.32-ci-12060 works.
Although I could not spot the difference between MALFORMED_RESPONSE_CHUNK and RANDOM_DATA_THEN_CLOSE.
Both respond with 200 OK and some random body.

From what I can understand in http://wiremock.org/docs/simulating-faults/ in 'Bad responses' section.
RANDOM_DATA_THEN_CLOSE does not send 200 OK header (it does not send any header at all)

@grzegorz-wolszczak commented on GitHub (Oct 24, 2019): WireMock.Net.1.0.32-ci-12060 works. Although I could not spot the difference between MALFORMED_RESPONSE_CHUNK and RANDOM_DATA_THEN_CLOSE. Both respond with 200 OK and some random body. From what I can understand in http://wiremock.org/docs/simulating-faults/ in 'Bad responses' section. RANDOM_DATA_THEN_CLOSE does not send 200 OK header (it does not send any header at all)
Author
Owner

@StefH commented on GitHub (Oct 24, 2019):

1] There is a difference.
In case you have a body defined, the MALFORMED_RESPONSE_CHUNK returns half of that body and the rest is random.

2] RANDOM_DATA_THEN_CLOSE should not write any header. However, maybe the default header is 200.
The other two errors will take your original defined header.

@StefH commented on GitHub (Oct 24, 2019): 1] There is a difference. In case you have a body defined, the MALFORMED_RESPONSE_CHUNK returns half of that body and the rest is random. 2] RANDOM_DATA_THEN_CLOSE should not write any header. However, maybe the default header is 200. The other two errors will take your original defined header.
Author
Owner

@StefH commented on GitHub (Oct 25, 2019):

@grzegorz-wolszczak Does my comment make sense?

@StefH commented on GitHub (Oct 25, 2019): @grzegorz-wolszczak Does my comment make sense?
Author
Owner

@grzegorz-wolszczak commented on GitHub (Oct 25, 2019):

With MALFORMED_RESPONSE_CHUNK it makes sense.
But when it comes to RANDOM_DATA_THEN_CLOSE i believe that the sole purpose of it was exactly that, 'random data' which means that response does not conform to any valid HTTP response from the protocol standpoint. So it is more a random TCP packet than HTTP message (with valid headers, status line and so on).

@grzegorz-wolszczak commented on GitHub (Oct 25, 2019): With MALFORMED_RESPONSE_CHUNK it makes sense. But when it comes to RANDOM_DATA_THEN_CLOSE i believe that the sole purpose of it was exactly that, 'random data' which means that response does not conform to any valid HTTP response from the protocol standpoint. So it is more a random TCP packet than HTTP message (with valid headers, status line and so on).
Author
Owner

@StefH commented on GitHub (Oct 25, 2019):

With the current code and setup, I don't think I can really generate random data (TCP packet) instead of an HTTP message. But I will investigate.

@StefH commented on GitHub (Oct 25, 2019): With the current code and setup, I don't think I can really generate random data (TCP packet) instead of an HTTP message. But I will investigate.
Author
Owner

@grzegorz-wolszczak commented on GitHub (Oct 25, 2019):

I understand that this might be very difficult or event impossible. Probably Kestrel does not allow of such low-level packet manipulation. If so I would remove the RANDOM_DATA_THEN_CLOSE from the solution utill you figure out how to achieve it. Apart from that , the rest of your code brings new value to WireMock library so I would merge it.

@grzegorz-wolszczak commented on GitHub (Oct 25, 2019): I understand that this might be very difficult or event impossible. Probably Kestrel does not allow of such low-level packet manipulation. If so I would remove the RANDOM_DATA_THEN_CLOSE from the solution utill you figure out how to achieve it. Apart from that , the rest of your code brings new value to WireMock library so I would merge it.
Author
Owner

@StefH commented on GitHub (Oct 26, 2019):

Merged to master and a new NuGet will be released shortly.

@StefH commented on GitHub (Oct 26, 2019): Merged to master and a new NuGet will be released shortly.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#212