flymock


Nameflymock JSON
Version 0.0.4 PyPI version JSON
download
home_pagehttps://github.com/coldnight/flymock
Summary"Easy to mock external HTTP request in Tornado."
upload_time2017-12-08 07:36:58
maintainer
docs_urlNone
authorGray King
requires_python
license
keywords
VCS
bugtrack_url
requirements six mock PyYAML tornado
Travis-CI
coveralls test coverage
            .. image:: https://travis-ci.org/coldnight/flymock.svg?branch=master
    :target: https://travis-ci.org/coldnight/flymock
.. image:: https://codecov.io/gh/coldnight/flymock/branch/master/graph/badge.svg
  :target: https://codecov.io/gh/coldnight/flymock
.. image:: https://img.shields.io/pypi/v/flymock.svg
    :target: https://pypi.python.org/pypi/flymock
    :alt: PyPI

flymock
=======

Easy to mock external HTTP request in Tornado.

Installation
------------

.. code-block:: shell

    $ pip install --upgrade flymock

Create mock data
----------------

Make a directory in your tests package:

.. code-block:: shell

    $ mkdir __mock__

Use the hostname as the config filename, assume the url is ``http://example.com/demo``,
the config filename should be ``example.com.yaml``, the config see below:

.. code-block:: yaml

    - path: /demo    # path of the request to match
      method: GET    # method of the request to match
      headers:       # Response headers
        Content-Type: application/json
      body: Hello world # Response body
      code: 200      # Response status code

    - path: /file
      body_type: file     # Use a file content as the response
      body: demo.json     # Filename(same path of the config file)
      code: 202

    - path: /json
      body:               # If body is an object, that will response JSON content.
       code: 2


Usage
-----

.. code-block:: python

    import os

    from tornado import httpclient
    from tornado import testing

    from flymock import FlyPatcher


    class DemoTestCase(testing.AsyncTestCase):
        def setUp(self):
            super(DemoTestCase, self).setUp()
            path = os.path.join(os.path.dirname(__file__), "__mock__")
            self.patcher = FlyPatcher(path)
            self.http_client = httpclient.AsyncHTTPClient()
            self.patcher.start()

        def tearDown(self):
            super(DemoTestCase, self).tearDown()
            self.patcher.stop()

        @testing.gen_test
        def test_mocked(self):
            resp = yield self.http_client.fetch("http://example.com/demo")
            self.assertEqual(resp.code, 200)


Adjust response dynamic:

.. code-block:: python

    patcher = FlyPatcher("/path/to/__mock__")
    def hook(response):
       response.patch_json({"a": 1})

    with patcher.dynamic_hook(hook):
        # code goes here
        pass

    # shortcut to adjust JSON
    with patcher.patch_json({"a": 1}):
       # code goes here
       pass


            

Raw data

            {
    "maintainer": "", 
    "docs_url": null, 
    "requires_python": "", 
    "maintainer_email": "", 
    "cheesecake_code_kwalitee_id": null, 
    "keywords": "", 
    "upload_time": "2017-12-08 07:36:58", 
    "requirements": [
        {
            "name": "six", 
            "specs": []
        }, 
        {
            "name": "mock", 
            "specs": []
        }, 
        {
            "name": "PyYAML", 
            "specs": []
        }, 
        {
            "name": "tornado", 
            "specs": []
        }
    ], 
    "author": "Gray King", 
    "home_page": "https://github.com/coldnight/flymock", 
    "github_user": "coldnight", 
    "download_url": "https://pypi.python.org/packages/69/76/d654fc19a0dad0a011106cd55068ab902c07ae0f621261df3528aefcd656/flymock-0.0.4.tar.gz", 
    "platform": "", 
    "version": "0.0.4", 
    "cheesecake_documentation_id": null, 
    "description": ".. image:: https://travis-ci.org/coldnight/flymock.svg?branch=master\n    :target: https://travis-ci.org/coldnight/flymock\n.. image:: https://codecov.io/gh/coldnight/flymock/branch/master/graph/badge.svg\n  :target: https://codecov.io/gh/coldnight/flymock\n.. image:: https://img.shields.io/pypi/v/flymock.svg\n    :target: https://pypi.python.org/pypi/flymock\n    :alt: PyPI\n\nflymock\n=======\n\nEasy to mock external HTTP request in Tornado.\n\nInstallation\n------------\n\n.. code-block:: shell\n\n    $ pip install --upgrade flymock\n\nCreate mock data\n----------------\n\nMake a directory in your tests package:\n\n.. code-block:: shell\n\n    $ mkdir __mock__\n\nUse the hostname as the config filename, assume the url is ``http://example.com/demo``,\nthe config filename should be ``example.com.yaml``, the config see below:\n\n.. code-block:: yaml\n\n    - path: /demo    # path of the request to match\n      method: GET    # method of the request to match\n      headers:       # Response headers\n        Content-Type: application/json\n      body: Hello world # Response body\n      code: 200      # Response status code\n\n    - path: /file\n      body_type: file     # Use a file content as the response\n      body: demo.json     # Filename(same path of the config file)\n      code: 202\n\n    - path: /json\n      body:               # If body is an object, that will response JSON content.\n       code: 2\n\n\nUsage\n-----\n\n.. code-block:: python\n\n    import os\n\n    from tornado import httpclient\n    from tornado import testing\n\n    from flymock import FlyPatcher\n\n\n    class DemoTestCase(testing.AsyncTestCase):\n        def setUp(self):\n            super(DemoTestCase, self).setUp()\n            path = os.path.join(os.path.dirname(__file__), \"__mock__\")\n            self.patcher = FlyPatcher(path)\n            self.http_client = httpclient.AsyncHTTPClient()\n            self.patcher.start()\n\n        def tearDown(self):\n            super(DemoTestCase, self).tearDown()\n            self.patcher.stop()\n\n        @testing.gen_test\n        def test_mocked(self):\n            resp = yield self.http_client.fetch(\"http://example.com/demo\")\n            self.assertEqual(resp.code, 200)\n\n\nAdjust response dynamic:\n\n.. code-block:: python\n\n    patcher = FlyPatcher(\"/path/to/__mock__\")\n    def hook(response):\n       response.patch_json({\"a\": 1})\n\n    with patcher.dynamic_hook(hook):\n        # code goes here\n        pass\n\n    # shortcut to adjust JSON\n    with patcher.patch_json({\"a\": 1}):\n       # code goes here\n       pass\n\n", 
    "tox": true, 
    "lcname": "flymock", 
    "bugtrack_url": null, 
    "github": true, 
    "coveralls": true, 
    "name": "flymock", 
    "license": "", 
    "travis_ci": true, 
    "github_project": "flymock", 
    "summary": "\"Easy to mock external HTTP request in Tornado.\"", 
    "split_keywords": [], 
    "author_email": "grayking.w@gmail.com", 
    "urls": [
        {
            "has_sig": false, 
            "upload_time": "2017-12-08T07:36:58", 
            "comment_text": "", 
            "python_version": "source", 
            "url": "https://pypi.python.org/packages/69/76/d654fc19a0dad0a011106cd55068ab902c07ae0f621261df3528aefcd656/flymock-0.0.4.tar.gz", 
            "md5_digest": "026db80768252cc06f8bb90d9c52ec1e", 
            "downloads": 0, 
            "filename": "flymock-0.0.4.tar.gz", 
            "packagetype": "sdist", 
            "path": "69/76/d654fc19a0dad0a011106cd55068ab902c07ae0f621261df3528aefcd656/flymock-0.0.4.tar.gz", 
            "size": 12098
        }
    ], 
    "_id": null, 
    "cheesecake_installability_id": null
}