Ejemplo n.º 1
0
def add_preset(uid):
    """
    Save the incoming request body as a preset response
    """
    preset = Preset(json_data=bottle.request.body.read())
    if preset.times != FOREVER and preset.times <= 0:
        msg = ("Preset has {0} times. Must be greater than "
               "zero.".format(preset.times).encode())
        raise HTTPResponse(msg, status=400)
    try:
        pretender = get_pretenders('http')[uid]
        if pretender.is_expired:
            raise HTTPResponse("{0} mock {1} is TIMED OUT".format('http', uid),
                               status=404)
    except KeyError:
        raise HTTPResponse("No matching {0} mock: {1}".format('http', uid),
                           status=404)

    rule = match_rule_from_dict(preset.rule)

    if rule not in PRESETS[uid]:
        PRESETS[uid][rule] = []
    url_presets = PRESETS[uid][rule]
    url_presets.append(preset)
Ejemplo n.º 2
0
def select_preset(uid, request):
    """
    Select a preset to respond with.

    Look through the presets for a match. If one is found pop off a preset
    response and return it.

    :param uid: The uid to look up presets for
    :param request: A dictionary representing the mock request which is checked
        to see if it matches the rule regex and headers stored in the preset.

    Return 404 if no preset found that matches.
    """
    preset_dict = PRESETS[uid]
    for key, preset_list in preset_dict.items():
        preset = preset_list[0]
        match_rule = match_rule_from_dict(preset.rule)

        if match_rule.matches(request):
            knock_off_preset(preset_dict, key)
            time.sleep(preset.after)
            return preset

    raise HTTPResponse(b"No matching preset response", status=404)