aioresponses icon indicating copy to clipboard operation
aioresponses copied to clipboard

Feature request: allow hdr.METH_ANY to match any HTTP request method

Open spapadim opened this issue 4 years ago • 0 comments

Was porting some code that uses requests + requests_mock to aiohttp + aioresponses, which occasionally uses requests_mock.ANY when adding requests to mocker object.

aiohttp already defines the aiohttp.hdrs.METH_ANY constant (defined simply as "*"). It seems that adding support for this in aioresponses would require only a minor change in the first line of the definition of the RequestMatch.match method should be changed do:

def match(self, method: str, url: URL) -> bool:
-  if self.method != method.lower():
+  if self.method != method.lower() and self.method != hdrs.METH_ANY:
      return False
  return self.match_func(url)

This behavior would exactly match the behavior of requests_mock. I'm not sure how other mock frameworks that support an "ANY" method parameter interpret it, nor if aiohttp has any special semantics around it (I could not find any, but didn't dig very deep--however, it does also define an hdrs.METH_ALL, which is a set value). Also, I have no idea if you have any other plans for extending match semantics (e.g., perhaps allow method to be a set of values?).

However, if this feature sounds reasonable and doesn't conflict with anything else, I could also spend a bit of time to submit a PR (either for just hdrs.METH_ANY support, or any preferred alternative such as set arg value, with appropriate unit tests), if that would help.

Finally, thanks for writing aioresponses, very helpful!

spapadim avatar Aug 31 '20 00:08 spapadim