Skip to main content

"Easy to mock external HTTP request in Tornado."

Project description

https://travis-ci.org/coldnight/flymock.svg?branch=master https://codecov.io/gh/coldnight/flymock/branch/master/graph/badge.svg PyPI

flymock

Easy to mock external HTTP request in Tornado.

Installation

$ pip install --upgrade flymock

Create mock data

Make a directory in your tests package:

$ 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:

- 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

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)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flymock-0.0.2.dev1.tar.gz (10.9 kB view details)

Uploaded Source

File details

Details for the file flymock-0.0.2.dev1.tar.gz.

File metadata

  • Download URL: flymock-0.0.2.dev1.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for flymock-0.0.2.dev1.tar.gz
Algorithm Hash digest
SHA256 31de9f27318cf1d39a45e57379294f85a13a606cf37f3b7b96431cb043494b24
MD5 e2efb8da68a935506c1d30355df09513
BLAKE2b-256 1bcf72931b105bf3898a7fec5e6b728fd737ed4474a87e877545db21f4082f28

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page