Example #1
0
def replay_http(uid, url):
    """
    Replay a previously recorded preset, and save the request in history
    """
    pretender.exists_or_404('http', uid)
    request_info = RequestSerialiser(url, bottle.request)
    body = request_info.serialize()
    LOGGER.debug("KEEPING UID {0} ALIVE".format(uid))
    pretender.keep_alive('http', uid)

    boss_response = replay(uid, body)
    preset = Preset(boss_response.as_json().encode('ascii'))
    # ^ Any suggestions about what we can do here?
    # Preset expects a string like object that can be decoded.
    # in py3k that means a 'bytes' object. in py2.X that means a string.
    # So the above works, but it looks ugly - ideally we'd handle both in
    # Preset constructor.
    return preset.as_http_response(bottle.response)
Example #2
0
    def add(self, match_rule=None, response_status=200,
            response_body=b'', response_headers={}, times=1, after=0):
        """
        Add a new preset to the boss server.
        """
        new_preset = Preset(
            headers=response_headers,
            body=binary_to_ascii(response_body),
            status=response_status,
            rule=match_rule,
            times=times,
            after=after
        )

        response, data = self.http('POST', url=self.path,
                                   body=new_preset.as_json())
        if response.status != 200:
            raise ConfigurationError(data.decode())
        return response
Example #3
0
def replay_http(path):
    """
    Replay a previously recorded preset, and save the request in history
    """
    uid = path.split('/')[0]
    url = path[len(uid):]
    pretender.exists_or_404('http', uid)
    request_info = RequestSerialiser(url, bottle.request)
    body = request_info.serialize()
    LOGGER.debug("KEEPING UID {0} ALIVE".format(uid))
    pretender.keep_alive('http', uid)

    boss_response = replay(uid, body)
    preset = Preset(boss_response.as_json().encode('ascii'))
    # ^ Any suggestions about what we can do here?
    # Preset expects a string like object that can be decoded.
    # in py3k that means a 'bytes' object. in py2.X that means a string.
    # So the above works, but it looks ugly - ideally we'd handle both in
    # Preset constructor.
    return preset.as_http_response(bottle.response)
Example #4
0
    def add(self,
            match_rule=None,
            response_status=200,
            response_body=b'',
            response_headers={},
            times=1,
            after=0):
        """
        Add a new preset to the boss server.
        """
        new_preset = Preset(headers=response_headers,
                            body=binary_to_ascii(response_body),
                            status=response_status,
                            rule=match_rule,
                            times=times,
                            after=after)

        response, data = self.http('POST',
                                   url=self.path,
                                   body=new_preset.as_json())
        if response.status != 200:
            raise ConfigurationError(data.decode())
        return response
Example #5
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)