diff --git a/README.md b/README.md index 85a157e..975e25d 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ JSONRPClib ========== This library is an implementation of the JSON-RPC specification. -It supports both the original 1.0 specification, as well as the +It supports both the original 1.0 specification, as well as the new (proposed) 2.0 spec, which includes batch submission, keyword arguments, etc. @@ -10,18 +10,18 @@ It is licensed under the Apache License, Version 2.0 Communication ------------- -Feel free to send any questions, comments, or patches to our Google Group -mailing list (you'll need to join to send a message): +Feel free to send any questions, comments, or patches to our Google Group +mailing list (you'll need to join to send a message): http://groups.google.com/group/jsonrpclib Summary ------- -This library implements the JSON-RPC 2.0 proposed specification in pure Python. -It is designed to be as compatible with the syntax of xmlrpclib as possible -(it extends where possible), so that projects using xmlrpclib could easily be +This library implements the JSON-RPC 2.0 proposed specification in pure Python. +It is designed to be as compatible with the syntax of xmlrpclib as possible +(it extends where possible), so that projects using xmlrpclib could easily be modified to use JSON and experiment with the differences. -It is backwards-compatible with the 1.0 specification, and supports all of the +It is backwards-compatible with the 1.0 specification, and supports all of the new proposed features of 2.0, including: * Batch submission (via MultiCall) @@ -29,19 +29,22 @@ new proposed features of 2.0, including: * Notifications (both in a batch and 'normal') * Class translation using the 'jsonclass' key. -I've added a "SimpleJSONRPCServer", which is intended to emulate the +I've added a "SimpleJSONRPCServer", which is intended to emulate the "SimpleXMLRPCServer" from the default Python distribution. Requirements ------------ -It supports cjson and simplejson, and looks for the parsers in that order -(searching first for cjson, then for the "built-in" simplejson as json in 2.6+, -and then the simplejson external library). One of these must be installed to -use this library, although if you have a standard distribution of 2.6+, you -should already have one. Keep in mind that cjson is supposed to be the -quickest, I believe, so if you are going for full-on optimization you may +It supports cjson and simplejson, and looks for the parsers in that order +(searching first for cjson, then for the "built-in" simplejson as json in 2.6+, +and then the simplejson external library). One of these must be installed to +use this library, although if you have a standard distribution of 2.6+, you +should already have one. Keep in mind that cjson is supposed to be the +quickest, I believe, so if you are going for full-on optimization you may want to pick it up. +Since library uses `contextlib` module You should have at least Python 2.5 +installed. + Installation ------------ You can install this from PyPI with one of the following commands (sudo @@ -86,8 +89,8 @@ This is (obviously) taken from a console session. {'key': 'value'} # Note that there are only two responses -- this is according to spec. -If you need 1.0 functionality, there are a bunch of places you can pass that -in, although the best is just to change the value on +If you need 1.0 functionality, there are a bunch of places you can pass that +in, although the best is just to change the value on jsonrpclib.config.version: >>> import jsonrpclib @@ -101,18 +104,37 @@ jsonrpclib.config.version: {"params": [7, 10], "id": "thes7tl2", "method": "add"} >>> print jsonrpclib.history.response {'id': 'thes7tl2', 'result': 17, 'error': None} - >>> + >>> -The equivalent loads and dumps functions also exist, although with minor -modifications. The dumps arguments are almost identical, but it adds three -arguments: rpcid for the 'id' key, version to specify the JSON-RPC -compatibility, and notify if it's a request that you want to be a -notification. +The equivalent loads and dumps functions also exist, although with minor +modifications. The dumps arguments are almost identical, but it adds three +arguments: rpcid for the 'id' key, version to specify the JSON-RPC +compatibility, and notify if it's a request that you want to be a +notification. -Additionally, the loads method does not return the params and method like -xmlrpclib, but instead a.) parses for errors, raising ProtocolErrors, and +Additionally, the loads method does not return the params and method like +xmlrpclib, but instead a.) parses for errors, raising ProtocolErrors, and b.) returns the entire structure of the request / response for manual parsing. +### Additional headers + +If your remote service requires custom headers in request, you can pass them +as as a `headers` keyword argument, when creating the `Server`: + + >>> import jsonrpclib + >>> server = jsonrpclib.Server("http://localhost:8080", headers={'X-Test' : 'Test'}) + +You can also put additional request headers only for certain method invocation: + + >>> import jsonrpclib + >>> server = jsonrpclib.Server("http://localhost:8080") + >>> with server._additional_headers({'X-Test' : 'Test'}) as test_server: + ... test_server.ping() + ... + >>> # X-Test header will be no longer sent in requests + +Of course `_additional_headers` contexts can be nested as well. + SimpleJSONRPCServer ------------------- This is identical in usage (or should be) to the SimpleXMLRPCServer in the default Python install. Some of the differences in features are that it obviously supports notification, batch calls, class translation (if left on), etc. Note: The import line is slightly different from the regular SimpleXMLRPCServer, since the SimpleJSONRPCServer is distributed within the jsonrpclib library. @@ -127,8 +149,8 @@ This is identical in usage (or should be) to the SimpleXMLRPCServer in the defau Class Translation ----------------- -I've recently added "automatic" class translation support, although it is -turned off by default. This can be devastatingly slow if improperly used, so +I've recently added "automatic" class translation support, although it is +turned off by default. This can be devastatingly slow if improperly used, so the following is just a short list of things to keep in mind when using it. * Keep It (the object) Simple Stupid. (for exceptions, keep reading.) @@ -136,18 +158,18 @@ the following is just a short list of things to keep in mind when using it. * Getter properties without setters could be dangerous (read: not tested) If any of the above are issues, use the _serialize method. (see usage below) -The server and client must BOTH have use_jsonclass configuration item on and -they must both have access to the same libraries used by the objects for +The server and client must BOTH have use_jsonclass configuration item on and +they must both have access to the same libraries used by the objects for this to work. -If you have excessively nested arguments, it would be better to turn off the -translation and manually invoke it on specific objects using -jsonrpclib.jsonclass.dump / jsonrpclib.jsonclass.load (since the default +If you have excessively nested arguments, it would be better to turn off the +translation and manually invoke it on specific objects using +jsonrpclib.jsonclass.dump / jsonrpclib.jsonclass.load (since the default behavior recursively goes through attributes and lists / dicts / tuples). [test_obj.py] - # This object is /very/ simple, and the system will look through the + # This object is /very/ simple, and the system will look through the # attributes and serialize what it can. class TestObj(object): foo = 'bar' @@ -179,13 +201,13 @@ behavior recursively goes through attributes and lists / dicts / tuples). # {"jsonrpc": "2.0", "params": [{"__jsonclass__": ["test_obj.TestSerial", ["foo"]]}], "id": "a0l976iv", "method": "ping"} print jsonrpclib.history.result # {'jsonrpc': '2.0', 'result': , 'id': 'a0l976iv'} - -To turn on this behaviour, just set jsonrpclib.config.use_jsonclass to True. -If you want to use a different method for serialization, just set -jsonrpclib.config.serialize_method to the method name. Finally, if you are -using classes that you have defined in the implementation (as in, not a -separate library), you'll need to add those (on BOTH the server and the -client) using the jsonrpclib.config.classes.add() method. + +To turn on this behaviour, just set jsonrpclib.config.use_jsonclass to True. +If you want to use a different method for serialization, just set +jsonrpclib.config.serialize_method to the method name. Finally, if you are +using classes that you have defined in the implementation (as in, not a +separate library), you'll need to add those (on BOTH the server and the +client) using the jsonrpclib.config.classes.add() method. (Examples forthcoming.) Feedback on this "feature" is very, VERY much appreciated. @@ -199,7 +221,7 @@ In my opinion, there are several reasons to choose JSON over XML for RPC: * Parsing - JSON should be much quicker to parse than XML. * Easy class passing with jsonclass (when enabled) -In the interest of being fair, there are also a few reasons to choose XML +In the interest of being fair, there are also a few reasons to choose XML over JSON: * Your server doesn't do JSON (rather obvious) diff --git a/jsonrpclib/jsonrpc.py b/jsonrpclib/jsonrpc.py index e11939a..d7ec6e2 100644 --- a/jsonrpclib/jsonrpc.py +++ b/jsonrpclib/jsonrpc.py @@ -1,15 +1,15 @@ """ -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 + http://www.apache.org/licenses/LICENSE-2.0 -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. ============================ JSONRPC Library (jsonrpclib) @@ -29,7 +29,7 @@ and other things to tie the thing off nicely. :) For a quick-start, just open a console and type the following, -replacing the server address, method, and parameters +replacing the server address, method, and parameters appropriately. >>> import jsonrpclib >>> server = jsonrpclib.Server('http://localhost:8181') @@ -55,6 +55,7 @@ import time import string import random +import contextlib # Library includes import jsonrpclib @@ -81,8 +82,8 @@ IDCHARS = string.ascii_lowercase+string.digits class UnixSocketMissing(Exception): - """ - Just a properly named Exception if Unix Sockets usage is + """ + Just a properly named Exception if Unix Sockets usage is attempted on a platform that doesn't support them (Windows) """ pass @@ -116,8 +117,36 @@ class TransportMixIn(object): # for Python 2.7 support _connection = None + additional_headers = [] + + def push_headers(self, headers): + self.additional_headers.append(headers) + + def pop_headers(self, headers): + """ Also validates that given headers are on top of the stack """ + assert self.additional_headers[-1] == headers + self.additional_headers.pop() + + def emit_additional_headers(self, connection): + """ + Compacts additional headers stack into one dictionary and + puts headers into connection. + + NOTE: additional_headers should have at least one entry here + """ + additional_headers = {} + for headers in self.additional_headers: + additional_headers.update(headers) + + for header, value in additional_headers.iteritems(): + connection.putheader(header, str(value)) + def send_content(self, connection, request_body): connection.putheader("Content-Type", "application/json-rpc") + + # Emit additional headers here in order not to override content-length + self.emit_additional_headers(connection) + connection.putheader("Content-Length", str(len(request_body))) connection.endheaders() if request_body: @@ -157,14 +186,14 @@ class SafeTransport(TransportMixIn, XMLSafeTransport): USE_UNIX_SOCKETS = False -try: +try: from socket import AF_UNIX, SOCK_STREAM USE_UNIX_SOCKETS = True except ImportError: pass - + if (USE_UNIX_SOCKETS): - + class UnixHTTPConnection(HTTPConnection): def connect(self): self.sock = socket(AF_UNIX, SOCK_STREAM) @@ -179,15 +208,16 @@ def make_connection(self, host): host, extra_headers, x509 = self.get_host_info(host) return UnixHTTP(host) - + class ServerProxy(XMLServerProxy): """ Unfortunately, much more of this class has to be copied since so much of it does the serialization. """ - def __init__(self, uri, transport=None, encoding=None, - verbose=0, version=None): + def __init__(self, uri, transport=None, encoding=None, + verbose=0, version=None, headers=None): + import urllib if not version: version = config.version @@ -218,6 +248,9 @@ def __init__(self, uri, transport=None, encoding=None, self.__encoding = encoding self.__verbose = verbose + # Global custom headers are injected into Transport + self.__transport.push_headers(headers or {}) + def _request(self, methodname, params, rpcid=None): request = dumps(params, methodname, encoding=self.__encoding, rpcid=rpcid, version=self.__version) @@ -241,13 +274,13 @@ def _run_request(self, request, notify=None): request, verbose=self.__verbose ) - + # Here, the XMLRPC library translates a single list # response to the single value -- should we do the # same, and require a tuple / list to be passed to - # the response object, or expect the Server to be + # the response object, or expect the Server to be # outputting the response appropriately? - + history.add_response(response) if not response: return None @@ -263,9 +296,24 @@ def _notify(self): # Just like __getattr__, but with notify namespace. return _Notify(self._request_notify) + @contextlib.contextmanager + def _additional_headers(self, headers): + """ + Allow to specify additional headers, to be added inside the with + block. Example of usage: + + >>> with client._additional_headers({'X-Test' : 'Test'}) as new_client: + ... new_client.method() + ... + >>> # Here old headers are restored + """ + self.__transport.push_headers(headers) + yield self + self.__transport.pop_headers(headers) + class _Method(XML_Method): - + def __call__(self, *args, **kwargs): if len(args) > 0 and len(kwargs) > 0: raise ProtocolError('Cannot use both positional ' + @@ -288,11 +336,11 @@ def __init__(self, request): def __getattr__(self, name): return _Method(self._request, name) - + # Batch implementation class MultiCallMethod(object): - + def __init__(self, method, notify=False): self.method = method self.params = [] @@ -313,14 +361,14 @@ def request(self, encoding=None, rpcid=None): def __repr__(self): return '%s' % self.request() - + def __getattr__(self, method): new_method = '%s.%s' % (self.method, method) self.method = new_method return self class MultiCallNotify(object): - + def __init__(self, multicall): self.multicall = multicall @@ -330,7 +378,7 @@ def __getattr__(self, name): return new_job class MultiCallIterator(object): - + def __init__(self, results): self.results = results @@ -348,7 +396,7 @@ def __len__(self): return len(self.results) class MultiCall(object): - + def __init__(self, server): self._server = server self._job_list = [] @@ -376,7 +424,7 @@ def __getattr__(self, name): __call__ = _request -# These lines conform to xmlrpclib's "compatibility" line. +# These lines conform to xmlrpclib's "compatibility" line. # Not really sure if we should include these, but oh well. Server = ServerProxy @@ -414,7 +462,7 @@ def __init__(self, rpcid=None, version=None): version = config.version self.id = rpcid self.version = float(version) - + def request(self, method, params=[]): if type(method) not in types.StringTypes: raise ValueError('Method name must be a string.') @@ -452,10 +500,10 @@ def error(self, code=-32000, message='Server error.'): error['error'] = {'code':code, 'message':message} return error -def dumps(params=[], methodname=None, methodresponse=None, +def dumps(params=[], methodname=None, methodresponse=None, encoding=None, rpcid=None, version=None, notify=None): """ - This differs from the Python implementation in that it implements + This differs from the Python implementation in that it implements the rpcid argument since the 2.0 spec requires it for responses. """ if not version: @@ -464,7 +512,7 @@ def dumps(params=[], methodname=None, methodresponse=None, if methodname in types.StringTypes and \ type(params) not in valid_params and \ not isinstance(params, Fault): - """ + """ If a method, and params are not in a listish or a Fault, error out. """ @@ -505,7 +553,7 @@ def loads(data): # notification return None result = jloads(data) - # if the above raises an error, the implementing server code + # if the above raises an error, the implementing server code # should return something like the following: # { 'jsonrpc':'2.0', 'error': fault.error(), id: None } if config.use_jsonclass == True: diff --git a/setup.py b/setup.py index 49d30ec..809c164 100644 --- a/setup.py +++ b/setup.py @@ -1,23 +1,23 @@ #!/usr/bin/env/python """ -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 + http://www.apache.org/licenses/LICENSE-2.0 -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. """ import distutils.core distutils.core.setup( name = "jsonrpclib", - version = "0.1.3", + version = "0.1.4", packages = ["jsonrpclib"], author = "Josh Marshall", author_email = "catchjosh@gmail.com", diff --git a/tests.py b/tests.py index 3ce1009..2343884 100644 --- a/tests.py +++ b/tests.py @@ -1,16 +1,16 @@ """ The tests in this file compare the request and response objects to the JSON-RPC 2.0 specification document, as well as testing -several internal components of the jsonrpclib library. Run this +several internal components of the jsonrpclib library. Run this module without any parameters to run the tests. -Currently, this is not easily tested with a framework like +Currently, this is not easily tested with a framework like nosetests because we spin up a daemon thread running the the Server, and nosetests (at least in my tests) does not ever "kill" the thread. If you are testing jsonrpclib and the module doesn't return to -the command prompt after running the tests, you can hit +the command prompt after running the tests, you can hit "Ctrl-C" (or "Ctrl-Break" on Windows) and that should kill it. TODO: @@ -27,7 +27,13 @@ import tempfile import unittest import os +import re import time + +import contextlib +import StringIO +import string, sys + try: import json except ImportError: @@ -37,18 +43,18 @@ PORTS = range(8000, 8999) class TestCompatibility(unittest.TestCase): - + client = None port = None server = None - + def setUp(self): self.port = PORTS.pop() self.server = server_set_up(addr=('', self.port)) self.client = Server('http://localhost:%d' % self.port) - + # v1 tests forthcoming - + # Version 2.0 Tests def test_positional(self): """ Positional arguments in a single call """ @@ -59,7 +65,7 @@ def test_positional(self): request = json.loads(history.request) response = json.loads(history.response) verify_request = { - "jsonrpc": "2.0", "method": "subtract", + "jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": request['id'] } verify_response = { @@ -67,7 +73,7 @@ def test_positional(self): } self.assertTrue(request == verify_request) self.assertTrue(response == verify_response) - + def test_named(self): """ Named arguments in a single call """ result = self.client.subtract(subtrahend=23, minuend=42) @@ -77,8 +83,8 @@ def test_named(self): request = json.loads(history.request) response = json.loads(history.response) verify_request = { - "jsonrpc": "2.0", "method": "subtract", - "params": {"subtrahend": 23, "minuend": 42}, + "jsonrpc": "2.0", "method": "subtract", + "params": {"subtrahend": 23, "minuend": 42}, "id": request['id'] } verify_response = { @@ -86,7 +92,7 @@ def test_named(self): } self.assertTrue(request == verify_request) self.assertTrue(response == verify_response) - + def test_notification(self): """ Testing a notification (response should be null) """ result = self.client._notify.update(1, 2, 3, 4, 5) @@ -99,7 +105,7 @@ def test_notification(self): verify_response = '' self.assertTrue(request == verify_request) self.assertTrue(response == verify_response) - + def test_non_existent_method(self): self.assertRaises(ProtocolError, self.client.foobar) request = json.loads(history.request) @@ -108,14 +114,14 @@ def test_non_existent_method(self): "jsonrpc": "2.0", "method": "foobar", "id": request['id'] } verify_response = { - "jsonrpc": "2.0", - "error": - {"code": -32601, "message": response['error']['message']}, + "jsonrpc": "2.0", + "error": + {"code": -32601, "message": response['error']['message']}, "id": request['id'] } self.assertTrue(request == verify_request) self.assertTrue(response == verify_response) - + def test_invalid_json(self): invalid_json = '{"jsonrpc": "2.0", "method": "foobar, '+ \ '"params": "bar", "baz]' @@ -127,7 +133,7 @@ def test_invalid_json(self): ) verify_response['error']['message'] = response['error']['message'] self.assertTrue(response == verify_response) - + def test_invalid_request(self): invalid_request = '{"jsonrpc": "2.0", "method": 1, "params": "bar"}' response = self.client._run_request(invalid_request) @@ -138,7 +144,7 @@ def test_invalid_request(self): ) verify_response['error']['message'] = response['error']['message'] self.assertTrue(response == verify_response) - + def test_batch_invalid_json(self): invalid_request = '[ {"jsonrpc": "2.0", "method": "sum", '+ \ '"params": [1,2,4], "id": "1"},{"jsonrpc": "2.0", "method" ]' @@ -150,7 +156,7 @@ def test_batch_invalid_json(self): ) verify_response['error']['message'] = response['error']['message'] self.assertTrue(response == verify_response) - + def test_empty_array(self): invalid_request = '[]' response = self.client._run_request(invalid_request) @@ -161,7 +167,7 @@ def test_empty_array(self): ) verify_response['error']['message'] = response['error']['message'] self.assertTrue(response == verify_response) - + def test_nonempty_array(self): invalid_request = '[1,2]' request_obj = json.loads(invalid_request) @@ -175,7 +181,7 @@ def test_nonempty_array(self): ) verify_resp['error']['message'] = resp['error']['message'] self.assertTrue(resp == verify_resp) - + def test_batch(self): multicall = MultiCall(self.client) multicall.sum(1,2,4) @@ -188,16 +194,16 @@ def test_batch(self): json_requests = '[%s]' % ','.join(job_requests) requests = json.loads(json_requests) responses = self.client._run_request(json_requests) - + verify_requests = json.loads("""[ {"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"}, {"jsonrpc": "2.0", "method": "notify_hello", "params": [7]}, {"jsonrpc": "2.0", "method": "subtract", "params": [42,23], "id": "2"}, {"foo": "boo"}, {"jsonrpc": "2.0", "method": "foo.get", "params": {"name": "myself"}, "id": "5"}, - {"jsonrpc": "2.0", "method": "get_data", "id": "9"} + {"jsonrpc": "2.0", "method": "get_data", "id": "9"} ]""") - + # Thankfully, these are in order so testing is pretty simple. verify_responses = json.loads("""[ {"jsonrpc": "2.0", "result": 7, "id": "1"}, @@ -206,13 +212,13 @@ def test_batch(self): {"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found."}, "id": "5"}, {"jsonrpc": "2.0", "result": ["hello", 5], "id": "9"} ]""") - + self.assertTrue(len(requests) == len(verify_requests)) self.assertTrue(len(responses) == len(verify_responses)) - + responses_by_id = {} response_i = 0 - + for i in range(len(requests)): verify_request = verify_requests[i] request = requests[i] @@ -227,15 +233,15 @@ def test_batch(self): response_i += 1 response = verify_response self.assertTrue(request == verify_request) - + for response in responses: verify_response = responses_by_id.get(response.get('id')) if verify_response.has_key('error'): verify_response['error']['message'] = \ response['error']['message'] self.assertTrue(response == verify_response) - - def test_batch_notifications(self): + + def test_batch_notifications(self): multicall = MultiCall(self.client) multicall._notify.notify_sum(1, 2, 4) multicall._notify.notify_hello(7) @@ -253,23 +259,23 @@ def test_batch_notifications(self): valid_req = valid_request[i] self.assertTrue(req == valid_req) self.assertTrue(history.response == '') - + class InternalTests(unittest.TestCase): - """ - These tests verify that the client and server portions of + """ + These tests verify that the client and server portions of jsonrpclib talk to each other properly. - """ + """ client = None server = None port = None - + def setUp(self): self.port = PORTS.pop() self.server = server_set_up(addr=('', self.port)) - + def get_client(self): return Server('http://localhost:%d' % self.port) - + def get_multicall_client(self): server = self.get_client() return MultiCall(server) @@ -278,33 +284,33 @@ def test_connect(self): client = self.get_client() result = client.ping() self.assertTrue(result) - + def test_single_args(self): client = self.get_client() result = client.add(5, 10) self.assertTrue(result == 15) - + def test_single_kwargs(self): client = self.get_client() result = client.add(x=5, y=10) self.assertTrue(result == 15) - + def test_single_kwargs_and_args(self): client = self.get_client() self.assertRaises(ProtocolError, client.add, (5,), {'y':10}) - + def test_single_notify(self): client = self.get_client() result = client._notify.add(5, 10) self.assertTrue(result == None) - + def test_single_namespace(self): client = self.get_client() response = client.namespace.sum(1,2,4) request = json.loads(history.request) response = json.loads(history.response) verify_request = { - "jsonrpc": "2.0", "params": [1, 2, 4], + "jsonrpc": "2.0", "params": [1, 2, 4], "id": "5", "method": "namespace.sum" } verify_response = { @@ -314,7 +320,7 @@ def test_single_namespace(self): verify_response['id'] = request['id'] self.assertTrue(verify_request == request) self.assertTrue(verify_response == response) - + def test_multicall_success(self): multicall = self.get_multicall_client() multicall.ping() @@ -325,14 +331,14 @@ def test_multicall_success(self): for result in multicall(): self.assertTrue(result == correct[i]) i += 1 - + def test_multicall_success(self): multicall = self.get_multicall_client() for i in range(3): multicall.add(5, i) result = multicall() self.assertTrue(result[2] == 7) - + def test_multicall_failure(self): multicall = self.get_multicall_client() multicall.ping() @@ -346,51 +352,51 @@ def test_multicall_failure(self): def func(): return result[i] self.assertRaises(raises[i], func) - - + + if jsonrpc.USE_UNIX_SOCKETS: # We won't do these tests unless Unix Sockets are supported - + class UnixSocketInternalTests(InternalTests): """ - These tests run the same internal communication tests, + These tests run the same internal communication tests, but over a Unix socket instead of a TCP socket. """ def setUp(self): suffix = "%d.sock" % PORTS.pop() - - # Open to safer, alternative processes + + # Open to safer, alternative processes # for getting a temp file name... temp = tempfile.NamedTemporaryFile( suffix=suffix ) self.port = temp.name temp.close() - + self.server = server_set_up( - addr=self.port, + addr=self.port, address_family=socket.AF_UNIX ) def get_client(self): - return Server('unix:/%s' % self.port) - + return Server('unix:/%s' % self.port, verbose=1) + def tearDown(self): """ Removes the tempory socket file """ os.unlink(self.port) - + class UnixSocketErrorTests(unittest.TestCase): - """ - Simply tests that the proper exceptions fire if + """ + Simply tests that the proper exceptions fire if Unix sockets are attempted to be used on a platform that doesn't support them. """ - + def setUp(self): self.original_value = jsonrpc.USE_UNIX_SOCKETS if (jsonrpc.USE_UNIX_SOCKETS): jsonrpc.USE_UNIX_SOCKETS = False - + def test_client(self): address = "unix://shouldnt/work.sock" self.assertRaises( @@ -398,34 +404,232 @@ def test_client(self): Server, address ) - + def tearDown(self): jsonrpc.USE_UNIX_SOCKETS = self.original_value - + +class HeadersTests(unittest.TestCase): + """ + These tests verify functionality of additional headers. + """ + client = None + server = None + port = None + + REQUEST_LINE = "^send: POST" + + def setUp(self): + self.port = PORTS.pop() + self.server = server_set_up(addr=('', self.port)) + + @contextlib.contextmanager + def captured_headers(self): + """ + Captures the request headers. Yields the {header : value} dict, + where keys are in lowercase. + """ + stdout = sys.stdout + sys.stdout = f = StringIO.StringIO() + headers = {} + yield headers + sys.stdout = stdout + + request_lines = f.getvalue().splitlines() + request_lines = filter(lambda l: l.startswith("send:"), request_lines) + request_line = request_lines[0] + + try: + request_line = eval(request_line.split("send: ")[-1]) + except: + request_line = str(request_line) + + raw_headers = request_line.splitlines()[1:-1] + raw_headers = map(lambda h: re.split(":\s?", h, 1), raw_headers) + for header, value in raw_headers: + headers[header.lower()] = value + + def test_should_extract_headers(self): + # given + client = Server('http://localhost:%d' % self.port, verbose=1) + + # when + with self.captured_headers() as headers: + response = client.ping() + self.assertTrue(response) + + # then + self.assertTrue(len(headers) > 0) + self.assertTrue('content-type' in headers) + self.assertEqual(headers['content-type'], 'application/json-rpc') + + def test_should_add_additional_headers(self): + # given + client = Server('http://localhost:%d' % self.port, verbose=1, + headers={'X-My-Header' : 'Test'}) + + # when + with self.captured_headers() as headers: + response = client.ping() + self.assertTrue(response) + + # then + self.assertTrue('x-my-header' in headers) + self.assertEqual(headers['x-my-header'], 'Test') + + def test_should_add_additional_headers_to_notifications(self): + # given + client = Server('http://localhost:%d' % self.port, verbose=1, + headers={'X-My-Header' : 'Test'}) + + # when + with self.captured_headers() as headers: + client._notify.ping() + + # then + self.assertTrue('x-my-header' in headers) + self.assertEqual(headers['x-my-header'], 'Test') + + def test_should_override_headers(self): + # given + client = Server('http://localhost:%d' % self.port, verbose=1, + headers={ + 'User-Agent' : 'jsonrpclib test', + 'Host' : 'example.com' + }) + + # when + with self.captured_headers() as headers: + response = client.ping() + self.assertTrue(response) + + # then + self.assertEqual(headers['user-agent'], 'jsonrpclib test') + self.assertEqual(headers['host'], 'example.com') + + def test_should_not_override_content_length(self): + # given + client = Server('http://localhost:%d' % self.port, verbose=1, + headers={'Content-Length' : 'invalid value'}) + + # when + with self.captured_headers() as headers: + response = client.ping() + self.assertTrue(response) + + # then + self.assertTrue('content-length' in headers) + self.assertNotEqual(headers['content-length'], 'invalid value') + + def test_should_convert_header_values_to_basestring(self): + # given + client = Server('http://localhost:%d' % self.port, verbose=1, + headers={'X-Test' : 123}) + + # when + with self.captured_headers() as headers: + response = client.ping() + self.assertTrue(response) + + # then + self.assertTrue('x-test' in headers) + self.assertEqual(headers['x-test'], '123') + + def test_should_add_custom_headers_to_methods(self): + # given + client = Server('http://localhost:%d' % self.port, verbose=1) + + # when + with self.captured_headers() as headers: + with client._additional_headers({'X-Method' : 'Method'}) as cl: + response = cl.ping() + + self.assertTrue(response) + + # then + self.assertTrue('x-method' in headers) + self.assertEqual(headers['x-method'], 'Method') + + def test_should_override_global_headers(self): + # given + client = Server('http://localhost:%d' % self.port, verbose=1, + headers={'X-Test' : 'Global'}) + + # when + with self.captured_headers() as headers: + with client._additional_headers({'X-Test' : 'Method'}) as cl: + response = cl.ping() + self.assertTrue(response) + + # then + self.assertTrue('x-test' in headers) + self.assertEqual(headers['x-test'], 'Method') + + def test_should_restore_global_headers(self): + # given + client = Server('http://localhost:%d' % self.port, verbose=1, + headers={'X-Test' : 'Global'}) + + # when + with self.captured_headers() as headers: + with client._additional_headers({'X-Test' : 'Method'}) as cl: + response = cl.ping() + self.assertTrue(response) + + with self.captured_headers() as headers: + response = cl.ping() + self.assertTrue(response) + + # then + self.assertTrue('x-test' in headers) + self.assertEqual(headers['x-test'], 'Global') + + def test_should_allow_to_nest_additional_header_blocks(self): + # given + client = Server('http://localhost:%d' % self.port, verbose=1) + + # when + with client._additional_headers({'X-Level-1' : '1'}) as cl_level1: + with self.captured_headers() as headers1: + response = cl_level1.ping() + self.assertTrue(response) + + with cl_level1._additional_headers({'X-Level-2' : '2'}) as cl: + with self.captured_headers() as headers2: + response = cl.ping() + self.assertTrue(response) + + # then + self.assertTrue('x-level-1' in headers1) + self.assertEqual(headers1['x-level-1'], '1') + + self.assertTrue('x-level-1' in headers2) + self.assertEqual(headers1['x-level-1'], '1') + self.assertTrue('x-level-2' in headers2) + self.assertEqual(headers2['x-level-2'], '2') """ Test Methods """ def subtract(minuend, subtrahend): """ Using the keywords from the JSON-RPC v2 doc """ return minuend-subtrahend - + def add(x, y): return x + y - + def update(*args): return args - + def summation(*args): return sum(args) - + def notify_hello(*args): return args - + def get_data(): return ['hello', 5] - + def ping(): return True - + def server_set_up(addr, address_family=socket.AF_INET): # Not sure this is a good idea to spin up a new server thread # for each test... but it seems to work fine. @@ -452,5 +656,5 @@ def log_request(self, *args, **kwargs): print "===============================================================" print " NOTE: There may be threading exceptions after tests finish. " print "===============================================================" - time.sleep(2) + time.sleep(1) unittest.main()