예제 #1
0
def desc(value):
    """ str | List[str]: A normalized representation for a user-provided value. """

    if isinstance(value, list):
        return map(desc, value)

    if isregex(value):
        value = value.pattern

    if isbytes(value):
        value = decode_bytes(value)

    if PY2:
        if isstring(value):
            # In Python 2, strings (``unicode`` objects) represent as ``u'...'``, so ensure
            # the string is encoded (as a ``str`` object) for cleaner representation.
            value = encode_string(value)

    return repr(value)
예제 #2
0
    def normalize_strings(value):
        if isinstance(value, list):
            value = [normalize_strings(e) for e in value]

        if isinstance(value, dict):
            value = {normalize_strings(k): normalize_strings(v) for k, v in iter(value.items())}

        if isregex(value):
            value = value.pattern

        if isbytes(value):
            value = decode_bytes(value)

        if PY2:
            if isstring(value):
                # In Python 2, strings (``unicode`` objects) represent as ``u'...'``, so ensure
                # the string is encoded (as a ``str`` object) for cleaner representation.
                value = encode_string(value)

        return value
예제 #3
0
    def save_page(self, path=None):
        """
        Save a snapshot of the page.

        If invoked without arguments, it will save a file to :data:`capybara.save_path` and the
        file will be given a randomly generated filename. If invoked with a relative path, the path
        will be relative to :data:`capybara.save_path`.

        Args:
            path (str, optional): The path to where it should be saved.

        Returns:
            str: The path to which the file was saved.
        """

        path = _prepare_path(path, "html")

        with open(path, "wb") as f:
            f.write(encode_string(self.body))

        return path
예제 #4
0
    def test_waits_for_pending_requests(self):
        counter = Counter()

        def app(environ, start_response):
            with counter:
                sleep(0.2)
                start_response("200 OK", [])
                return [encode_string("Hello Server!")]

        server = Server(app).boot()

        # Start request, but don't wait for it to finish
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((server.host, server.port))
        s.send(encode_string("GET / HTTP/1.0\r\n\r\n"))
        sleep(0.1)

        assert counter.value == 1

        server.wait_for_pending_requests()

        assert counter.value == 0

        s.close()
예제 #5
0
 def app(environ, start_response):
     with counter:
         sleep(0.2)
         start_response("200 OK", [])
         return [encode_string("Hello Server!")]
예제 #6
0
 def app2(environ, start_response):
     start_response("200 OK", [])
     return [encode_string("Hello Second Server!")]
예제 #7
0
 def identify(self, environ, start_response):
     start_response("200 OK", [("Content-Type", "text/plain")])
     return [encode_string(str(id(self.app)))]