diff --git a/.gitignore b/.gitignore index c88edd9..9c541d8 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ output.xml parts/ report.html src/robotframework_httplibrary.egg-info/ +/build/ diff --git a/README.rst b/README.rst index 42a10f3..f961b54 100644 --- a/README.rst +++ b/README.rst @@ -74,6 +74,24 @@ mostly a wrapper supposed to have a nice API)! Changelog --------- +**v0.4.7** +- Improvement: Added PATCH HTTP method + +**v0.4.6** +- Improvement: Added stringify parameter to json_value_should_equal and json_value_should_not_equal methods + +**v0.4.5** +- Fix: Keep original data type returned by 'Get Json Value' keyword + +**v0.4.4** +- Fix: 'Set Json Value' keyword. + https://github.com/vikulin/robotframework-httplibrary/pull/1 + +**v0.4.3** + +- Fix: Added explicit OPTIONS method + https://github.com/peritus/robotframework-httplibrary/issues/30 + **v0.4.2** - Don't enforce ASCII when converting to JSON (so chinese characters are diff --git a/setup.py b/setup.py index 42ef4c1..b688b97 100644 --- a/setup.py +++ b/setup.py @@ -12,19 +12,18 @@ setup( name='robotframework-httplibrary', - version="0.4.2", + version="0.4.7", description='Robot Framework keywords for HTTP requests', long_description=long_description, - author='Filip Noetzel', - author_email='filip+rfhttplibrary@j03.de', - url='https://github.com/peritus/robotframework-httplibrary', + author='Vadym Vikulin', + author_email='vadym.vikulin@gmail.com', + url='https://github.com/vikulin/robotframework-httplibrary', license='Beerware', - keywords='robotframework testing testautomation web http webtest', + keywords='robotframework testing test automation web http webtest', platforms='any', zip_safe=False, classifiers=CLASSIFIERS.splitlines(), package_dir={'': 'src'}, - install_requires=['robotframework', 'webtest>=2.0', 'jsonpatch', - 'jsonpointer'], + install_requires=['robotframework', 'webtest>=2.0', 'jsonpatch', 'jsonpointer'], packages=['HttpLibrary'] ) diff --git a/src/HttpLibrary/__init__.py b/src/HttpLibrary/__init__.py index ae11328..abaead8 100644 --- a/src/HttpLibrary/__init__.py +++ b/src/HttpLibrary/__init__.py @@ -35,7 +35,7 @@ class HTTP: Pointer, go to http://tools.ietf.org/html/draft-pbryan-zyp-json-pointer-00. """ - ROBOT_LIBRARY_VERSION = "0.4.2" + ROBOT_LIBRARY_VERSION = "0.4.7" class Context(object): def __init__(self, http, host=None, scheme='http'): @@ -281,6 +281,39 @@ def DELETE(self, url): self.app.delete(path, {}, self.context.request_headers) ) + def OPTIONS(self, url): + """ + Issues a HTTP OPTIONS request. + + `url` is the URL relative to the server root, e.g. '/_utils/config.html' + """ + path = self._path_from_url_or_path(url) + self.context.pre_process_request() + logger.debug("Performing OPTIONS request on %s://%s%s" % ( + self.context._scheme, self.app.host, path)) + self.context.post_process_request( + self.app.options(path, self.context.request_headers) + ) + + def PATCH(self, url): + """ + Issues a HTTP PATCH request. + + `url` is the URL relative to the server root, e.g. '/_utils/config.html' + """ + path = self._path_from_url_or_path(url) + kwargs = {} + if 'Content-Type' in self.context.request_headers: + kwargs[ + 'content_type'] = self.context.request_headers['Content-Type'] + self.context.pre_process_request() + logger.debug("Performing PATCH request on %s://%s%s" % ( + self.context._scheme, self.app.host, url)) + self.context.post_process_request( + self.app.patch(path, self.context.request_body or {}, + self.context.request_headers, **kwargs) + ) + def follow_response(self): """ Follows a HTTP redirect if the previous response status code was a 301 or 302. @@ -552,44 +585,46 @@ def stringify_json(self, data): raise ValueError( "Could not stringify '%r' to JSON: %s" % (data, e)) - @_with_json - def get_json_value(self, json_string, json_pointer): + def get_json_value(self, json_string, json_pointer, stringify=True): """ Get the target node of the JSON document `json_string` specified by `json_pointer`. - + `stringify` specifies whether JSON data should be transformed to string before assertion. Example: | ${result}= | Get Json Value | {"foo": {"bar": [1,2,3]}} | /foo/bar | | Should Be Equal | ${result} | [1, 2, 3] | | """ - return jsonpointer.resolve_pointer(json_string, json_pointer) + if stringify: + return json.dumps(jsonpointer.resolve_pointer(load_json(json_string), json_pointer), ensure_ascii=False) + else: + return jsonpointer.resolve_pointer(load_json(json_string), json_pointer) - def json_value_should_equal(self, json_string, json_pointer, expected_value): + def json_value_should_equal(self, json_string, json_pointer, expected_value, stringify=True): """ Fails if the value of the target node of the JSON document `json_string` specified by JSON Pointer `json_pointer` is not `expected_value`. + `stringify` specifies whether JSON data should be transformed to string before assertion. Example: | Set Test Variable | ${doc} | {"foo": {"bar": [1,2,3]}} | | | Json Value Should Equal | ${doc} | /foo/bar | "[1, 2, 3]" | """ - - got = self.get_json_value(json_string, json_pointer) + got = self.get_json_value(json_string, json_pointer, stringify) assert got == expected_value, \ 'JSON value "%s" does not equal "%s", but should have.' % ( got, expected_value) - def json_value_should_not_equal(self, json_string, json_pointer, expected_value): + def json_value_should_not_equal(self, json_string, json_pointer, expected_value, stringify=True): """ Fails if the value of the target node of the JSON document `json_string` specified by JSON Pointer `json_pointer` is `expected_value`. + `stringify` specifies whether JSON data should be transformed to string before assertion. Example: | Set Test Variable | ${doc} | {"foo": {"bar": [1,2,3]}} | | | Json Value Should Not Equal | ${doc} | /foo/bar | "[1, 2, 3]" | """ - - got = self.get_json_value(json_string, json_pointer) + got = self.get_json_value(json_string, json_pointer, stringify) message = 'JSON value "%s" does not equal "%s"' % (got, expected_value) @@ -607,10 +642,18 @@ def set_json_value(self, json_string, json_pointer, json_value): | ${result}= | Set Json Value | {"foo": {"bar": [1,2,3]}} | /foo | 12 | | Should Be Equal | ${result} | {"foo": 12} | | | """ + try: + loaded_json = load_json(json_value) + except ValueError, e: + if isinstance(json_value, basestring): + loaded_json = json_value + else: + raise ValueError("Could not parse '%s' as JSON: %s" % (json_value, e)) + return jsonpatch.apply_patch(json_string, [{ 'op': 'add', 'path': json_pointer, - 'value': load_json(json_value) + 'value': loaded_json }]) @_with_json