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

Match data and params in onPut, onPost #129

Closed
thk2b opened this issue Apr 19, 2018 · 2 comments
Closed

Match data and params in onPut, onPost #129

thk2b opened this issue Apr 19, 2018 · 2 comments

Comments

@thk2b
Copy link

thk2b commented Apr 19, 2018

I would like to match specific data and params in an onPut handler.

The docs mention this:

Mocking a request with a specific request body/data
mock.onPut('/product', { id: 4, name: 'foo' }).reply(204);

Mocking a GET request with specific parameters
mock.onGet('/users', { params: { searchText: 'John' } }).reply(200, {...})

So, it's possible to match params for get requests, and data for put and post. But this test case shows that it's not possible to match params in onPut.

  it("can't pass query params for put to match to a handler", function() {
    mock.onPut('/withParams', { params: { foo: 'bar', bar: 'foo' } }).reply(200);

    return instance
      .put('/withParams', { params: { bar: 'foo', foo: 'bar' }, in: true })
      .catch(function(error) {
        expect(error.response.status).to.equal(404);
      });
  });

Specifically, something like this would be convenient:
mock.onPut('/users', { params: { userId: '123' }, data: { userName: 'new userName' }}),
or
mock.onPut('/users', { userName: 'new userName' }, { userId: '123' })

I achieved something equivalent with mock.onAny().reply(), which works well with jest.

const newProfile = {
     name: 'new name'
}

mock.onAny().reply( config => {
    try {
        expect(config.url).toBe('/profile')
        expect(config.method).toBe('put')
        expect(config.params).toEqual({ id: '123' })
        expect(config.data).toEqual(JSON.stringify(newProfile))
        return [202, newProfile]    
    } catch (e){
        done.fail(e)
        return [500]
    }

})

But this is a bit convoluted, I have to manually check the url, params, parse the data, etc...

Is there a reason why this isn't or can't be implemented ? Otherwise, I'd be happy to work on a solution.

@thk2b
Copy link
Author

thk2b commented Apr 19, 2018

This is similar to #88, possibly a duplicate.

@ctimmerm
Copy link
Owner

PR here: #130

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

No branches or pull requests

2 participants