Adds rules to global http mock.

    It permits to set mock in a more global way than decorators, cf.:
    https://github.com/openstack/requests-mock

    Here we assume urls in the passed dict are regex we recompile before adding
    a rule.

    Rules example:

    >>> def fake_duckduckgo_cb(request):
    ...     return 200, {}, 'Coincoin!'

    >>> rules = [
        {
            'method': 'GET',
            'status_code': 200,
            'text': 'I am watching you',
            'url': r'^https://www.google.com/#q='
        },
        {
            'method': 'GET',
            'text': fake_duckduckgo_cb,
            'url': r'^https://duckduckgo.com/?q='
        },
    ]

def update_http_rules(rules, content_type='text/plain'):
    """Adds rules to global http mock.

    It permits to set mock in a more global way than decorators, cf.:
    https://github.com/openstack/requests-mock

    Here we assume urls in the passed dict are regex we recompile before adding
    a rule.

    Rules example:

    >>> def fake_duckduckgo_cb(request):
    ...     return 200, {}, 'Coincoin!'

    >>> rules = [
        {
            'method': 'GET',
            'status_code': 200,
            'text': 'I am watching you',
            'url': r'^https://www.google.com/#q='
        },
        {
            'method': 'GET',
            'text': fake_duckduckgo_cb,
            'url': r'^https://duckduckgo.com/?q='
        },
    ]

    """
    for kw in deepcopy(rules):

        kw['url'] = re.compile(kw['url'])

        # ensure headers dict for at least have a default content type
        if 'Content-Type' not in kw.get('headers', {}):
            kw['headers'] = dict(kw.get('headers', {}), **{
                'Content-Type': content_type,
            })

        method = kw.pop('method')
        url = kw.pop('url')

        http_mock.register_uri(method, url, **kw)