def test_correct_auth(self, proto_ver, proto_name, fake_websocket_broker, auth_headers, init_response_headers): """ Make sure it sends the right auth headers """ mqttc = client.Client("test_correct_path", protocol=proto_ver, transport="websockets") mqttc.ws_set_options(headers=auth_headers, ) def check_headers_used(decoded): # Make sure it connects to the right path if auth_headers: for h in auth_headers: assert re.search("{:s}: {:s}".format(h, auth_headers[h]), decoded, re.IGNORECASE) is not None response = self._get_callback_handler( init_response_headers, check_request=check_headers_used, ) with fake_websocket_broker.serve(response): mqttc.connect("localhost", 1888, keepalive=10) mqttc.disconnect()
def test_correct_auth(self, proto_ver, proto_name, fake_websocket_broker, auth_headers, init_response_headers): """ Make sure it sends the right auth headers """ mqttc = client.Client( "test_correct_path", protocol=proto_ver, transport="websockets" ) mqttc.ws_set_options( headers=auth_headers, ) def check_headers_used(decoded): # Make sure it connects to the right path if auth_headers: for h in auth_headers: assert re.search("{:s}: {:s}".format(h, auth_headers[h]), decoded, re.IGNORECASE) is not None response = self._get_callback_handler( init_response_headers, check_request=check_headers_used, ) with fake_websocket_broker.serve(response): mqttc.connect("localhost", 1888, keepalive=10) mqttc.disconnect()
def test_correct_path(self, proto_ver, proto_name, fake_websocket_broker, mqtt_path, init_response_headers): """ Make sure it can connect on user specified paths """ mqttc = client.Client("test_correct_path", protocol=proto_ver, transport="websockets") mqttc.ws_set_options(path=mqtt_path, ) def check_path_correct(decoded): # Make sure it connects to the right path if mqtt_path: assert re.search("GET {:s} HTTP/1.1".format(mqtt_path), decoded, re.IGNORECASE) is not None response = self._get_callback_handler( init_response_headers, check_request=check_path_correct, ) with fake_websocket_broker.serve(response): mqttc.connect("localhost", 1888, keepalive=10) mqttc.disconnect()
def test_correct_path(self, proto_ver, proto_name, fake_websocket_broker, mqtt_path, init_response_headers): """ Make sure it can connect on user specified paths """ mqttc = client.Client( "test_correct_path", protocol=proto_ver, transport="websockets" ) mqttc.ws_set_options( path=mqtt_path, ) def check_path_correct(decoded): # Make sure it connects to the right path if mqtt_path: assert re.search("GET {:s} HTTP/1.1".format(mqtt_path), decoded, re.IGNORECASE) is not None response = self._get_callback_handler( init_response_headers, check_request=check_path_correct, ) with fake_websocket_broker.serve(response): mqttc.connect("localhost", 1888, keepalive=10) mqttc.disconnect()
def test_successful_connection(self, proto_ver, proto_name, fake_websocket_broker, init_response_headers): """ Connect successfully, on correct path """ mqttc = client.Client("test_successful_connection", protocol=proto_ver, transport="websockets") response = self._get_callback_handler(init_response_headers) with fake_websocket_broker.serve(response): mqttc.connect("localhost", 1888, keepalive=10) mqttc.disconnect()
def test_bad_secret_key(self, proto_ver, proto_name, fake_websocket_broker, init_response_headers): """ Server doesn't give anything after connection: upgrade """ mqttc = client.Client("test_bad_secret_key", protocol=proto_ver, transport="websockets") response = self._get_basic_handler(init_response_headers) with fake_websocket_broker.serve(response): with pytest.raises(WebsocketConnectionError) as exc: mqttc.connect("localhost", 1888, keepalive=10) assert str( exc.value) == "WebSocket handshake error, invalid secret key"
def test_no_upgrade(self, proto_ver, proto_name, fake_websocket_broker, init_response_headers): """ Server doesn't respond with 'connection: upgrade' """ mqttc = client.Client("test_no_upgrade", protocol=proto_ver, transport="websockets") init_response_headers["Connection"] = "bad" response = self._get_basic_handler(init_response_headers) with fake_websocket_broker.serve(response): with pytest.raises(WebsocketConnectionError) as exc: mqttc.connect("localhost", 1888, keepalive=10) assert str( exc.value) == "WebSocket handshake error, connection not upgraded"
def test_successful_connection(self, proto_ver, proto_name, fake_websocket_broker, init_response_headers): """ Connect successfully, on correct path """ mqttc = client.Client( "test_successful_connection", protocol=proto_ver, transport="websockets" ) response = self._get_callback_handler(init_response_headers) with fake_websocket_broker.serve(response): mqttc.connect("localhost", 1888, keepalive=10) mqttc.disconnect()
def test_bad_secret_key(self, proto_ver, proto_name, fake_websocket_broker, init_response_headers): """ Server doesn't give anything after connection: upgrade """ mqttc = client.Client( "test_bad_secret_key", protocol=proto_ver, transport="websockets" ) response = self._get_basic_handler(init_response_headers) with fake_websocket_broker.serve(response): with pytest.raises(WebsocketConnectionError) as exc: mqttc.connect("localhost", 1888, keepalive=10) assert str(exc.value) == "WebSocket handshake error, invalid secret key"
def test_unexpected_response(self, proto_ver, proto_name, fake_websocket_broker): """ Server responds with a valid code, but it's not what the client expected """ mqttc = client.Client("test_unexpected_response", protocol=proto_ver, transport="websockets") class WebsocketHandler(socketserver.BaseRequestHandler): def handle(_self): # Respond with data passed in to serve() _self.request.sendall("200 OK".encode("utf8")) with fake_websocket_broker.serve(WebsocketHandler): with pytest.raises(WebsocketConnectionError) as exc: mqttc.connect("localhost", 1888, keepalive=10) assert str(exc.value) == "WebSocket handshake error"
def test_no_upgrade(self, proto_ver, proto_name, fake_websocket_broker, init_response_headers): """ Server doesn't respond with 'connection: upgrade' """ mqttc = client.Client( "test_no_upgrade", protocol=proto_ver, transport="websockets" ) init_response_headers["Connection"] = "bad" response = self._get_basic_handler(init_response_headers) with fake_websocket_broker.serve(response): with pytest.raises(WebsocketConnectionError) as exc: mqttc.connect("localhost", 1888, keepalive=10) assert str(exc.value) == "WebSocket handshake error, connection not upgraded"
def test_unexpected_response(self, proto_ver, proto_name, fake_websocket_broker): """ Server responds with a valid code, but it's not what the client expected """ mqttc = client.Client( "test_unexpected_response", protocol=proto_ver, transport="websockets" ) class WebsocketHandler(socketserver.BaseRequestHandler): def handle(_self): # Respond with data passed in to serve() _self.request.sendall("200 OK".encode("utf8")) with fake_websocket_broker.serve(WebsocketHandler): with pytest.raises(WebsocketConnectionError) as exc: mqttc.connect("localhost", 1888, keepalive=10) assert str(exc.value) == "WebSocket handshake error"