Mock Hive

Most of the time creating a new API is a time-consuming task. Other teams (or people) might be depending on your API to create clients or the application that will consume your data.

Usually you’d give them the contract of your API (or worse, they’d have to wait until your API is ready). bzz comes packed with a mocked responses API that allows you to easily craft a mock API that can be used by clients/partners/teams/aliens.

This way you can keep focus in the development of your API, while at the same time allowing people to work with what will eventually be replaced by the real API.

Using Mock Hive

class bzz.mock.MockHive[source]
classmethod routes_for(routes_tuple)[source]

Returns a tuples list of paths, tornado ready

Let’s create a new server with a few mocked routes. MockedRoutes expects a list of tuples with

[(‘METHOD’, ‘URL or regex’, dict(body=”string or function”, status=”200”, cookies={‘cookie’: ‘yum’}))]

import tornado.web
import bzz

server = None

#first create the routes
mocked_routes = bzz.MockHive.routes_for([
   ('GET', '/much/api', dict(body='much api')),
   ('POST', '/much/api'),
   ('*', '/much/match', dict(body='such match')),
   ('*', r'/such/.*', dict(body='such match')),
   ('GET', '/much/error', dict(body='WOW', status=404)),
   ('GET', '/much/authentication', dict(body='WOW', cookies={'super': 'cow'})),
   ('GET', '/such/function', dict(body=lambda x: x.method)),

def handle_api_response(response):
   # making sure we get the right route
      assert response.code == 200, response.code
      assert response.body == six.b('much api'), response.body

def get_route():
   # let's test one of them

application = tornado.web.Application(mocked_routes)
server = HTTPServer(application, io_loop=io_loop)
io_loop.add_timeout(1, get_route)