Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Please provide support for Bad responses. #343

Closed
grzegorz-wolszczak opened this issue Sep 10, 2019 · 23 comments
Closed

Feature: Please provide support for Bad responses. #343

grzegorz-wolszczak opened this issue Sep 10, 2019 · 23 comments
Labels

Comments

@grzegorz-wolszczak
Copy link

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/

@grzegorz-wolszczak grzegorz-wolszczak changed the title Feature: Please provide support for (Bad responses). Feature: Please provide support for Bad responses. Sep 10, 2019
@StefH
Copy link
Collaborator

StefH commented Sep 16, 2019

What kind of bad responses do you need?

@grzegorz-wolszczak
Copy link
Author

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:

@StefH
Copy link
Collaborator

StefH commented 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?

@grzegorz-wolszczak
Copy link
Author

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.

@StefH StefH added the feature label Sep 23, 2019
@StefH
Copy link
Collaborator

StefH commented 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?

@grzegorz-wolszczak
Copy link
Author

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

@StefH
Copy link
Collaborator

StefH commented Oct 11, 2019

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

@StefH
Copy link
Collaborator

StefH commented Oct 22, 2019

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

@grzegorz-wolszczak
Copy link
Author

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
Copy link
Author

grzegorz-wolszczak commented 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.

@StefH
Copy link
Collaborator

StefH commented 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"
        }
    }
}

@grzegorz-wolszczak
Copy link
Author

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 :)

@StefH
Copy link
Collaborator

StefH commented Oct 23, 2019

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

@StefH
Copy link
Collaborator

StefH commented Oct 24, 2019

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

@grzegorz-wolszczak
Copy link
Author

grzegorz-wolszczak commented 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

@StefH
Copy link
Collaborator

StefH commented Oct 24, 2019

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

@grzegorz-wolszczak
Copy link
Author

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)

@StefH
Copy link
Collaborator

StefH commented 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
Copy link
Collaborator

StefH commented Oct 25, 2019

@grzegorz-wolszczak Does my comment make sense?

@grzegorz-wolszczak
Copy link
Author

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).

@StefH
Copy link
Collaborator

StefH commented 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.

@grzegorz-wolszczak
Copy link
Author

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.

@StefH
Copy link
Collaborator

StefH commented Oct 26, 2019

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

@StefH StefH closed this as completed Oct 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants