Esempio n. 1
0
    def __init__(self, filepath, options=None, match_options=None):
        self._filepath = filepath
        default_options = dict(verify_ssl=False)
        if options:
            default_options.update(options)

        self._options = options or dict()
        self._formatter = Formatter()
        self._match_options = match_options or dict()
Esempio n. 2
0
 def __init__(self, filepath):
     """
     :param filepath: new record will append to file if filepath exists
     :return:
     """
     self.filepath = filepath
     self._running = False
     self._closing = False
     self._service_thread = None
     self._queue = Queue()
     self._formatter = Formatter()
Esempio n. 3
0
def test_http_recorder():
    _, filepath = tempfile.mkstemp()
    formatter = Formatter()
    recorder = HTTPRecorder(filepath)
    try:
        recorder.start_service()
        recorder.record_http(req, res)
    finally:
        recorder.close()

    with open(filepath, 'r') as readable:
        request, response = formatter.read_record(readable)
        assert req.__dict__ == request.__dict__
        assert res.__dict__ == response.__dict__

    os.remove(filepath)
Esempio n. 4
0
def test_http_recorder():
    _, filepath = tempfile.mkstemp()
    formatter = Formatter()
    recorder = HTTPRecorder(filepath)
    try:
        recorder.start_service()
        recorder.record_http(req, res)
    finally:
        recorder.close()

    with open(filepath, 'r') as readable:
        request, response = formatter.read_record(readable)
        assert req.__dict__ == request.__dict__
        assert res.__dict__ == response.__dict__

    os.remove(filepath)
Esempio n. 5
0
    def __init__(self, filepath, options=None, match_options=None):
        self._filepath = filepath
        default_options = dict(verify_ssl=False)
        if options:
            default_options.update(options)

        self._options = options or dict()
        self._formatter = Formatter()
        self._match_options = match_options or dict()
Esempio n. 6
0
 def __init__(self, filepath):
     """
     :param filepath: new record will append to file if filepath exists
     :return:
     """
     self.filepath = filepath
     self._running = False
     self._closing = False
     self._service_thread = None
     self._queue = Queue()
     self._formatter = Formatter()
Esempio n. 7
0
class Generator(object):
    """
    Generator test scripts from record data
    """

    def __init__(self, filepath, options=None, match_options=None):
        self._filepath = filepath
        default_options = dict(verify_ssl=False)
        if options:
            default_options.update(options)

        self._options = options or dict()
        self._formatter = Formatter()
        self._match_options = match_options or dict()

    def generate(self):
        """
        return None if records data not found
        :return:
        """

        import codecs

        records = []
        with codecs.open(self._filepath, 'r', 'utf-8') as record_file:
            while True:
                result = self._formatter.read_record(record_file)
                if not result:
                    break
                records.append(result)

        from zerotest.generator.python.render import Renderer

        if len(records) == 0:
            return None

        renderer = Renderer(options=self._options,
                            match_options=self._match_options)

        return renderer.render(records)
Esempio n. 8
0
class Generator(object):
    """
    Generator test scripts from record data
    """
    def __init__(self, filepath, options=None, match_options=None):
        self._filepath = filepath
        default_options = dict(verify_ssl=False)
        if options:
            default_options.update(options)

        self._options = options or dict()
        self._formatter = Formatter()
        self._match_options = match_options or dict()

    def generate(self):
        """
        return None if records data not found
        :return:
        """

        import codecs

        records = []
        with codecs.open(self._filepath, 'r', 'utf-8') as record_file:
            while True:
                result = self._formatter.read_record(record_file)
                if not result:
                    break
                records.append(result)

        from zerotest.generator.python.render import Renderer

        if len(records) == 0:
            return None

        renderer = Renderer(options=self._options,
                            match_options=self._match_options)

        return renderer.render(records)
Esempio n. 9
0
class HTTPRecorder(object):
    """
    format and record tunnel request/response to file
    """
    def __init__(self, filepath):
        """
        :param filepath: new record will append to file if filepath exists
        :return:
        """
        self.filepath = filepath
        self._running = False
        self._closing = False
        self._service_thread = None
        self._queue = Queue()
        self._formatter = Formatter()

    def start_service(self):
        """
        start recorder service
        :return:
        """
        self._service_thread = threading.Thread(target=self._loop_work)
        self._running = True
        LOG.info("recorder service start, store http record to '{}'".format(
            self.filepath))
        self._service_thread.start()

    def _loop_work(self):
        import codecs
        with codecs.open(self.filepath, 'a+', 'utf-8') as record_file:
            while True:
                task = self._queue.get()
                LOG.debug("receive task %s", task)
                if task is None:
                    record_file.close()
                    return

                self._formatter.write_record(record_file, task[0], task[1])
                record_file.flush()
                LOG.debug("writen task %s", task)

    def record_http(self, request, response):
        """
        async method, put request, response into task queue
        :return:
        """
        if not self._closing:
            LOG.debug("record http %s, %s", request, response)
            self._queue.put((request, response))

    def close(self):
        """
        graceful close, wait util queue empty
        :return:
        """
        if self._running:
            LOG.debug("closing...")
            self._closing = True
            self._queue.put(None)
            LOG.debug("wait task complete...")
            self._service_thread.join()
        else:
            raise RuntimeError("current service is not running")

        LOG.info("closed, record file: '{}'".format(self.filepath))

    def shutdown(self):
        """
        shutdown service, threw out items already in queue
        :return:
        """
        LOG.debug("shutdown...")
        self._closing = True
        try:
            while True:
                self._queue.get_nowait()
        except Empty:
            pass

        self.close()
Esempio n. 10
0
from zerotest.utils.io_helper import StringIO

from zerotest.record.formatter import Formatter
from zerotest.request import Request
from zerotest.response import Response

req = Request(scheme="http",
              method="get",
              params="query_string=here",
              host="example.com",
              path="/test",
              headers={"just": "header"},
              data="request")
res = Response(status=200, headers={"responsed": "header"}, body="response")
formatter = Formatter()


def test_formatter():
    writable = StringIO()
    formatter.write_record(writable, req, res)
    readable = StringIO(writable.getvalue())
    request, response = formatter.read_record(readable)
    assert req.__dict__ == request.__dict__
    assert res.__dict__ == response.__dict__
Esempio n. 11
0
class HTTPRecorder(object):
    """
    format and record tunnel request/response to file
    """

    def __init__(self, filepath):
        """
        :param filepath: new record will append to file if filepath exists
        :return:
        """
        self.filepath = filepath
        self._running = False
        self._closing = False
        self._service_thread = None
        self._queue = Queue()
        self._formatter = Formatter()

    def start_service(self):
        """
        start recorder service
        :return:
        """
        self._service_thread = threading.Thread(target=self._loop_work)
        self._running = True
        LOG.info("recorder service start, store http record to '{}'".format(self.filepath))
        self._service_thread.start()

    def _loop_work(self):
        import codecs
        with codecs.open(self.filepath, 'a+', 'utf-8') as record_file:
            while True:
                task = self._queue.get()
                LOG.debug("receive task %s", task)
                if task is None:
                    record_file.close()
                    return

                self._formatter.write_record(record_file, task[0], task[1])
                record_file.flush()
                LOG.debug("writen task %s", task)

    def record_http(self, request, response):
        """
        async method, put request, response into task queue
        :return:
        """
        if not self._closing:
            LOG.debug("record http %s, %s", request, response)
            self._queue.put((request, response))

    def close(self):
        """
        graceful close, wait util queue empty
        :return:
        """
        if self._running:
            LOG.debug("closing...")
            self._closing = True
            self._queue.put(None)
            LOG.debug("wait task complete...")
            self._service_thread.join()
        else:
            raise RuntimeError("current service is not running")

        LOG.info("closed, record file: '{}'".format(self.filepath))

    def shutdown(self):
        """
        shutdown service, threw out items already in queue
        :return:
        """
        LOG.debug("shutdown...")
        self._closing = True
        try:
            while True:
                self._queue.get_nowait()
        except Empty:
            pass

        self.close()