Skip to content

Commit

Permalink
allowing passing option to send
Browse files Browse the repository at this point in the history
- #60
- it’s helpful to test server before production.
  • Loading branch information
mission-liao committed Feb 16, 2016
1 parent 25e81a7 commit fb27b73
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
10 changes: 7 additions & 3 deletions pyswagger/contrib/client/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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={}):
"""
Expand Down Expand Up @@ -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,
Expand Down
24 changes: 24 additions & 0 deletions pyswagger/tests/contrib/client/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
)

0 comments on commit fb27b73

Please sign in to comment.