Exemplo n.º 1
0
    def test_encoder(self):
        """
        Test
        """

        buf = "BUF\u001B\u0BD9\U0001A10D\u1501FUB"
        bin_buf = SolBase.unicode_to_binary(buf, "utf-8")
        self.assertEqual(buf, SolBase.binary_to_unicode(bin_buf, "utf-8"))
Exemplo n.º 2
0
    def _on_log(self, message):
        """
        Log callback
        :param message: Log
        :type message: bytes
        """

        # We receive binary, go to str
        self.lastMessage = SolBase.binary_to_unicode(message)
        self.onLogCallCount += 1
Exemplo n.º 3
0
    def _decode(cls, buf):
        """
        Decode buffer
        :param buf: bytes
        :type buf: bytes
        :return tuple (data, ttl_ms, ms_added)
        :rtype tuple
        """

        try:
            d = ujson.loads(SolBase.binary_to_unicode(buf, "utf-8"))
            return SolBase.unicode_to_binary(
                d["data"], "utf-8"), d["ttl_ms"], d["ms_added"]
        except Exception as e:
            logger.warning("Unable to decode, buf=%s, ex=%s", buf,
                           SolBase.extostr(e))
            return None, None, None
Exemplo n.º 4
0
    def _on_receive(self, binary_buffer):
        """
        Callback called upon server receive.
        :param binary_buffer: The binary buffer received.

        """

        # Received something...
        logger.debug("binary_buffer=%s", binary_buffer)

        # Base
        TcpSimpleClient._on_receive(self, binary_buffer)

        # Pump the queue
        while True:
            try:
                # Try
                item = self.get_from_receive_queue()
                # Process
                self._process_item(SolBase.binary_to_unicode(item, "utf-8"))
            except Empty:
                break
Exemplo n.º 5
0
    def _http_basic_internal_to_httpmock(self,
                                         force_implementation,
                                         proxy=False):
        """
        Test
        """

        logger.info("impl=%s, proxy=%s", force_implementation, proxy)

        self.h = HttpMock()

        self.h.start()
        self.assertTrue(self.h._is_running)
        self.assertIsNotNone(self.h._wsgi_server)
        self.assertIsNotNone(self.h._server_greenlet)

        # Param
        v = parse.urlencode({"p1": "v1 2.3/4"})

        # Client
        hc = HttpClient()

        # ---------------------
        # AUTO-DETECT
        # ---------------------

        # AUTO-DETECT : Http get
        logger.info("AUTO: GET")
        hreq = HttpRequest()
        hreq.force_http_implementation = force_implementation
        if proxy:
            hreq.http_proxy_host = "127.0.0.1"
            hreq.http_proxy_port = 1180
        hreq.uri = "http://127.0.0.1:7900/unittest?" + v
        hresp = hc.go_http(hreq)
        logger.info("Got=%s", hresp)
        self.assertEqual(hresp.status_code, 200)
        self.assertEqual(
            SolBase.binary_to_unicode(hresp.buffer, "utf-8"),
            "OK\nfrom_qs={'p1': 'v1 2.3/4'} -EOL\nfrom_post={} -EOL\nfrom_method=GET\n"
        )

        # AUTO-DETECT : Http post
        logger.info("AUTO: POST")
        hreq = HttpRequest()
        hreq.force_http_implementation = force_implementation
        if proxy:
            hreq.http_proxy_host = "127.0.0.1"
            hreq.http_proxy_port = 1180
        hreq.uri = "http://127.0.0.1:7900/unittest"
        hreq.post_data = v
        hresp = hc.go_http(hreq)
        logger.info("Got=%s", hresp)
        self.assertEqual(hresp.status_code, 200)

        # PY3 : urllib parse return binary, so we have binary in post...
        self.assertEqual(
            SolBase.binary_to_unicode(hresp.buffer, "utf-8"),
            "OK\nfrom_qs={} -EOL\nfrom_post={b'p1': b'v1 2.3/4'} -EOL\nfrom_method=POST\n"
        )

        # ---------------------
        # MANUAL
        # ---------------------

        # No post
        for cur_method in ["GET", "HEAD", "OPTIONS", "TRACE"]:
            # Gevent do not support some, skip
            if force_implementation == HttpClient.HTTP_IMPL_GEVENT and cur_method in [
                    "PATCH", "OPTIONS", "TRACE"
            ]:
                continue
            logger.info("MANUAL : %s", cur_method)
            hreq = HttpRequest()
            hreq.force_http_implementation = force_implementation
            if proxy:
                hreq.http_proxy_host = "127.0.0.1"
                hreq.http_proxy_port = 1180
            hreq.uri = "http://127.0.0.1:7900/unittest?" + v
            hreq.method = cur_method
            hresp = hc.go_http(hreq)
            logger.info("Got=%s", hresp)
            self.assertEqual(hresp.status_code, 200)
            if cur_method == "HEAD":
                # No body in reply
                self.assertEqual(
                    SolBase.binary_to_unicode(hresp.buffer, "utf-8"), "")
            else:

                self.assertEqual(
                    SolBase.binary_to_unicode(hresp.buffer, "utf-8"),
                    "OK\nfrom_qs={'p1': 'v1 2.3/4'} -EOL\nfrom_post={} -EOL\nfrom_method="
                    + cur_method + "\n")

        # Post
        for cur_method in ["POST", "PUT", "PATCH", "DELETE"]:
            # Gevent do not support some, skip
            if force_implementation == HttpClient.HTTP_IMPL_GEVENT and cur_method in [
                    "PATCH", "OPTIONS", "TRACE"
            ]:
                continue
            logger.info("MANUAL : %s", cur_method)
            hreq = HttpRequest()
            hreq.force_http_implementation = force_implementation
            if proxy:
                hreq.http_proxy_host = "127.0.0.1"
                hreq.http_proxy_port = 1180
            hreq.uri = "http://127.0.0.1:7900/unittest"
            hreq.method = cur_method
            hreq.post_data = v
            hresp = hc.go_http(hreq)
            logger.info("Got=%s", hresp)
            self.assertEqual(hresp.status_code, 200)

            # PY3 : urllib parse return binary, so we have binary in post...
            self.assertEqual(
                SolBase.binary_to_unicode(hresp.buffer, "utf-8"),
                "OK\nfrom_qs={} -EOL\nfrom_post={b'p1': b'v1 2.3/4'} -EOL\nfrom_method="
                + cur_method + "\n")

        # ---------------------
        # INVALID
        # ---------------------

        # Http get toward invalid
        hreq = HttpRequest()
        hreq.force_http_implementation = force_implementation
        if proxy:
            hreq.http_proxy_host = "127.0.0.1"
            hreq.http_proxy_port = 1180
        hreq.uri = "http://127.0.0.1:7900/invalid"
        hresp = hc.go_http(hreq)
        logger.info("Got=%s", hresp)
        self.assertEqual(hresp.status_code, 400)

        # Over
        self.h.stop()
        self.assertFalse(self.h._is_running)
        self.assertIsNone(self.h._wsgi_server)
        self.assertIsNone(self.h._server_greenlet)