natenho / Mockaco

🐵 HTTP mock server, useful to stub services and simulate dynamic API responses, leveraging ASP.NET Core features, built-in fake data generation and pure C# scripting
https://natenho.github.io/Mockaco/
Other
335 stars 39 forks source link

Mock being returned when there is an error in condition #127

Closed natenho closed 8 months ago

natenho commented 8 months ago

Prerequisites

Description

When the condition expression has an error, the condition is completely ignored, returning the mock.

Steps to reproduce

Using this template:

{
  "request": {
    "method": "GET",
    "route": "hello/{message}",
    "condition": "<#= Request.Query["missing"].Equals("foobar") #>"
  },
  "response": {
    "body": {
      "error": "this should not be returned"
    }
  }
}

Then call the request without the query parameter "missing"

> curl "http://localhost:5000/hello/there"

StatusCode        : 200
StatusDescription : OK
Content           : {
                      "error": "this should not be returned"
                    }
RawContent        : HTTP/1.1 200 OK
                    Transfer-Encoding: chunked
                    Content-Type: application/json
                    Date: Sun, 10 Mar 2024 19:51:55 GMT
                    Server: Kestrel

                    {
                      "error": "this should not be returned"
                    }
Forms             : {}
Headers           : {[Transfer-Encoding, chunked], [Content-Type, application/json], [Date, Sun, 10 Mar 2024 19:51:55
                    GMT], [Server, Kestrel]}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 46

Expected behavior

HTTP 501 In this case, the condition should not match, the mock should not be returned.

Screenshots

image

Additional context

Mockaco Version 1.8.1

natenho commented 8 months ago

It is also reproducible in the current version 1.9.0, considering the following template:

{
  "request": {
    "method": "GET",
    "route": "hello/{message}",
    "condition": "<#= this.Should.GenerateError() #>"
  },
  "response": {
    "body": {
      "error": "this should not be returned!"
    }
  }
}