Exemplo n.º 1
0
    def test_binds_to_the_specified_port(self, app):
        capybara.server_host = "127.0.0.1"
        server = Server(app).boot()
        with closing(urlopen("http://127.0.0.1:{}".format(
                server.port))) as response:
            assert "Hello Server" in decode_bytes(response.read())

        capybara.server_host = "0.0.0.0"
        server = Server(app).boot()
        with closing(urlopen("http://127.0.0.1:{}".format(
                server.port))) as response:
            assert "Hello Server" in decode_bytes(response.read())
Exemplo n.º 2
0
    def test_uses_the_existing_server_if_it_is_already_running(self, app):
        server1 = Server(app).boot()
        server2 = Server(app).boot()

        with closing(urlopen("http://{}:{}".format(
                server1.host, server1.port))) as response1:
            assert "Hello Server" in decode_bytes(response1.read())

        with closing(urlopen("http://{}:{}".format(
                server2.host, server2.port))) as response2:
            assert "Hello Server" in decode_bytes(response2.read())

        assert server1.port == server2.port
Exemplo n.º 3
0
    def test_finds_an_available_port(self, app):
        def app2(environ, start_response):
            start_response("200 OK", [])
            return [encode_string("Hello Second Server!")]

        server1 = Server(app).boot()
        server2 = Server(app2).boot()

        with closing(urlopen("http://{}:{}".format(
                server1.host, server1.port))) as response1:
            assert "Hello Server" in decode_bytes(response1.read())

        with closing(urlopen("http://{}:{}".format(
                server2.host, server2.port))) as response2:
            assert "Hello Second Server" in decode_bytes(response2.read())
Exemplo n.º 4
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)

    return repr(value)
Exemplo n.º 5
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)
Exemplo n.º 6
0
    def responsive(self):
        """ bool: Whether the server for this app is up and responsive. """

        if self.server_thread and self.server_thread.join(0):
            return False

        try:
            # Try to fetch the endpoint added by the middleware.
            identify_url = "http://{0}:{1}/__identify__".format(self.host, self.port)

            with closing(urlopen(identify_url)) as response:
                body, status_code = response.read(), response.getcode()

                if str(status_code)[0] == "2" or str(status_code)[0] == "3":
                    return decode_bytes(body) == str(id(self.app))
        except URLError:
            pass

        return False
Exemplo n.º 7
0
def normalize_text(value):
    """
    Normalizes the given value to a string of text with extra whitespace removed.

    Byte sequences are decoded. ``None`` is converted to an empty string. Everything else
    is simply cast to a string.

    Args:
        value (Any): The data to normalize.

    Returns:
        str: The normalized text.
    """

    if value is None:
        return ""

    text = decode_bytes(value) if isbytes(value) else str(value)

    return normalize_whitespace(text)
Exemplo n.º 8
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
Exemplo n.º 9
0
 def test_uses_given_port(self, app):
     server = Server(app, port=22790).boot()
     with closing(urlopen("http://{}:22790".format(
             server.host))) as response:
         assert "Hello Server" in decode_bytes(response.read())
Exemplo n.º 10
0
 def test_uses_specified_port(self, app):
     capybara.server_port = 22789
     server = Server(app).boot()
     with closing(urlopen("http://{}:22789".format(
             server.host))) as response:
         assert "Hello Server" in decode_bytes(response.read())
Exemplo n.º 11
0
 def test_spools_up_a_wsgi_server(self, app):
     server = Server(app).boot()
     with closing(urlopen("http://{}:{}".format(server.host,
                                                server.port))) as response:
         assert "Hello Server" in decode_bytes(response.read())
Exemplo n.º 12
0
 def html(self):
     return decode_bytes(
         self.last_response.data) if self.last_response else ""
Exemplo n.º 13
0
 def __init__(self, native):
     if isinstance(native, (bytes_, str_)):
         from lxml import etree
         native = decode_bytes(native)
         native = etree.HTML(native)
     self.native = native