pretenderjs / pretender

A mock server library with a nice routing DSL
MIT License
1.26k stars 158 forks source link

Mocking route with query params #316

Open cmjio opened 3 years ago

cmjio commented 3 years ago

I am using MirageJS which uses pretender but the issue I think lies in pretender.

Trying to mock an API that uses RSQL as a query language. Requests look like the following /api/films?filter=genres=in=(comedy);actor=="Ryan Reynolds" with fairly complex query params. Is there any way to mock this exact URL?

I would have thought I could have done this.get('/films?filter=genres=in=(comedy);actor=="Ryan Reynolds"', () => {...}) but looks like the only route the will catch is mocking /films though currently using the request.queryParams it returns {filter: "genres"} and omits the rest of the query.

How might I, if at all mock API endpoints with specific queries appended to return different responses.

xg-wang commented 3 years ago

https://github.com/pretenderjs/pretender/tree/54ea79ede7bbbf5bcebaac507f2ec9db6875f4a5#query-parameters You can access from the request object:

const server = new Pretender(function() {
  this.get('/api/songs', request => request.queryParams.sortOrder);
});

Does this suffice the need?

cmjio commented 3 years ago

For a simple query param yes, but when using the RSQL format it does not. From my example /films?filter=genres=in=(comedy);actor=="Ryan Reynolds" the queryParams object does not return the complex query in parts or even the full string.

As a simple approach even if it returned { filter: 'genres=in=(comedy);actor=="Ryan Reynolds"'} I would be happy, seems it ignores the = after genres

xg-wang commented 3 years ago

Looks like route-recognizer does not support the standard searchParams parsing, I just filed an issue there