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
336 stars 39 forks source link

Request matching on query string to return mocked GraphQL responses #113

Closed joshuajones02 closed 1 year ago

joshuajones02 commented 1 year ago

Prerequisites

Description

This solutions works great for what I needed until I went to Mock out GraphQL requests and it does not seem to like it. With the .NET GraphQL library used as a client it will add a ?q=queryName query string to a request but this isn't required. I was thinking this could work for this library utilizing the query string however when trying to match on that, I've received the error The literal section '?q=allCustomersV2' is invalid. Literal sections cannot contain the '?' character. I could look at contributing the implementation if this is something that would be wanted.

Proposed solution

Request matching only on a query string.

Alternatives

The only other alternative for this request would be to pull out a property from the request body

Additional context

Thank you!

natenho commented 1 year ago

Hello @joshuajones02 , this is because of how ASPNet routing works.

But you can definetely achieve what you want by using Mockaco C# scripting. Try this:

{
  "request": {
    "method": "GET",
    "route": "ping",
    "condition": "<#= Request.Query["q"]?.ToString() == "allCustomersV2" #>"
  },
  "response": {
    "status": "OK",
    "body": {
      "response": "This is <#= Request.Query["q"]?.ToString() #>"
    }
  }
}

Then

$ curl -iX GET http://localhost:5000/ping?q=allCustomersV2
HTTP/1.1 200 OK
Content-Type: application/json
Date: Sat, 28 Jan 2023 05:03:58 GMT
Server: Kestrel
Transfer-Encoding: chunked

{
  "response": "This is allCustomersV2"
}
joshuajones02 commented 1 year ago

Hello @joshuajones02 , this is because of how ASPNet routing works.

But you can definetely achieve what you want by using Mockaco C# scripting. Try this:

{
  "request": {
  "method": "GET",
  "route": "ping",
  "condition": "<#= Request.Query["q"]?.ToString() == "allCustomersV2" #>"
  },
  "response": {
  "status": "OK",
  "body": {
    "response": "This is <#= Request.Query["q"]?.ToString() #>"
  }
  }
}

Then

$ curl -iX GET http://localhost:5000/ping?q=allCustomersV2
HTTP/1.1 200 OK
Content-Type: application/json
Date: Sat, 28 Jan 2023 05:03:58 GMT
Server: Kestrel
Transfer-Encoding: chunked

{
  "response": "This is allCustomersV2"
}

Thank you @natenho, I will try it out