Exemple #1
0
class CommandResponseStatus(Command, metaclass=CommandRegister):
    pretty_name = "RESPONSE STATUS"

    def __init__(self, result_collector):
        super(CommandResponseStatus, self).__init__(result_collector)
        self.mapping = CaseInsensitiveDict()
        self.mapping["Ok"] = 200
        self.mapping["Not found"] = 404

    def map_status_code(self, status):
        """

        author Damian Mirecki

        :param status
        :return:
        :rtype: int
        :raise LookupError: When status is not implemented yet.
        """

        if status not in self.mapping:
            raise LookupError("Status " + status + " not found in " + self.mapping.__str__())

        return self.mapping[status]

    def parse(self, path):
        response = self.result_collector.get_response()
        if response is None:
            raise ParsingException(self, result.ERROR_RESPONSE_NOT_FOUND)

        actual = response.status_code

        if len(path) == 0:
            return actual, ParsedValue(self, None, "")

        status = path[0]

        if not status.isdigit():
            try:
                status = self.map_status_code(status)
            except LookupError as e:
                raise ParsingException(self, Error.from_exception(self, e))
        else:
            if int(status) not in _codes.keys():
                raise ParsingException(self, result.ERROR_INVALID_STATUS_CODE)

        expected = int(status)
        return actual, ParsedValue(self, expected, "")