ctimmerm / axios-mock-adapter

Axios adapter that allows to easily mock requests
MIT License
3.47k stars 245 forks source link

POST request with params? #259

Closed OliverEvans96 closed 3 months ago

OliverEvans96 commented 4 years ago

Hi there,

Thanks for the great package!

Just a quick question - it seems that it's not currently possible to .reply or .replyOnce to a POST request containing specific URL parameters due to the definition of var allowedParamsMethods = ["delete", "get", "head", "options"]; here

Is it a general rule that only these HTTP methods should have URL parameters? Is there some other reason for not allowing url parameters in all verbs here? Am I misunderstanding?

Thanks a lot! Oliver

reagent commented 3 years ago

I ran into this issue as well -- I ended up forking the repository and making a change to the isBodyOrParametersMatching utility function: https://github.com/KindHealth-Holdings/axios-mock-adapter/commit/6793904f9e6a031a4e3bfa439025b08500d1cc8d

I haven't battle tested it much just yet and I'm not 100% satisfied with the implementation, but here's how I've been using it with a POST request matcher using the params key of the body:

httpMock.onPost(
  'https://host.example/path', 
  {key: 'value', other: 'value', params: {key: 'value'}}
).replyOnce(200, {response: {key: 'value'}})

If you want to try out my fork, you can just do:

yarn add -D https://github.com/KindHealth-Holdings/axios-mock-adapter.git#match-params-on-post

I'll likely be making changes there, so I can't guarantee the stability of that branch, but it may help you get unblocked.

marcbachmann commented 3 months ago

This is now supported in v2.0.0