예제 #1
0
    async def __aenter__(self) -> WebSocketClientProtocol:
        uri = self._get_uri()

        # Create connection with Basic Auth.
        self._conn = websockets.client.connect(
            uri=uri,
            ssl=self.ssl_context,
            extra_headers={
                "Authorization": build_authorization_basic(
                    self.user,
                    self.password
                )
            },
            ping_interval=None,         # do not send pings
            ping_timeout=None,          # do not expect pongs
            ** self.other_conn_args
        )

        try:
            _ws_client = await self._conn.__aenter__()
            logging.info(
                f'Connected to {uri}. State: {_ws_client.open and "Open" or "Not Open"}'
            )
        except websockets.exceptions.WebSocketException as we:
            logging.warning(
                f"Websockets lib exception while awaiting on NifiWebSocketClient.__aenter__(). Cause: {we}."
            )
            raise NifiWebSocketConnectionException(
                f"Failed to estabilish a connection with {uri}."
            )

        return _ws_client
예제 #2
0
 def test_basic_auth_invalid_credentials_details(self):
     with self.assertRaises(urllib.error.HTTPError) as raised:
         authorization = build_authorization_basic("hello", "ihateyou")
         self.loop.run_until_complete(
             self.make_http_request(
                 headers={"Authorization": authorization}))
     self.assertEqual(raised.exception.code, 403)
     self.assertNotIn("WWW-Authenticate", raised.exception.headers)
     self.assertEqual(raised.exception.read().decode(),
                      "Invalid credentials\n")