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

Returned strings get extra quotes #140

Open paule96 opened 1 month ago

paule96 commented 1 month ago

Prerequisites

Description

If you provide a mock JSON that should return in the body a string, the actual HTTP response includes always the quotes of the string. That is an issue because normally in the dotnet world if an API just returns a string, it is not enclosed into quoates.

Steps to reproduce

Sample mock file:

{
  "request": {
    "method": "GET",
    "route": "something"
  },
  "response": {
    "status": "OK",
    "body": "1ccbed54-c179-450f-a0a8-cf5b07faa7b2"
  }
}

If you run the mockaco container with this file you will get the following response:

StatusCode        : 200
StatusDescription : OK
Content           : "1ccbed54-c179-450f-a0a8-cf5b07faa7b2"
RawContent        : HTTP/1.1 200 OK
                    Date: Mon, 09 Sep 2024 06:20:38 GMT
                    Server: Kestrel
                    Transfer-Encoding: chunked
                    Content-Type: application/json

                    "1ccbed54-c179-450f-a0a8-cf5b07faa7b2"
Headers           : {[Date, System.String[]], [Server, System.String[]], [Transfer-Encoding, System.String[]],
                    [Content-Type, System.String[]]}
Images            : {}
InputFields       : {}
Links             : {}
RawContentLength  : 38
RelationLink      : {}

Expected behavior

The expexted behavior should be more like:

StatusCode        : 200
StatusDescription : OK
Content           : "1ccbed54-c179-450f-a0a8-cf5b07faa7b2"
RawContent        : HTTP/1.1 200 OK
                    Date: Mon, 09 Sep 2024 06:20:38 GMT
                    Server: Kestrel
                    Transfer-Encoding: chunked
                    Content-Type: application/json

                    1ccbed54-c179-450f-a0a8-cf5b07faa7b2
Headers           : {[Date, System.String[]], [Server, System.String[]], [Transfer-Encoding, System.String[]],
                    [Content-Type, System.String[]]}
Images            : {}
InputFields       : {}
Links             : {}
RawContentLength  : 38
RelationLink      : {}

Quotes are removed

Screenshots

No response

Additional context

If a user wants explicitly return a string with quotes, he should use normal JSON escaping:

{
  "request": {
    "method": "GET",
    "route": "something"
  },
  "response": {
    "status": "OK",
    "body": "\"1ccbed54-c179-450f-a0a8-cf5b07faa7b2\""
  }
}
natenho commented 1 month ago

Thank you @paule96 , that is probably because the body is being treated as a JSON.

When the body itself is not a JSON object we should return the raw content, instead of considering it a JSON Token.

natenho commented 1 month ago

@paule96 , as a workaround you can always use a raw response file like this:

1) Create a file named foo.txt 2) Setup your mock

{
  "request": {
    "method": "GET",
    "route": "/raw-string-example"
  },
  "response": {
    "status": "OK",
    "file": "Mocks/foo.txt"
  }
}

Then

$ curl -iX GET http://localhost:5000/raw-string-example
HTTP/1.1 200 OK
Content-Type: application/json
Date: Sun, 15 Sep 2024 19:04:53 GMT
Server: Kestrel
Transfer-Encoding: chunked

This is a raw string
paule96 commented 1 month ago

Hi @natenho,

yes, I know about that option. But it sounds a bit weird to me for short responses to create an extra file. It would be easier to define it directly in the JSON itself

natenho commented 1 month ago

@paule96 Absolutely! Just wanted to give you a workaround so you're not blocked.