{ "info": { "author": "Ross Patterson", "author_email": "me@rpatterson.net", "bugtrack_url": null, "classifiers": [], "description": "============================================\ntest-har\n============================================\nUse HTTP Archive (HAR) files in Python tests\n--------------------------------------------\n\nTo send this request to an API backend and assert the API response matches this\nresponse...\n\n.. code:: json\n\n {\n \"log\": {\n \"version\": \"1.2\",\n \"entries\": [\n {\n \"request\": {\n \"method\": \"POST\",\n \"url\": \"mock://example.com/users/\",\n \"headers\" : [\n {\n \"name\": \"Accept\",\n \"value\": \"application/json\"\n }\n ],\n \"postData\": {\n \"mimeType\": \"application/json\",\n \"text\" : {\n \"username\": \"foo_username\",\n \"email\": \"foo@example.com\",\n \"group\": \"479e75e6-755a-46f1-a949-d6fee3671b3c\"\n }\n },\n \"comment\" : \"Test validation errors\"\n },\n \"response\": {\n \"status\": 201,\n \"statusText\": \"Created\",\n \"headers\" : [\n {\n \"name\": \"Allow\",\n \"value\": \"GET, POST, HEAD, OPTIONS\"\n },\n {\n \"name\": \"Vary\",\n \"value\": \"Accept, Cookie\"\n }\n ],\n \"content\": {\n \"mimeType\": \"application/json\",\n \"text\": {\n \"username\": \"foo_username\"\n }\n }\n }\n }\n ]\n }\n }\n\nUse this test:\n\n.. code:: python\n\n import datetime\n\n # Import the appropriate backend for your framework.\n # Currently there are backends for the `requests` library...\n from test_har.requests_har as test_har\n # and for the Django ReST Framework.\n # from test_har.django_rest_har as test_har\n # Contributions for other frameworks welcome!\n \n from .. import models\n\n\n class MyAPITest(test_har.HARTestCase):\n \"\"\"\n Specify the HAR file to use by default.\n \"\"\"\n\n # Path to file relative to this test file\n # Set to `None` to skip automatic HAR parsing and set up\n example_har = 'example.har.json'\n \n def test_my_response(self):\n \"\"\"\n Write your test as you would.\n \"\"\"\n # For convenience, `self.example` is the parsed JSON in\n # `self.example_har`, `self.entry` is the first request/response\n # entry from `log/entries/0` in the HAR., `self.headers` is a\n # dictionary of the response headers for that entry, and\n # `self.content` is the response body content from that entry.\n\n # Make any changes to the test fixture necessary for the test such\n # as creating related objects before a POST that requires them.\n group = models.Group(\n name='foo_group',\n uuid=self.entry[\"request\"][\"postData\"][\"text\"][\"group\"])\n\n # Make any changes to the HAR necessary for the assertions to work\n # before sending the requests\n self.content[\"some_dynamic_value\"] = models.my_dynamic_function()\n\n # Send the HAR requests, assert responses match the HAR, and return\n # the responses. Currently, assertions are made against the\n # response: `Status` code, `Status` reason text, `Content-Type`\n # MIME type, other headers in the HAR, and the response body\n # content. If the response MIME type is a JSON type,\n # then assertions will be made against each top-level key\n # individually and ignore any key in the response not included in\n # the HAR.\n now = datetime.datetime.now()\n responses = self.assertHAR(self.example)\n\n # Make any other assertions, or do anything else you'd like to do,\n # with the responses.\n self.assertAlmostEqual(\n datetime.strptime(response[0].json()['created'], format='...'),\n now, delta=datetime.timedelta(seconds=1),\n msg='Wrong automatic creation time')\n\n def test_my_other_response(self):\n \"\"\"\n Test a different HAR file.\n \"\"\"\n # Replace `self.example` and the other convenience attributes with\n # the content form another HAR file\n self.setUpHAR('other.har.json')\n responses = self.assertHAR(self.example)\n ...\n\n----\nWhy?\n----\n\nWriting tests for API backends can often involve a lot of repetitive code in\nthe tests to construct the content either to POST to the API or the expected\ncontent in the response against which to make assertions. For example,\nconstructing a Python dictionary to represent the JSON POSTed to or returned\nfrom the API. Similarly, testing the returned content often requires many\ndetailed assertions in order to sufficiently cover how the API should behave.\n\nWhile writing this repetitive test code is tedious, more importantly, it isn't\nvery readable and makes discerning the intended behavior the test is meant to\ncover unnecessarily difficult. Ideally, one should be able to describe the\nexpected behavior of the API in a format much closer to the format used by the\nAPI. Also ideally, one should be able to clearly read related requests and\nresponses together.\n\nThe JSON-based `HTTP Archive`_ (HAR) format used by browsers for recording\nbrowser sessions gives us exactly such a format, especially for JSON-based\nAPIs. The `test_har` package provides support for using HAR files to drive\ntests and make the more common assertions against the response while still\nallowing the developer to continue doing anything not able to be covered using\nthe HAR file the same way they would have otherwise.\n\n\n.. _HTTP Archive: https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/HAR/Overview.html\n", "description_content_type": null, "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/rpatterson/test-har", "keywords": "testing test har", "license": "GPL", "maintainer": "", "maintainer_email": "", "name": "test-har", "package_url": "https://pypi.org/project/test-har/", "platform": "", "project_url": "https://pypi.org/project/test-har/", "project_urls": { "Homepage": "https://github.com/rpatterson/test-har" }, "release_url": "https://pypi.org/project/test-har/0.2.dev0/", "requires_dist": null, "requires_python": "", "summary": "Use HTTP Archive (HAR) files in Python tests", "version": "0.2.dev0" }, "last_serial": 3174121, "releases": { "0.1.dev0": [ { "comment_text": "", "digests": { "md5": "9bdb51cfd137a1ded3e1bacef947a62f", "sha256": "edf9d47a1f04d7138a93c13cfee149893fea85b5b5bc006bfc501eb5c6af53ff" }, "downloads": -1, "filename": "test_har-0.1.dev0-py2-none-any.whl", "has_sig": false, "md5_digest": "9bdb51cfd137a1ded3e1bacef947a62f", "packagetype": "bdist_wheel", "python_version": "2.7", "requires_python": null, "size": 16901, "upload_time": "2017-09-09T20:36:27", "url": "https://files.pythonhosted.org/packages/ee/4a/1ba238722e1325403648b66566a90a15e40c71eeb7649ea902a317614e8a/test_har-0.1.dev0-py2-none-any.whl" }, { "comment_text": "", "digests": { "md5": "253372d6789c16406df9ba8684630e2a", "sha256": "2623f25570f215c93e4eb96ce9359a7c00160be46a9b1b73c25334781100f701" }, "downloads": -1, "filename": "test-har-0.1.dev0.tar.gz", "has_sig": false, "md5_digest": "253372d6789c16406df9ba8684630e2a", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 12508, "upload_time": "2017-09-09T20:34:37", "url": "https://files.pythonhosted.org/packages/c2/ac/5c029f287cf9704664cf1afb66a216828ca13b8d81b9d2fa7ce56aee5977/test-har-0.1.dev0.tar.gz" } ], "0.2.dev0": [ { "comment_text": "", "digests": { "md5": "58cde148e122384210ba304431d2f260", "sha256": "8cfe5437d7cd73d91fda8f46a8e5a4755b96b531753382ddc7a73fdd603df97d" }, "downloads": -1, "filename": "test_har-0.2.dev0-py3-none-any.whl", "has_sig": false, "md5_digest": "58cde148e122384210ba304431d2f260", "packagetype": "bdist_wheel", "python_version": "3.6", "requires_python": null, "size": 16972, "upload_time": "2017-09-14T16:08:36", "url": "https://files.pythonhosted.org/packages/13/47/5bb98fa57c8f0221d30e133cd26ec13e28aa9e5b32512839f745ff334627/test_har-0.2.dev0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "68c40129c9143f1c77682e21af16d50c", "sha256": "a771f8aac1c9e9989f68d76c53a952b0cf8960531de853c9b25fca73c82aa7eb" }, "downloads": -1, "filename": "test-har-0.2.dev0.tar.gz", "has_sig": false, "md5_digest": "68c40129c9143f1c77682e21af16d50c", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 12562, "upload_time": "2017-09-14T16:08:34", "url": "https://files.pythonhosted.org/packages/20/61/5b56c49f4f64a759c15a79e28a4547a2a468edb17988d1c53719dec0f4bb/test-har-0.2.dev0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "58cde148e122384210ba304431d2f260", "sha256": "8cfe5437d7cd73d91fda8f46a8e5a4755b96b531753382ddc7a73fdd603df97d" }, "downloads": -1, "filename": "test_har-0.2.dev0-py3-none-any.whl", "has_sig": false, "md5_digest": "58cde148e122384210ba304431d2f260", "packagetype": "bdist_wheel", "python_version": "3.6", "requires_python": null, "size": 16972, "upload_time": "2017-09-14T16:08:36", "url": "https://files.pythonhosted.org/packages/13/47/5bb98fa57c8f0221d30e133cd26ec13e28aa9e5b32512839f745ff334627/test_har-0.2.dev0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "68c40129c9143f1c77682e21af16d50c", "sha256": "a771f8aac1c9e9989f68d76c53a952b0cf8960531de853c9b25fca73c82aa7eb" }, "downloads": -1, "filename": "test-har-0.2.dev0.tar.gz", "has_sig": false, "md5_digest": "68c40129c9143f1c77682e21af16d50c", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 12562, "upload_time": "2017-09-14T16:08:34", "url": "https://files.pythonhosted.org/packages/20/61/5b56c49f4f64a759c15a79e28a4547a2a468edb17988d1c53719dec0f4bb/test-har-0.2.dev0.tar.gz" } ] }