fix(Redis): Poco::Redis after executing auth command next command always return OK #2457

This commit is contained in:
Alex Fabijanic 2022-06-25 20:25:03 +02:00
parent ae00f1c8eb
commit 5ef96287ee
3 changed files with 37 additions and 1 deletions

View File

@ -119,6 +119,12 @@ public:
bool isConnected() const;
/// Returns true iff the Client is connected to a Redis server.
bool sendAuth(const std::string& password);
/// Sends password to Redis server
bool isAuthenticated();
/// Returns true when the client is authenticated
template<typename T>
T execute(const Array& command)
/// Sends the Redis Command to the server. It gets the reply
@ -199,6 +205,7 @@ private:
Net::StreamSocket _socket;
RedisInputStream* _input;
RedisOutputStream* _output;
bool _authenticated = false;
};
@ -233,6 +240,12 @@ inline void Client::setReceiveTimeout(const Timespan& timeout)
}
inline bool Client::isAuthenticated()
{
return _authenticated;
}
} } // namespace Poco::Redis

View File

@ -151,6 +151,29 @@ bool Client::isConnected() const
}
bool Client::sendAuth(const std::string& password)
{
Array cmd;
cmd << "AUTH" << password;
bool ret = true;
std::string response;
try
{
response = execute<std::string>(cmd);
}
catch (...)
{
ret = false;
}
_authenticated = (ret && (response == "OK"));
return _authenticated;
}
void Client::writeCommand(const Array& command, bool doFlush)
{
poco_assert(_output);

View File

@ -287,7 +287,7 @@ void RedisTest::testDECR()
try
{
Poco::Int64 result = _redis.execute<Poco::Int64>(decr);
_redis.execute<Poco::Int64>(decr);
fail("This must fail");
}
catch (RedisException& e)