From fb27b731a06433569241661086d02b5399632991 Mon Sep 17 00:00:00 2001 From: mission-liao Date: Tue, 16 Feb 2016 16:34:15 +0800 Subject: [PATCH] allowing passing option to send MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - https://github.com/mission-liao/pyswagger/issues/60 - it’s helpful to test server before production. --- pyswagger/contrib/client/requests.py | 10 +++++--- .../tests/contrib/client/test_requests.py | 24 +++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/pyswagger/contrib/client/requests.py b/pyswagger/contrib/client/requests.py index e19e55b..d6753af 100644 --- a/pyswagger/contrib/client/requests.py +++ b/pyswagger/contrib/client/requests.py @@ -10,11 +10,15 @@ class Client(BaseClient): __schemes__ = set(['http', 'https']) - def __init__(self, auth=None): - """ + def __init__(self, auth=None, send_opt={}): + """ constructor + + :param auth pyswagger.SwaggerAuth: auth info used when requesting + :param send_opt dict: options used in requests.send, ex verify=False """ super(Client, self).__init__(auth) self.__s = Session() + self.__send_opt = send_opt def request(self, req_and_resp, opt={}): """ @@ -43,7 +47,7 @@ def request(self, req_and_resp, opt={}): files=file_obj ) rq = self.__s.prepare_request(rq) - rs = self.__s.send(rq, stream=True) + rs = self.__s.send(rq, stream=True, **self.__send_opt) resp.apply_with( status=rs.status_code, diff --git a/pyswagger/tests/contrib/client/test_requests.py b/pyswagger/tests/contrib/client/test_requests.py index 9b746ba..f1a1bb2 100644 --- a/pyswagger/tests/contrib/client/test_requests.py +++ b/pyswagger/tests/contrib/client/test_requests.py @@ -170,3 +170,27 @@ def test_uploadFile(self): self.assertTrue(body.find('a test Content') != -1) self.assertTrue(body.find('filename="test.txt"') != -1) +@pytest.mark.skipif(sys.version_info[:2] >= (3, 3), reason='httpretty corrupt in python3') +@httpretty.activate +class RequestsOptTestCase(unittest.TestCase): + """ make sure that passing options to requests won't fail """ + + def test_verify(self): + """ option for send: verify """ + client = Client(send_opt=dict(verify=False)) + + # testing this client with getPetById + httpretty.register_uri(httpretty.GET, 'http://petstore.swagger.wordnik.com/api/pet/1', + status=200, + content_type='application/json', + body=json.dumps(pet_Tom) + ) + + resp = client.request(app.op['getPetById'](petId=1)) + + self.assertEqual(resp.status, 200) + self.assertTrue(isinstance(resp.data, Model)) + self.assertEqual(resp.data, + {u'name': 'Tom', u'tags': [{u'id': 0, u'name': 'available'}, {u'id': 1, u'name': 'sold'}], u'id': 1} + ) +