def test_headers_passthrough(self): """Test that we correctly send RFC-defined headers and merge them with user defined ones""" def callback(request): expected_headers = { 'Content-Type': 'application/json', 'Accept': 'application/json-rpc', 'X-TestCustomHeader': '1' } self.assertTrue(set(expected_headers.items()).issubset(set(request.headers.items()))) return 200, {}, u'{"jsonrpc": "2.0", "result": true, "id": 1}' responses.add_callback( responses.POST, 'http://mock/xmlrpc', content_type='application/json', callback=callback, ) s = Server('http://mock/xmlrpc', headers={'X-TestCustomHeader': '1'}) s.foo()
def main(): bionet = Server('http://localhost:3000/rpc') res = bionet.foo('foo') print "no stream: %s" % res res = bionet.bar() print "return stream: %s" % res res = bionet.baz('a') print "callback stream: %s" % res try: res = bionet.fail() except ProtocolError as err: print("got error: %s" % err.message) print("just the error message: %s" % err.server_data['error']['message']) try: res = bionet.secret() except ProtocolError as err: print("while calling .secret(): %s" % err.message) res = bionet.login('foo', 'bar') # note: your probably won't ever need to use this token manually print "logged in and got token: %s" % res res = bionet.secret() print "secret: %s" % res try: res = bionet.admin_secret() except ProtocolError as err: print("while calling .admin_secret(): %s" % err.message)
def test_exception_passthrough(self): with self.assertRaises(TransportError) as transport_error: s = Server('http://host-doesnt-exist') s.foo() self.assertEqual(transport_error.exception.args[0], "Error calling method 'foo'") self.assertIsInstance(transport_error.exception.args[1], requests.exceptions.RequestException)