getsentry / responses

A utility for mocking out the Python Requests library.
Apache License 2.0
4.15k stars 354 forks source link

Problems with url's that contain '?' #57

Closed victorevector closed 9 years ago

victorevector commented 9 years ago

Hi everybody,

I've written up a test that only fails when a '?' is present in the url. The url in question (and used by both Requests and Responses) is "https://api.23andme.com/1/demo/genotypes/c4480ba411939067/?locations=rs3094315". I've ran the test twice. Once with the '?' available to both requests and responses and another with the '?' omitted from the url. The former failed whereas the latter passed.

Any insight into what the problem may be? I have pasted both the test method and the interpreter's error message below. One thing I've noticed in the error message is that Responses's '_find_match' method isn't being called. I'm not sure if that's of any importance...

Test method in question:

@responses.activate
def test_get_genotype_with_valid_profile_id_and_valid_locations_as_method_arguments(self):
    json_response = '{ "i3000001": "II", "rs3094315": "AA", "id": "c4480ba411939067"}'
    profile_id = "c4480ba411939067"
    locations = "rs3094315"
    url = "https://api.23andme.com/1/demo/genotypes/{}/?locations={}".format(profile_id, locations) 
    responses.add(responses.GET,
        url,
        body = json_response,
        content_type ='application/json',
        status = 200
        )
    client = Client._23AndMeClient('89822b93d2')
    expected_json_response = json.loads(json_response)
    self.assertEqual(client.get_genotype(profile_id= profile_id, locations=locations), expected_json_response ) 

Error Message:

ERROR: test_get_genotype_with_valid_profile_id_and_valid_locations_as_method_arguments (api.tests.ClientTestCase)
Traceback (most recent call last):
File "<string>", line 2, in _wrapper_
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/responses.py", line 167, in wrapped
return func(*args, **kwargs)
File "/Users/victorestebanfimbres/django_workspace/andMe/api/tests.py", line 89, in test_get_genotype_with_valid_profile_id_and_valid_locations_as_method_arguments
self.assertEqual(client.get_genotype(profile_id= profile_id, locations=locations), expected_json_response )
File "/Users/victorestebanfimbres/django_workspace/andMe/api/Client.py", line 59, in get_genotype
return self._get_resource('demo/genotypes/{}/?locations={}'.format(profile_id, locations) )
File "/Users/victorestebanfimbres/django_workspace/andMe/api/Client.py", line 50, in _get_resource
response = requests.get(url, headers= headers, verify = False)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/api.py", line 55, in get
return request('get', url, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/api.py", line 44, in request
return session.request(method=method, url=url, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/sessions.py", line 383, in request
resp = self.send(prep, **send_kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/responses.py", line 263, in unbound_on_send
return self._on_request(session, requests, *a, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/responses.py", line 219, in _on_request
raise response
ConnectionError: Connection refused: https://api.23andme.com/1/demo/genotypes/c4480ba411939067/?locations=rs3094315
freylis commented 9 years ago

U can set match_querystring to True in add method:

responses.add(responses.GET,
    url,
    body = json_response,
    content_type ='application/json',
    status = 200,
    match_querystring=True
)
victorevector commented 9 years ago

Thank you Freylis!

kiddten commented 8 years ago

It actually should be in docs somewhere..

zapplecat commented 8 years ago

After hours of trying to write tests and having completely random fails and passes, I also agree with @kiddick that this should be in the docs somewhere.