HTTPretty
HTTPretty copied to clipboard
Bug in matcher priority - plain URL overrides query string
In my understanding and opinion the attached file shows a bug.
What do I do? extract and run the file
What happens?
- An exception "we should never get here"
What did I expect to happen?
- the lower priority matcher should be overridden by the higher priority one
- the program should run and put out "a okay"
import httpretty
import requests
def bad_request(a,b,c):
raise Exception("we should never get here")
url="http://example.com/a"
query="?ab=cd"
httpretty.HTTPretty.allow_net_connect = False
httpretty.enable()
httpretty.register_uri(
httpretty.GET, url + query,
match_querystring=True, status=200, body="a okay",
priority=5)
if True:
httpretty.register_uri(
httpretty.GET, url,
match_querystring=False, status=200, body=bad_request,
priority=0
)
r=requests.get(url + query)
print (r.text)
Simply changing True to False makes the program run as expected so the first matcher clearly works.
Hi, I made a fix in my fork make unit functional
is passing without errors. For testing I used next code
import httpretty
import requests
def bad_request(a, b, c):
raise Exception("we should never get here")
url = "http://example.com/a"
query = "?ab=cd"
query2 = '?ab=zd'
query_notreg = '?bird=duck'
httpretty.HTTPretty.allow_net_connect = False
httpretty.enable()
httpretty.register_uri(
httpretty.GET, url + query,
match_querystring=True, status=200, body="a okay",
priority=5)
httpretty.register_uri(
httpretty.GET, url + query2,
match_querystring=True, status=200, body="BEES!!!",
priority=5)
httpretty.register_uri(
httpretty.GET, url + query_notreg,
match_querystring=False, status=200, body="quack!",
priority=5)
httpretty.register_uri(httpretty.GET, "http://example.com/b", body='b like a bee')
if True:
httpretty.register_uri(httpretty.GET, url, match_querystring=False, status=200, body=bad_request, priority=0)
r = requests.get(url + query2)
print (r.text) # print "BEES!!!"
r = requests.get(url + query)
print r.text # print "a okay"
try:
r = requests.get(url + query_notreg) # should fail
print r.text
except requests.exceptions.ConnectionError:
print 'got exception'
I think this may be the problem I am encountering with httpretty==1.1.1
. I find that the example in the docs here does not work:
somename.py:29:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <sure.AssertionBuilder object at 0x7f14d8940550>, args = ({'success': True},), kw = {}
@wraps(func)
def wrapper(self, *args, **kw):
try:
value = func(self, *args, **kw)
except AssertionError as e:
> raise AssertionError(e)
E AssertionError: given
E X = {'success': False}
E and
E Y = {'success': True}
E X['success'] is False whereas Y['success'] is True
.venv/lib/python3.6/site-packages/sure/__init__.py:387: AssertionError
If I make this change:
- second_url = "http://foo-api.com/data?page=2"
+ second_url = "http://foo-api.com/data2?page=2"
Then it works. It appears that if the only difference between URLs is in the query string then the registered URLs are confused with one another.
(If this is a separate issue then let me know and I can create a ticket but I think the root cause is probably the same.)
... so the sample referenced in the previous comment:
- could be used to reproduce the problem and verify a fix (if it is the same problem)
- makes a reasonable new unit test which could be included with the fix (or ideally all samples could be verified automatically but one thing at a time)
I regret that I may not get around to verifying that it is the same problem myself but I hope that spelling this out might help an existing contributor do so,