How do I mock an endpoint to return an error given a specific parameter, otherwise return success? #441

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

Originally created by @rosemaryehlers on GitHub (Aug 19, 2022).

I am mocking an endpoint /mark-item, which takes a numeric id. when id=666, I want the endpoint to return 500, otherwise all other requests to that endpoint should return 200.

This is what my static mapping file currently looks like:

[{
    "Guid": "68ae335b-5d79-42dc-8ca7-236280ab9111",
    "Priority": 100,
    "Request": {
        "Path": {
            "Matchers": [
                {
                    "Name": "WildcardMatcher",
                    "Pattern": "/mark-item",
                    "IgnoreCase": true
                }
            ]
        },
        "Methods": ["post", "get"],
        "Params": [
            {
                "Name": "id",
                "Matchers": [
                    {
                        "Name": "ExactMatcher",
                        "Pattern": "666"
                    }
                ]
            }
        ]
    },
    "Response": {
        "StatusCode": 500,
        "BodyAsJson": {},
        "Headers": {
            "Content-Type": "application/json",
            "Access-Control-Allow-Origin": "*"
        }
    }
},
{
    "Guid": "68ae335b-5d79-42dc-8ca7-236280ab9111",
    "Priority": 1,
    "Request": {
        "Path": {
            "Matchers": [
                {
                    "Name": "WildcardMatcher",
                    "Pattern": "/mark-item",
                    "IgnoreCase": true
                }
            ]
        },
        "Methods": ["post", "get"],
        "Params": [
            {
                "Name": "id",
                "Matchers": [
                    {
                        "Name": "WildcardMatcher",
                        "Pattern": "*"
                    }
                ]
            }
        ]
    },
    "Response": {
        "StatusCode": 200,
        "BodyAsJson": {},
        "Headers": {
            "Content-Type": "application/json",
            "Access-Control-Allow-Origin": "*"
        }
    }
}]

When I remove the success stub, a request to http://localhost:49223/mark-item?id=66 returns 404 and a request to http://localhost:49223/mark-item?id=666 returns 500. But when I add in the success stub, both requests return 200. I have tried flipping the priorities, but that also makes no difference.
What is the correct way to do this? Thank you!

Originally created by @rosemaryehlers on GitHub (Aug 19, 2022). I am mocking an endpoint /mark-item, which takes a numeric id. when id=666, I want the endpoint to return 500, otherwise all other requests to that endpoint should return 200. This is what my static mapping file currently looks like: ``` json [{ "Guid": "68ae335b-5d79-42dc-8ca7-236280ab9111", "Priority": 100, "Request": { "Path": { "Matchers": [ { "Name": "WildcardMatcher", "Pattern": "/mark-item", "IgnoreCase": true } ] }, "Methods": ["post", "get"], "Params": [ { "Name": "id", "Matchers": [ { "Name": "ExactMatcher", "Pattern": "666" } ] } ] }, "Response": { "StatusCode": 500, "BodyAsJson": {}, "Headers": { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*" } } }, { "Guid": "68ae335b-5d79-42dc-8ca7-236280ab9111", "Priority": 1, "Request": { "Path": { "Matchers": [ { "Name": "WildcardMatcher", "Pattern": "/mark-item", "IgnoreCase": true } ] }, "Methods": ["post", "get"], "Params": [ { "Name": "id", "Matchers": [ { "Name": "WildcardMatcher", "Pattern": "*" } ] } ] }, "Response": { "StatusCode": 200, "BodyAsJson": {}, "Headers": { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*" } } }] ``` When I remove the success stub, a request to http://localhost:49223/mark-item?id=66 returns 404 and a request to http://localhost:49223/mark-item?id=666 returns 500. But when I add in the success stub, _both_ requests return 200. I have tried flipping the priorities, but that also makes no difference. What is the correct way to do this? Thank you!
adam added the question label 2025-12-29 08:28:12 +01:00
adam closed this issue 2025-12-29 08:28:12 +01:00
Author
Owner

@StefH commented on GitHub (Aug 19, 2022):

I think you should switch the priority.

-> The priority. (A low value means higher priority.)

Can you try that?

@StefH commented on GitHub (Aug 19, 2022): I think you should switch the priority. -> `The priority. (A low value means higher priority.)` Can you try that?
Author
Owner

@rosemaryehlers commented on GitHub (Aug 22, 2022):

I've tried the priority values flipped, and it does not appear to affect anything.

Further testing shows that the WildcardMatcher for * for the id param actually fails requests with 404 no mapping, so I removed that clause entirely. New file is as follows:

[{
    "Guid": "68ae335b-5d79-42dc-8ca7-236280ab9111",
    "Priority": 1,
    "Request": {
        "Path": {
            "Matchers": [
                {
                    "Name": "WildcardMatcher",
                    "Pattern": "/mark-item",
                    "IgnoreCase": true
                }
            ]
        },
        "Methods": ["post", "get"],
        "Params": [
            {
                "Name": "id",
                "Matchers": [
                    {
                        "Name": "ExactMatcher",
                        "Pattern": "666"
                    }
                ]
            }
        ]
    },
    "Response": {
        "StatusCode": 500,
        "BodyAsJson": {},
        "Headers": {
            "Content-Type": "application/json",
            "Access-Control-Allow-Origin": "*"
        }
    }
},
{
    "Guid": "68ae335b-5d79-42dc-8ca7-236280ab9111",
    "Priority": 100,
    "Request": {
        "Path": {
            "Matchers": [
                {
                    "Name": "WildcardMatcher",
                    "Pattern": "/mark-item",
                    "IgnoreCase": true
                }
            ]
        },
        "Methods": ["post", "get"]
    },
    "Response": {
        "StatusCode": 200,
        "BodyAsJson": {},
        "Headers": {
            "Content-Type": "application/json",
            "Access-Control-Allow-Origin": "*"
        }
    }
}]

Hitting the following urls in my web browser give the following:
http://localhost:49159/mark-item?id=666 -> Return 200 (Want it to return 500)
http://localhost:49159/mark-item?id=4 -> Return 200 (Correct)

@rosemaryehlers commented on GitHub (Aug 22, 2022): I've tried the priority values flipped, and it does not appear to affect anything. Further testing shows that the WildcardMatcher for * for the id param actually fails requests with 404 no mapping, so I removed that clause entirely. New file is as follows: ``` [{ "Guid": "68ae335b-5d79-42dc-8ca7-236280ab9111", "Priority": 1, "Request": { "Path": { "Matchers": [ { "Name": "WildcardMatcher", "Pattern": "/mark-item", "IgnoreCase": true } ] }, "Methods": ["post", "get"], "Params": [ { "Name": "id", "Matchers": [ { "Name": "ExactMatcher", "Pattern": "666" } ] } ] }, "Response": { "StatusCode": 500, "BodyAsJson": {}, "Headers": { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*" } } }, { "Guid": "68ae335b-5d79-42dc-8ca7-236280ab9111", "Priority": 100, "Request": { "Path": { "Matchers": [ { "Name": "WildcardMatcher", "Pattern": "/mark-item", "IgnoreCase": true } ] }, "Methods": ["post", "get"] }, "Response": { "StatusCode": 200, "BodyAsJson": {}, "Headers": { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*" } } }] ``` Hitting the following urls in my web browser give the following: http://localhost:49159/mark-item?id=666 -> Return 200 (Want it to return 500) http://localhost:49159/mark-item?id=4 -> Return 200 (Correct)
Author
Owner

@rosemaryehlers commented on GitHub (Aug 22, 2022):

Oh god I'm an idiot. The guids are matching, so the last configuration is overriding the first one. Sorry about that!

@rosemaryehlers commented on GitHub (Aug 22, 2022): Oh god I'm an idiot. The guids are matching, so the last configuration is overriding the first one. Sorry about that!
Author
Owner

@StefH commented on GitHub (Aug 22, 2022):

Ah. I also missed duplicate GUID.

Maybe WireMock.Net should throw exception when duplicate GUiD is detected ..

@StefH commented on GitHub (Aug 22, 2022): Ah. I also missed duplicate GUID. Maybe WireMock.Net should throw exception when duplicate GUiD is detected ..
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#441