{ "info": { "author": "INAP", "author_email": "opensource@internap.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 3 - Alpha", "Intended Audience :: Developers", "License :: OSI Approved :: Apache Software License", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Topic :: Software Development :: Testing" ], "description": "# MockServer Python Client\n\n[![Build Status](https://travis-ci.org/internap/python-mockserver-friendly-client.svg?branch=master)](https://travis-ci.org/internap/python-mockserver-friendly-client)\n\nPython client to James D. Bloom's awesome MockServer : http://www.mock-server.com/\n\n## Philosophy\n\nTests should be readable and Mock Server is already very complete and customizable. This library tries to keep it\nsimple and straight-forward using python's kwargs to avoid big declarations.\n\nWe think a mock should be able to fit into one 120 char line of code should the expectation be simple enough :) \n\n```\nclient.stub(request(method=\"GET\", path=\"/auth\"), response(code=401, body=\"unauthorized\"))\n```\n\n## Installation\n\nWARNING: **THIS IS A VERY EARLY VERSION THE API IS MOST LIKELY TO CHANGE**\n\nFrom source:\n\n```\ngit clone https://github.com/internap/python-mockserver-friendly-client.git\ncd python-mockserver-friendly-client\npython setup.py install\n```\n\nFrom [PyPi](https://pypi.org/) directly:\n\n```\npip install mockserver-friendly-client\n```\n\n## Prerequisite\n\nYou need a running MockServer running, see http://www.mock-server.com/mock_server/getting_started.html#start_mockserver\n\nThis project's tests uses the docker image : https://github.com/internap/python-mockserver-friendly-client/blob/master/docker-compose.yml\n\n## Usage\n\n**The whole Mock Server API is not all covered**. We just implemented what we needed, if you need something not yet\nimplemented you can open an issue and/or contribute\n\n### *Stubbing*\n\n(For when you are testing what your code *DOES* with another component's data)\n\n```\nfrom mockserver import MockServerClient, request, response\n\nclient = MockServerClient(\"http://localhost:1080\")\n\nclient.stub(\n request(method=\"GET\", path=\"/that/thing\", querystring={\"is\": \"good\"}, headers={\"so\": \"good\"}),\n response(code=418, body=\"i'm a teapot\", headers={\"hi\": \"haa\"})\n)\n```\n\n* You can also add a `times(N)` as a third parameter to limit how many times this stub can be called, default is unlimited\n* All parameters are always optional. When not specified, the match everything.\n\n### *Expecting*\n\n(For when calling another component *IS* what you are testing)\n\nUsing the `expect` will remember the request and verify it when `verify_expectations` is called.\n\n```\nimport json\nfrom mockserver import MockServerClient, request, response, times\n\nclient = MockServerClient(\"http://localhost:1080\")\n\nclient.expect(\n request(method=\"POST\", path=\"/postme\", body=json.dumps({\"some\": \"json\"})),\n response(code=204, body=json.dumps({\"return\": \"something\"}), headers={\"Content-Type\": \"application/json\"}),\n times(1)\n)\n\nclient.verify_expectations() # AssertionError !\n```\n\n* The `times(N)` parameter is mandatory for expect\n\n### *Resetting*\n\n(Because tests shouldn't impact each other)\n\n```\nfrom mockserver import MockServerClient\n\nclient = MockServerClient(\"http://localhost:1080\")\n\nclient.reset()\n```\n\n## Customizing and shortcuts\n\nThis client consumes Mock Server's REST API : https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi\n\nHere's a few shortcuts already ready, there may be more to come\n\n### Mocking a form post\n\n```\nfrom mockserver import MockServerClient, request, response, form\n\nclient = MockServerClient(\"http://localhost:1080\")\n\nclient.stub(\n request(method=\"POST\", body=form({\"user\": \"foo\", \"pass\": \"bar\"})),\n response(code=201)\n)\n```\n\nThis currently takes a 1 level `dict`. For array mocking, use `{\"key[index]\": \"value\"}` \n\n### Returning JSON\n\n```\nfrom mockserver import MockServerClient, request, json_response\n\nclient = MockServerClient(\"http://localhost:1080\")\n\nclient.stub(\n request(path=\"/stuff\"),\n json_response(code=200, body={\"full\": \"json\", \"structure\": [\"with\", \"stuff\", 1]})\n)\n```\n\nThis automatically dumps the body in json and appends the `application/json` Content-Type to the headers.\n\n### Expecting JSON request\n\n```\nfrom mockserver import MockServerClient, request, json_equals\n\nclient = MockServerClient(\"http://localhost:1080\")\nclient.expect(\n request(body=json_equals({\"key\": \"value\"})),\n response(),\n times(1)\n)\n```\n\nThis only matches requests with the provided json body. Json conversation is done automatically.\nIf you only want to match parts of a given JSON you can provide only the matching parts using `json_contains`:\n\n```\nfrom mockserver import MockServerClient, request, json_contains\n\nclient = MockServerClient(\"http://localhost:1080\")\nclient.expect(\n request(body=json_contains({\"key\": \"value\"})),\n response(),\n times(1)\n)\n```\nI.e. this will match `{\"key\": \"value\"}` or `{\"key\": \"value\", \"another\": \"key\"}`\n\n### Verifying request\n\n```\nfrom mockserver import MockServerClient, request, times\n\nclient = MockServerClient(\"http://localhost:1080\")\nclient.verify(\n request(path=\"/some_path\", querystring({\"key\": \"value\"})),\n times(1)\n)\n```\nVerify will check request on MockServer and raise AssertionError if request not found.\n\n## More documentation\n\nThere is currently no official documentation, however you can consider the tests as a type of documentation, they are\npretty explicit and simple to follow and help to clarify the purpose of each feature.\n\n## Good practices\n\nHaving your test setup/teardown like this is probably a good idea.\n\n```\nclass ServerMockingTestBase(...):\n def setUp(self):\n super(ServerMockingTestBase, self).setUp()\n \n self.client = MockServerClient(MOCK_SERVER_URL)\n self.client.reset()\n\n def tearDown(self):\n super(ServerMockingTestBase, self).tearDown()\n \n self.client.verify_expectations()\n```\n\n# Troubleshooting\n\nChecking MockServer's logs is the first place to go. If you don't see any logs, try another LOG_LEVEL such as INFO.\n\nIf the problem is in the code, please open an issue :)\n\n# Contributing\n\nThe form may not be final, we would love to hear what you think of the client!\n\nFeel free to raise issues and send some pull request, we'll be happy to look at them!\n\nMake sure all new code is tested, current tests run against a MockServer container.\n\n## Running tests\n\nYou can easily run the tests with https://pypi.python.org/pypi/tox\n\n```\ntox -e py34\n```\n\nYou can also call the `test-runner.sh` directly, you will need https://pypi.python.org/pypi/nose installed\n\nYou can also launch the container\n\n```\ndocker-compose up-d\n```\n\nand run the tests in your favorite IDE :)", "description_content_type": "", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/internap/python-mockserver-friendly-client", "keywords": "", "license": "", "maintainer": "", "maintainer_email": "", "name": "mockserver-friendly-client", "package_url": "https://pypi.org/project/mockserver-friendly-client/", "platform": "", "project_url": "https://pypi.org/project/mockserver-friendly-client/", "project_urls": { "Homepage": "https://github.com/internap/python-mockserver-friendly-client" }, "release_url": "https://pypi.org/project/mockserver-friendly-client/0.3.0/", "requires_dist": null, "requires_python": "", "summary": "Friendly python client to James D. Bloom's awesome MockServer", "version": "0.3.0" }, "last_serial": 4931170, "releases": { "0.1.0": [ { "comment_text": "", "digests": { "md5": "03ec57ae5b6da0d527d6dbc6145c8ea7", "sha256": "d63e49cf830a5794a229de19eda40319e246fcabe6091ab8a75ea48e02b5059f" }, "downloads": -1, "filename": "mockserver-friendly-client-0.1.0.tar.gz", "has_sig": false, "md5_digest": "03ec57ae5b6da0d527d6dbc6145c8ea7", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 14923, "upload_time": "2019-02-11T14:35:01", "url": "https://files.pythonhosted.org/packages/69/c8/0c8d27b9985aaead995a67ba3d159c511d4206147ed40753be69804fb5bc/mockserver-friendly-client-0.1.0.tar.gz" } ], "0.2.0": [ { "comment_text": "", "digests": { "md5": "04b7750b5e9755318f949a7975ca0609", "sha256": "7127f21085fa0583ba5d5d3e821fe58557899451753661385aa8bc1709b6493c" }, "downloads": -1, "filename": "mockserver-friendly-client-0.2.0.tar.gz", "has_sig": false, "md5_digest": "04b7750b5e9755318f949a7975ca0609", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 14814, "upload_time": "2019-02-13T16:12:47", "url": "https://files.pythonhosted.org/packages/3c/12/6f621a29f16bdda85bdd1a1b96f8ac44f747afa842f69ef9a9fd4b1522bf/mockserver-friendly-client-0.2.0.tar.gz" } ], "0.2.1": [ { "comment_text": "", "digests": { "md5": "04c1b3aced49a40b5cf57118595ddc57", "sha256": "ea47d30597704f5c9d59acd1c294ba3f7b13845c0606b4dcea4991862eb70b6c" }, "downloads": -1, "filename": "mockserver-friendly-client-0.2.1.tar.gz", "has_sig": false, "md5_digest": "04c1b3aced49a40b5cf57118595ddc57", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 14833, "upload_time": "2019-02-13T16:41:22", "url": "https://files.pythonhosted.org/packages/bb/9e/a1aeedab287b7b78daa0e468e948fbbbba862b7f1449df93290f39cc9224/mockserver-friendly-client-0.2.1.tar.gz" } ], "0.3.0": [ { "comment_text": "", "digests": { "md5": "e54fa3b0cde7f0958d982f0c27a830ad", "sha256": "fee618005c670a94e2b2f8f4a0f1e6c46585c8cb8317cf364fcc6aafb87b4eb9" }, "downloads": -1, "filename": "mockserver-friendly-client-0.3.0.tar.gz", "has_sig": false, "md5_digest": "e54fa3b0cde7f0958d982f0c27a830ad", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 15159, "upload_time": "2019-03-12T17:34:13", "url": "https://files.pythonhosted.org/packages/88/9a/6c1d973fb90139887dd724498349b5449237bd4a2ad923cac9be2c04a7c6/mockserver-friendly-client-0.3.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "e54fa3b0cde7f0958d982f0c27a830ad", "sha256": "fee618005c670a94e2b2f8f4a0f1e6c46585c8cb8317cf364fcc6aafb87b4eb9" }, "downloads": -1, "filename": "mockserver-friendly-client-0.3.0.tar.gz", "has_sig": false, "md5_digest": "e54fa3b0cde7f0958d982f0c27a830ad", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 15159, "upload_time": "2019-03-12T17:34:13", "url": "https://files.pythonhosted.org/packages/88/9a/6c1d973fb90139887dd724498349b5449237bd4a2ad923cac9be2c04a7c6/mockserver-friendly-client-0.3.0.tar.gz" } ] }