Example #1
0
 def start(self):
     websocket_connect(
         self.url,
     #     self.ioloop, # no longer takes this arg?
         callback=self.on_connected,
         on_message_callback=self.on_message)
     self.ioloop.start()
Example #2
0
    def _connect(self,
                 conn_type,
                 session,
                 force_close,
                 force_release,
                 pool):
        future = self._future_class()
        request = self._connector(self._url)
        if self._timeout:
            future_conn = with_timeout(timeout, websocket_connect(request))
        else:
            future_conn = websocket_connect(request)

        def get_conn(f):
            try:
                conn = f.result()
            except socket.error:
                future.set_exception(
                    RuntimeError("Could not connect to server."))
            except socket.gaierror:
                future.set_exception(
                    RuntimeError("Could not connect to server."))
            except HTTPError as e:
                future.set_exception(e)
            except Exception as e:
                future.set_exception(e)
            else:
                resp = Response(conn, self._future_class, self._loop)
                gc = conn_type(resp, self._future_class, self._timeout,
                               self._username, self._password, self._loop,
                               force_close, pool, force_release, session)
                future.set_result(gc)
        future_conn.add_done_callback(get_conn)
        return future
Example #3
0
    def test_status_dual_ws(self):

        _mock_device = self._mock_device
        class EventThread(threading.Thread):
            def run(self):
                for x in xrange(5):
                    stat = x % 2 and 'ready' or 'unavailable'
                    _mock_device._set_device('rtl', stat)
                    time.sleep(.2)
  
        url = self.get_url('/status').replace('http', 'ws')
        conn1 = yield websocket.websocket_connect(url,
                                                  io_loop=self.io_loop) 
        conn2 = yield websocket.websocket_connect(url,
                                                  io_loop=self.io_loop) 
        self.io_loop.add_callback(EventThread().start)
        for x in xrange(5):
            stat = x % 2 and 'ready' or 'unavailable'
            message = yield conn1.read_message()
            logging.debug("Connection 1 message #%s: %s", x, message)
            data = json.loads(message)
            self.assertEquals(stat, data['body']['status'])

            message = yield conn2.read_message()
            logging.debug("Connection 2 message #%s: %s", x, message)
            data = json.loads(message)
            self.assertEquals(stat, data['body']['status'])
        conn1.close()
        conn2.close()
        # FIXME: Ensure that close is being called on websocket so that is tested
        # tried with stop(), but unsure if that does anything
        self.stop()
 def test_websocket_callbacks(self):
     websocket_connect("ws://localhost:%d/echo" % self.get_http_port(), io_loop=self.io_loop, callback=self.stop)
     ws = self.wait().result()
     ws.write_message("hello")
     ws.read_message(self.stop)
     response = self.wait().result()
     self.assertEqual(response, "hello")
    def _assertCrossDomainRequestFails(self, auth_url):
        encoded_query_args = urlparse.urlencode(
            {'jupyter_http_over_ws_auth_url': auth_url})

        request = self.get_ws_connection_request(
            'http_over_websocket/proxied_ws/requires-cookies-from-fake-auth-ws?'
            + encoded_query_args)
        request.headers.add('Origin', WHITELISTED_ORIGIN)

        with testing.ExpectLog('tornado.application',
                               'Uncaught error when proxying request',
                               required=True) as expect_log:
            client = yield websocket.websocket_connect(request)
            self.assertIsNotNone(client)

            msg = yield client.read_message()
            # Message of None indicates that the connection has been closed.
            self.assertIsNone(msg)
            self.assertEqual(500, client.close_code)
            self.assertTrue(expect_log.logged_stack)
    def test_diagnostic_handler_newer_protocol_version_requested(self):
        request = self.get_ws_connection_request(
            'http_over_websocket/diagnose')
        request.url += '?min_version=0.0.7'
        request.headers.add('Origin', WHITELISTED_ORIGIN)
        request.headers.add('Cookie', '_xsrf=' + FAKE_XSRF_VALUE)

        client = yield websocket.websocket_connect(request)
        client.write_message('1')

        response_body = yield client.read_message()
        response = json.loads(response_body)

        self.assertEqual(
            {
                'message_id': '1',
                'extension_version': '0.0.6',
                'has_authentication_cookie': True,
                'is_outdated_extension': True
            }, response)
    def test_auth_url_provided_fails(self):
        auth_url = self.get_server_base_url() + '/fake-auth?always_fail=1'
        encoded_query_args = urlparse.urlencode(
            {'jupyter_http_over_ws_auth_url': auth_url})

        request = self.get_ws_connection_request('http_over_websocket?' +
                                                 encoded_query_args)

        client = yield websocket.websocket_connect(request)
        client.write_message(
            self.get_request_json('/requires-cookies-from-fake-auth', '1234'))

        response_body = yield client.read_message()
        response = json.loads(response_body)
        self.assertEqual('1234', response['message_id'])
        self.assertEqual(500, response['status'])
        self.assertEqual(
            'Uncaught server-side exception. Check logs for additional details.',
            response['statusText'])
        self.assertTrue(response['done'])
Example #8
0
    def _on_open(self):
        # Only proxy local connections.
        proxy_path = self.request.uri.replace(self._PATH_PREFIX,
                                              os.environ['JUPYTER_SERVER_URL'])
        proxy_url = os.environ['JUPYTER_SERVER_URL']
        #proxy_url = '{}://{}{}'.format(_PROTOCOL_MAP[self.request.protocol],
        #                               self.request.host, proxy_path)
        yield self._attach_auth_cookies()

        self.log.info('proxying WebSocket connection to: {}'.format(proxy_url))
        proxy_request = httpclient.HTTPRequest(url=proxy_url,
                                               method='GET',
                                               headers=self.request.headers,
                                               body=None,
                                               ca_certs=self.ca_certs)
        _modify_proxy_request_test_only(proxy_request)

        client = yield websocket.websocket_connect(
            proxy_request, on_message_callback=self._on_proxied_message)
        raise gen.Return(client)
        def connect_to_server(self, host, port):
            def on_connect(conn):
                self.matching_server_connection = conn
                print('Connected to matching server')

            def on_message_callback(message):
                if message is None:
                    self.matching_server_connection = None
                    self.connect_to_next_server()
                else:
                    self.message.on_next(message)

            future = websocket_connect(
                'ws://{}:{}/exchange'.format(host, port),
                on_message_callback=on_message_callback
            )
            Observable.from_future(future).subscribe(
                on_connect,
                lambda error: self.connect_to_next_server()
            )
Example #10
0
    def test_bulkio_ws(self):

        # NOTE: A timeout means the website took too long to respond
        # it could mean that bulkio port is not sending data
        cid = next((cp['id']
                    for cp in self.components if cp['name'] == 'SigGen'), None)
        if not cid:
            self.fail('Unable to find SigGen component')

        url = self.get_url("%s/components/%s/ports/out/bulkio" %
                           (Default.REST_BASE + self.base_url, cid)).replace(
                               'http', 'ws')
        conn1 = yield websocket.websocket_connect(url, io_loop=self.io_loop)

        foundSRI = False
        for x in xrange(10):
            msg = yield conn1.read_message()
            try:
                data = json.loads(msg)
                logging.debug("Got SRI %s", data)
                foundSRI = True
                props = set(('hversion', 'xstart', 'xdelta', 'xunits',
                             'subsize', 'ystart', 'ydelta', 'yunits', 'mode',
                             'streamID', 'blocking', 'keywords'))
                missing = props.difference(data.keys())
                if missing:
                    self.fail("Missing SRI properties %s" % missing)
            except ValueError:
                data = dict(data=msg)

        if data.get('error', None):
            self.fail('Recieved websocket error %s' % data)
        #conn1.protocol.close()
        conn1.close()

        # wait a little bit to force close to take place in ioloop
        # (if we return without waiting, ioloop closes before websocket closes)
        x = yield gen.Task(self.io_loop.add_timeout, time.time() + .5)

        if not foundSRI:
            self.fail('Did not receive SRI')
Example #11
0
    def test_forbidden_update(self):
        token = yield self.get_token("*****@*****.**", "elastickube123")
        request = HTTPRequest(
            "ws://%s/api/v1/ws" % self.get_api_address(),
            headers=dict([(api.ELASTICKUBE_TOKEN_HEADER, token)]),
            validate_cert=False
        )

        connection = yield websocket_connect(request)

        correlation = str(uuid.uuid4())[:10]
        connection.write_message(json.dumps({"action": "settings", "operation": "update", "correlation": correlation}))
        response = yield self.wait_message(connection, correlation)
        self.validate_response(response, 403, correlation, "update", "settings")
        self.assertTrue(isinstance(response["body"], dict), "Body is not a dict but %s" % type(response["body"]))

        self.assertTrue(response["body"]["message"] == "Operation update forbidden for action settings.",
                        "Message is %s instead of 'Operation update forbidden for action settings.'" %
                        response["body"]["message"])

        connection.close()
Example #12
0
    def test_diagnostic_handler_no_problems_request(self):
        request = self.get_ws_connection_request(
            http_over_ws_url='http_over_websocket/diagnose')
        request.url += '?min_version=0.0.6'
        request.headers.add('Origin', WHITELISTED_ORIGIN)
        request.headers.add('Cookie', '_xsrf=5678')

        client = yield websocket.websocket_connect(request)
        self.assertNotEqual(None, client)
        client.write_message('1')

        response_body = yield client.read_message()
        response = json.loads(response_body)

        self.assertEqual(
            {
                'message_id': '1',
                'extension_version': '0.0.6',
                'has_authentication_cookie': True,
                'is_outdated_extension': False
            }, response)
Example #13
0
def geth_websocket_connect(url,
                           io_loop=None,
                           callback=None,
                           connect_timeout=None,
                           on_message_callback=None,
                           compression_options=None):
    """Helper function for connecting to geth via websockets, which may need the
    origin set should there be no --wsorigin * option set in geth's config"""

    if not isinstance(url, tornado.httpclient.HTTPRequest):
        if url.startswith('wss://'):
            origin = 'https://{}'.format(socket.gethostname())
        else:
            origin = 'http://{}'.format(socket.gethostname())
        url = tornado.httpclient.HTTPRequest(url, headers={'Origin': origin})
    return websocket_connect(url,
                             io_loop=io_loop,
                             callback=callback,
                             connect_timeout=connect_timeout,
                             on_message_callback=on_message_callback,
                             compression_options=compression_options)
Example #14
0
    def test_websocket_handler_cors_origin_wildcard(self):
        self._app.mod_opts['cors_origin'] = '*'

        response = yield self.http_client.fetch(
            self.get_url('/login'),
            method='POST',
            body=urlencode(self.auth_creds),
            headers={'Content-Type': self.content_type_map['form']})
        token = json.loads(
            self.decode_body(response).body)['return'][0]['token']

        url = 'ws://127.0.0.1:{0}/all_events/{1}'.format(
            self.get_http_port(), token)
        request = HTTPRequest(url,
                              headers={
                                  'Origin': 'http://foo.bar',
                                  'Host': 'example.com'
                              })
        ws = yield websocket_connect(request)
        ws.write_message('websocket client ready')
        ws.close()
Example #15
0
    def spawn_kernel(self, kernel_body='{}'):
        '''
        Code to spawn a kernel and return a websocket connection to it.
        '''
        # Request a kernel
        response = yield self.http_client.fetch(
            self.get_url('/api/kernels'),
            method='POST',
            body=kernel_body
        )
        self.assertEqual(response.code, 201)

        # Connect to the kernel via websocket
        kernel = json_decode(response.body)
        ws_url = 'ws://localhost:{}/api/kernels/{}/channels'.format(
            self.get_http_port(),
            url_escape(kernel['id'])
        )

        ws = yield websocket_connect(ws_url)
        raise Return(ws)
Example #16
0
    def test_auth_url_provided_forwards_cookies(self):
        auth_url = self.get_server_base_url() + '/fake-auth'
        encoded_query_args = urlparse.urlencode(
            {'jupyter_http_over_ws_auth_url': auth_url})

        request = self.get_ws_connection_request('http_over_websocket?' +
                                                 encoded_query_args)

        client = yield websocket.websocket_connect(request)
        client.write_message(
            self.get_request_json('/requires-cookies-from-fake-auth', '1234'))

        response_body = yield client.read_message()
        response = json.loads(response_body)
        self.assertEqual(200, response['status'])
        # RequiresCookieFromFakeAuthHandler writes cookies that should have
        # been received by performing a request to the auth url.
        self.assertEqual('X-Test-Cookie: "1" X-Other-Test-Cookie: "2"',
                         base64.b64decode(response['data']).decode('utf-8'))
        self.assertEqual('1234', response['message_id'])
        self.assertTrue(response['done'])
Example #17
0
 def _connect(self, kernel_id):
     ws_url = url_path_join(
         os.getenv('KG_WS_URL', KG_URL.replace('http', 'ws')),
         '/api/kernels', url_escape(kernel_id), 'channels')
     self.log.info('Connecting to {}'.format(ws_url))
     parameters = {
         "headers": KG_HEADERS,
         "validate_cert": VALIDATE_KG_CERT,
         "auth_username": KG_HTTP_USER,
         "auth_password": KG_HTTP_PASS,
         "connect_timeout": KG_CONNECT_TIMEOUT,
         "request_timeout": KG_REQUEST_TIMEOUT
     }
     if KG_CLIENT_KEY:
         parameters["client_key"] = KG_CLIENT_KEY
         parameters["client_cert"] = KG_CLIENT_CERT
         if KG_CLIENT_CA:
             parameters["ca_certs"] = KG_CLIENT_CA
     request = HTTPRequest(ws_url, **parameters)
     self.ws_future = websocket_connect(request)
     self.ws = yield self.ws_future
Example #18
0
    async def _connect(self, kernel_id, message_callback):
        # websocket is initialized before connection
        self.ws = None
        self.kernel_id = kernel_id
        ws_url = url_path_join(
            GatewayClient.instance().ws_url,
            GatewayClient.instance().kernels_endpoint,
            url_escape(kernel_id),
            "channels",
        )
        self.log.info(f"Connecting to {ws_url}")
        kwargs: dict = {}
        kwargs = GatewayClient.instance().load_connection_args(**kwargs)

        request = HTTPRequest(ws_url, **kwargs)
        self.ws_future = websocket_connect(request)
        self.ws_future.add_done_callback(self._connection_done)

        loop = IOLoop.current()
        loop.add_future(self.ws_future,
                        lambda future: self._read_messages(message_callback))
  def test_auth_url_provided_forwards_cookies_and_propagates_xsrf(self):
    auth_url = self.get_server_base_url() + '/fake-auth'
    encoded_query_args = urlparse.urlencode(
        {'jupyter_http_over_ws_auth_url': auth_url})

    request = self.get_ws_connection_request('http_over_websocket?' +
                                             encoded_query_args)
    client = yield websocket.websocket_connect(request)
    client.write_message(
        self.get_request_json(
            '/api/sessions', '1234', method='POST', body='somedata'))

    response_body = yield client.read_message()
    response = json.loads(response_body)
    self.assertEqual(200, response['status'])
    self.assertEqual('somedata',
                     base64.b64decode(response['data']).decode('utf-8'))
    self.assertEqual('1234', response['message_id'])
    self.assertIn(TEST_XSRF_HEADER, response['headers'])
    self.assertEqual(FAKE_XSRF_VALUE, response['headers'][TEST_XSRF_HEADER])
    self.assertTrue(response['done'])
Example #20
0
    def _connect(self, message=None):
        """ Create (force) a tornado websocket connection. Try NB_CONNECTION_ATTEMPTS attempts,
            waiting for ATTEMPT_DELAY_SECONDS seconds between 2 attempts.
            Raise an exception if it cannot connect.

            :param message: if provided, print this message as a logger info before starting to connect.
            :type message: str, optional
        """
        if message:
            LOGGER.info(message)

        # We are connecting.
        self.is_connecting.clear()

        # Create a connection (currently using websockets).
        self.connection = None
        for attempt_index in range(constants.NB_CONNECTION_ATTEMPTS):
            try:
                future_connection = websocket_connect(self.url)
                self.connection = yield gen.with_timeout(
                    timedelta(seconds=constants.ATTEMPT_DELAY_SECONDS),
                    future_connection)
                break
            except (gen.TimeoutError, ConnectionAbortedError, ConnectionError,
                    ConnectionRefusedError, ConnectionResetError) as ex:
                if attempt_index + 1 == constants.NB_CONNECTION_ATTEMPTS:
                    raise ex
                LOGGER.warning('Connection failing (attempt %d), retrying.',
                               attempt_index + 1)
                yield gen.sleep(constants.ATTEMPT_DELAY_SECONDS)

        if not self.connection_count:
            # Start receiving messages as soon as we are connected.
            ioloop.IOLoop.current().add_callback(self._handle_socket_messages)

        # We are connected.
        self.connection_count += 1
        self.is_connecting.set()

        LOGGER.info('Connection succeeds.')
Example #21
0
def connect_cloud(url):
    conn = yield websocket_connect(url)
    print "edison connected to cloud"
    while True:
        msg = yield conn.read_message()
        if msg is None: break
        msg = json_decode(msg)
        print "received one message from cloud"
        print msg
        protocal = msg[0]
        if protocal == "LITEOSDEMO":
            content = msg[1]["content"]
            if content.startswith("HUAWEI//CMD//"):
                command = content.split("HUAWEI//CMD//")[1]
                print "receive command from cloud: %s" % command
                # cmd 格式  01$1!
                u.writeStr(cmd.encode("ascii"))
                print "send keyboard move: %s" % cmd
            else:
                print "error content"
        else:
            print "unsupported comment"
 def test_gets_user_session_auth(self):
     c = Client()
     response = c.post("/login/",
                       data={
                           "username": self.username,
                           "password": self.password
                       })
     cookies = response.cookies
     cookie_names = (
         "sessionid",
         "csrftoken",
     )
     cookie_header = "; ".join("{}={}".format(name, cookies[name].value)
                               for name in cookie_names)
     request = HTTPRequest(self.get_url("/").replace("http", "ws"),
                           headers={
                               "Cookie": cookie_header,
                               "X-CSRFToken": cookies["csrftoken"].value
                           })
     conn = yield websocket_connect(request)
     response = yield conn.read_message()
     self.assertEquals(User.objects.get(pk=int(response)), self.user)
Example #23
0
    def test_invalid_token(self):
        request = HTTPRequest("ws://%s/api/v1/ws" % self.get_api_address(),
                              headers=dict([(api.ELASTICKUBE_TOKEN_HEADER,
                                             'fake_token')]),
                              validate_cert=False)

        connection = yield websocket_connect(request)
        connection.write_message(
            json.dumps({
                "action": "charts",
                "operation": "watch",
                "correlation": str(uuid.uuid4())[:10]
            }))

        response = yield self.wait_message(connection)
        self.assertTrue(isinstance(response, dict),
                        "Body is not a dict but %s" % type(response))
        self.assertTrue(
            response["error"]["message"] == "Invalid token.",
            "Message is %s instead of 'Invalid token.'" %
            (response["error"]["message"]))
        connection.close()
Example #24
0
 def new_kernel(self, session_id):
     """Start a new kernel, and connect a websocket for each channel
     
     Returns Kernel model dict, adding websocket object at each channel name.
     """
     kernel = self.api_request('kernels',
                               method='POST',
                               data=json.dumps({'name': 'python'}))
     kernel_id = kernel['id']
     cookie_headers = {
         'Cookie':
         '; '.join([
             '%s=%s' % (name, value)
             for name, value in self.cookies.items()
         ])
     }
     url = url_path_join('ws' + self.url[4:], 'api', 'kernels', kernel_id,
                         'channels')
     url += '?session_id=%s' % session_id
     req = HTTPRequest(url, headers=cookie_headers, validate_cert=False)
     kernel['channels'] = yield websocket_connect(req)
     raise gen.Return(kernel)
Example #25
0
    def _connect(self):
        """
        Fire up a websocket connection.
        """
        try:
            if self._sim.predict is True:
                url = self.brain._prediction_url()
            else:
                url = self.brain._simulation_url()

            log.info("trying to connect: {}".format(url))
            req = HTTPRequest(url,
                              connect_timeout=_CONNECT_TIMEOUT_SECS,
                              request_timeout=_CONNECT_TIMEOUT_SECS)
            req.headers['Authorization'] = self.brain.config.accesskey
            req.headers['User-Agent'] = self.brain._user_info

            self._ws = yield websocket_connect(req)
        except Exception as e:
            raise gen.Return(repr(e))
        else:
            raise gen.Return(None)
Example #26
0
    def create_broker_connection(self, url):
        """
        Create an asynchronous connection to the broker.
        """
        while True:
            try:
                self.broker = yield websocket_connect(url)
            except ConnectionRefusedError:
                logger.warning("Cannot connect, retrying in 3s")
            else:
                logger.info("Connected to broker, sending auth token")
                self.broker.write_message(auth_token(self.keys))
                yield gen.sleep(1)
                self.fetch_nodes_cache('all')
                while True:
                    message = yield self.broker.read_message()
                    if message is None:
                        logger.warning("Connection with broker lost.")
                        break
                    self.on_broker_message(message)

            yield gen.sleep(3)
Example #27
0
    def test_publish_event(self, mock_authorize):
        connection_subscriber = MockConnection()
        tornwamp_topic.topics.add_subscriber("interstellar",
                                             connection_subscriber)

        request = self.build_request()
        ws = yield websocket_connect(request)

        options = {"acknowledge": True}
        msg = messages.PublishMessage(request_id=1,
                                      topic="interstellar",
                                      kwargs={"review": "Mind blowing"},
                                      options=options)
        ws.write_message(msg.json)

        text = yield ws.read_message()
        published_msg = messages.PublishedMessage.from_text(text)
        self.assertEqual(published_msg.request_id, 1)

        text = connection_subscriber._websocket.message
        event_msg = messages.EventMessage.from_text(text)
        self.assertEqual(event_msg.kwargs, {u'review': u'Mind blowing'})
Example #28
0
    def connect(self):
        print ("Establishing connection to "+self.url)
        try:
            self.ws = yield websocket_connect(self.url)
        except:
            print ("connection error "+self.url)
        else:
            print ("connected to "+self.url)
            self.run()
            # code for subscribing goes here
            if self.mode==1:
                self.bitfinexSubscribe()#comment this line for debugging
                pass
            elif self.mode==2:
                self.gdaxSubscribe()
                pass
            # elif self.mode==0:
            #     print ("you have entered the dungeon, exit now!!!!")
            #     self.ioloop.stop()

            else:
                pass
Example #29
0
def goods(students, plan_id, user_id, token, num):
    ws = yield websocket_connect(g_ws_url)
    request = {
        "MessageType": "get",
        "UserIdFrom": user_id,
        "UserFlagFrom": token[:5],
        "PlanId": plan_id,
        "StartTextId": 0,
        "StartSignalId": 0,
        "StartGoodId": 0
    }
    text = json.dumps(request, separators=(",", ":"))
    ws.write_message(text)
    read_msg(ws)
    print("start good")
    n = 0
    for _ in range(num):
        for i in students:
            print("i=[{}] n=[{}]".format(i, n))
            n += 1
            add_msg(ws, plan_id, user_id, token, i, "", 100, "1")
            yield gen.sleep(0.0001)
Example #30
0
def websocket_open(io_loop, url, origin=None):
    result = {}
    @gen.coroutine
    def handle_connection(future):
        result['connection'] = future
        io_loop.stop()

    request = HTTPRequest(url)
    if origin is not None:
        request.headers['Origin'] = origin
    resp = websocket_connect(request)
    io_loop.add_future(resp, handle_connection)
    io_loop.start()

    if 'connection' not in result:
        raise RuntimeError("Failed to handle websocket connect")
    future = result['connection']
    if future.exception():
        raise future.exception()
    else:
        future.result().close()
        return None
Example #31
0
def setupQQLight():
    while 1:
        try:
            BotInterface.QQLightWs = yield websocket_connect(
                HTTPRequest('ws://{}:{}/'.format(options.qlhost,
                                                 options.qlport),
                            validate_cert=False))
            while 1:
                nxt = sleep(options.delay)
                message = yield BotInterface.QQLightWs.read_message()
                try:
                    message = json.loads(to_basestring(message),
                                         object_hook=DottedDict)
                    yield MessageInQueue.put(message)
                except Exception as e:
                    log.exception(e)
                yield nxt
        except Exception as e:
            log.exception(e)

        # 300秒后重连
        log.info('reconnect qqlight bot after 300s')
        yield sleep(300)
Example #32
0
    def test_stop_does_stop_car(self):
        self.test_app.car_state.zero_all()
        ws = yield websocket_connect(self.test_url % self.get_http_port(),
                                     io_loop=self.io_loop)

        ws.write_message(generate_speed_inc_message(-10, left=True))
        response = yield ws.read_message()

        self.assertIsNotNone(response)
        res = json.loads(response)
        self.assertIsInstance(res, dict)
        car_state = self.test_app.car_state
        self.assertEqual(car_state.left_motor, -10)
        self.assertEqual(car_state.right_motor, 0)

        ws.write_message(generate_stop_message())
        response = yield ws.read_message()

        self.assertIsInstance(res, dict)
        car_state = self.test_app.car_state
        self.assertEqual(car_state.left_motor, 0)
        self.assertEqual(car_state.right_motor, 0)
        self.test_app.car_state.zero_all()
Example #33
0
  def test_programming_error_propagates(self):
    # Ensure that any programming errors from how the proxy is implemented
    # (i.e. malformed requests) are properly logged.

    # Ideally, there aren't any programming errors in the current
    # implementation. Should one exist, it would be better to fix it rather than
    # use it as a test case here.
    ws_request = self.get_ws_connection_request('always_throwing_http_over_ws')
    client = yield websocket.websocket_connect(ws_request)

    with testing.ExpectLog(
        'tornado.application',
        'Uncaught error when proxying request',
        required=True) as expect_log:
      client.write_message(self.get_request_json('/api/sessions', '1234'))

      response_body = yield client.read_message()
      # Message of None indicates that the connection has been closed.
      self.assertIsNotNone(response_body)

      response = json.loads(response_body)
      self.assertEqual(500, response['status'])
      self.assertTrue(expect_log.logged_stack)
Example #34
0
    def test_proxied_connection_io(self):
        request = self.get_ws_connection_request(
            'http_over_websocket/proxied_ws/echoing-ws?someparam=1')
        request.headers.add('Origin', ALLOWED_ORIGIN)
        request.headers.add('X-Echo-Header', 'example')

        client = yield websocket.websocket_connect(request)
        self.assertIsNotNone(client)

        # EchoingWebSocketHandler writes a message on connection with request
        # details.
        initial_message = yield client.read_message()
        self.assertEqual(
            '<ECHO INIT> GET /echoing-ws?someparam=1 X-Echo-Header: example',
            initial_message)

        client.write_message('1')
        echo_message = yield client.read_message()
        self.assertEqual('<ECHO RESPONSE> 1', echo_message)

        client.write_message('something else!')
        echo_message = yield client.read_message()
        self.assertEqual('<ECHO RESPONSE> something else!', echo_message)