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

Support matching query params for post, put methods. #130

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

thk2b
Copy link

@thk2b thk2b commented Apr 21, 2018

This PR closes #88 and #129.

Currently, it is possible to expect query params for GET, or a specific body for POST, but it is impossible to expect specific query params and a specific body.

To solve this, I added a fourth argument to MockAdapter.prototype[methodName] (ie. onPost, onGet), which corresponds to the expected query parameters for the request. This allows matching both a body and parameters for methods that have a body (PUT, POST, ...).

This is entirely backwards compatible, meaning that we can still match the params for GET, DELETE, etc... by specifying the expected params as the second argument (in place of the expected body as for PUT and POST methods). Overall, this PR does not affect the behaviour of the current public api, since it only adds an argument.

This test case demonstrates the new behaviour:

it('can pass a body, query params, and headers to match a handler', function() {
  var headers = {
    Accept: 'application/json, text/plain, */*',
    'Content-Type': 'application/x-www-form-urlencoded',
    'Header-test': 'test-header'
  };
  mock.onPost('/withBodyAndParams', { data: { is: 'passed' }, in: true }, headers, { foo: 'bar', bar: 'baz' }).reply(200);
  
  return instance
    .post('/withBodyAndParams', { data: { is: 'passed' }, in: true }, { headers: headers, params: { foo: 'bar', bar: 'baz' }})
    .then(function(response) {
      expect(response.status).to.equal(200);
    });
});

Implementing this required adding the expected request parameters to the handler array. All indexes throughout the source were incremented as needed. Then, we check that the params do match in utils.findHandler.

I added several new test cases.

I also modified some tests involving matching params, where the expected params were passed inside the params key of an object. This was not required, and I'm not sure why the tests passed in the first place. See this commit.

@thk2b thk2b changed the title Support matching query params in post, put methods. Support matching query params for post, put methods. Apr 21, 2018
@thk2b
Copy link
Author

thk2b commented Apr 26, 2018

@ctimmerm , can you let me know if anything can be improved or if there is a problem with the PR? Thanks !

@thk2b
Copy link
Author

thk2b commented May 3, 2018

@ctimmerm Hello Colin,

Again, may I get some feedback on this PR ? I'd really like to get this merged. If something is wrong please let me know and I will work on it.

Thank you for your time !

@clockworked247
Copy link

clockworked247 commented Dec 20, 2018

@ctimmerm @thk2b It'd be great to get this feature in! Usage of params with POST/PUT requests shows up in the Axios docs and I'm sure many developers need this feature to properly filter which requests to mock.

Right now my not-so-great-work-around without this feature is to check the params in the reply

    mock.onPost(/\/path.+/).reply(function(config) {
      return new Promise(function(resolve, reject) {
        if (config.params 
            && config.params.querytype === 'javascript'
            && config.params.action === 'profile') {
          headers.qid = config.params.qid;
          resolve([200, responseBody, headers]);
        }
        else
          // this is not what I want to do, since this triggers the .catch() method.  
          // If this work-around functioned as I would like, I would need to trigger a passThrough,
          // rather than reject().   The proper implementation would be to support params with onPost()
          reject();  
      })
    });

Since I have no way to trigger a passThrough within the reply, I need the ability to filter up front as a part of the onPost call with params.

@jDeppen
Copy link
Contributor

jDeppen commented Mar 2, 2019

Thanks, @clockworked247 (and @thk2b).
For posterity, here's how I handled mine (stripped down version) with Jest snapshots. I only need to ensure the correct data is being sent.

it('example test', () => {
  let actualData
  mock.onPost(url).reply(config => {
    actualData = config.data
    return new Promise(resolve => resolve([200, responseBody, headers]))
  })

  return store.dispatch(fnToTest()).then(() => {
    expect(JSON.parse(actualData)).toMatchSnapshot()
  })
})

@keeganwitt
Copy link

Should #88 or #129 be reopened since this never got merged?

@renanzulian
Copy link

Do you know when this feature will be merged?

@mvitz
Copy link

mvitz commented Feb 24, 2022

Any news if or when this does get merged?

@ctimmerm ctimmerm added the 2.0 label Jun 1, 2022
@nasyarobby
Copy link

any news? it's like 2023 already.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Mock a post request with data and query params
8 participants