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

Returned strings get extra quotes #140

Open
2 tasks done
paule96 opened this issue Sep 9, 2024 · 4 comments
Open
2 tasks done

Returned strings get extra quotes #140

paule96 opened this issue Sep 9, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@paule96
Copy link

paule96 commented Sep 9, 2024

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing bugs to ensure a similar bug has not already been created

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\""
  }
}
@paule96 paule96 added the bug Something isn't working label Sep 9, 2024
@natenho
Copy link
Owner

natenho commented Sep 12, 2024

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

natenho commented Sep 15, 2024

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

paule96 commented Sep 16, 2024

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

natenho commented Sep 16, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants