mirror of
https://github.com/boostorg/redis.git
synced 2026-01-19 04:42:09 +00:00
Refactors add_hello and adds unit tests.
This commit is contained in:
@@ -698,6 +698,10 @@ https://lists.boost.org/Archives/boost/2023/01/253944.php.
|
|||||||
[this](https://github.com/boostorg/redis/issues/181#issuecomment-1913346983)
|
[this](https://github.com/boostorg/redis/issues/181#issuecomment-1913346983)
|
||||||
comment.
|
comment.
|
||||||
|
|
||||||
|
* ([Issue 182](https://github.com/boostorg/redis/issues/182)).
|
||||||
|
Sets `"default"` as the default value of `config::username`. This
|
||||||
|
makes it simpler to use the `requirepass` configuration in Redis.
|
||||||
|
|
||||||
### Boost 1.84 (First release in Boost)
|
### Boost 1.84 (First release in Boost)
|
||||||
|
|
||||||
* Deprecates the `async_receive` overload that takes a response. Users
|
* Deprecates the `async_receive` overload that takes a response. Users
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ struct config {
|
|||||||
* [HELLO](https://redis.io/commands/hello/) command. If left
|
* [HELLO](https://redis.io/commands/hello/) command. If left
|
||||||
* empty `HELLO` will be sent without authentication parameters.
|
* empty `HELLO` will be sent without authentication parameters.
|
||||||
*/
|
*/
|
||||||
std::string username;
|
std::string username = "default";
|
||||||
|
|
||||||
/** @brief Password passed to the
|
/** @brief Password passed to the
|
||||||
* [HELLO](https://redis.io/commands/hello/) command. If left
|
* [HELLO](https://redis.io/commands/hello/) command. If left
|
||||||
|
|||||||
@@ -30,6 +30,8 @@
|
|||||||
namespace boost::redis::detail
|
namespace boost::redis::detail
|
||||||
{
|
{
|
||||||
|
|
||||||
|
void push_hello(config const& cfg, request& req);
|
||||||
|
|
||||||
template <class Runner, class Connection, class Logger>
|
template <class Runner, class Connection, class Logger>
|
||||||
struct hello_op {
|
struct hello_op {
|
||||||
Runner* runner_ = nullptr;
|
Runner* runner_ = nullptr;
|
||||||
@@ -42,9 +44,6 @@ struct hello_op {
|
|||||||
{
|
{
|
||||||
BOOST_ASIO_CORO_REENTER (coro_)
|
BOOST_ASIO_CORO_REENTER (coro_)
|
||||||
{
|
{
|
||||||
runner_->hello_req_.clear();
|
|
||||||
if (runner_->hello_resp_.has_value())
|
|
||||||
runner_->hello_resp_.value().clear();
|
|
||||||
runner_->add_hello();
|
runner_->add_hello();
|
||||||
|
|
||||||
BOOST_ASIO_CORO_YIELD
|
BOOST_ASIO_CORO_YIELD
|
||||||
@@ -232,17 +231,10 @@ private:
|
|||||||
|
|
||||||
void add_hello()
|
void add_hello()
|
||||||
{
|
{
|
||||||
if (!cfg_.username.empty() && !cfg_.password.empty() && !cfg_.clientname.empty())
|
hello_req_.clear();
|
||||||
hello_req_.push("HELLO", "3", "AUTH", cfg_.username, cfg_.password, "SETNAME", cfg_.clientname);
|
if (hello_resp_.has_value())
|
||||||
else if (cfg_.username.empty() && cfg_.password.empty() && cfg_.clientname.empty())
|
hello_resp_.value().clear();
|
||||||
hello_req_.push("HELLO", "3");
|
push_hello(cfg_, hello_req_);
|
||||||
else if (cfg_.clientname.empty())
|
|
||||||
hello_req_.push("HELLO", "3", "AUTH", cfg_.username, cfg_.password);
|
|
||||||
else
|
|
||||||
hello_req_.push("HELLO", "3", "SETNAME", cfg_.clientname);
|
|
||||||
|
|
||||||
if (cfg_.database_index && cfg_.database_index.value() != 0)
|
|
||||||
hello_req_.push("SELECT", cfg_.database_index.value());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool has_error_in_response() const noexcept
|
bool has_error_in_response() const noexcept
|
||||||
|
|||||||
27
include/boost/redis/impl/runner.ipp
Normal file
27
include/boost/redis/impl/runner.ipp
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
/* Copyright (c) 2018-2024 Marcelo Zimbres Silva (mzimbres@gmail.com)
|
||||||
|
*
|
||||||
|
* Distributed under the Boost Software License, Version 1.0. (See
|
||||||
|
* accompanying file LICENSE.txt)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <boost/redis/detail/runner.hpp>
|
||||||
|
|
||||||
|
namespace boost::redis::detail
|
||||||
|
{
|
||||||
|
|
||||||
|
void push_hello(config const& cfg, request& req)
|
||||||
|
{
|
||||||
|
if (!cfg.username.empty() && !cfg.password.empty() && !cfg.clientname.empty())
|
||||||
|
req.push("HELLO", "3", "AUTH", cfg.username, cfg.password, "SETNAME", cfg.clientname);
|
||||||
|
else if (cfg.password.empty() && cfg.clientname.empty())
|
||||||
|
req.push("HELLO", "3");
|
||||||
|
else if (cfg.clientname.empty())
|
||||||
|
req.push("HELLO", "3", "AUTH", cfg.username, cfg.password);
|
||||||
|
else
|
||||||
|
req.push("HELLO", "3", "SETNAME", cfg.clientname);
|
||||||
|
|
||||||
|
if (cfg.database_index && cfg.database_index.value() != 0)
|
||||||
|
req.push("SELECT", cfg.database_index.value());
|
||||||
|
}
|
||||||
|
|
||||||
|
} // boost::redis::detail
|
||||||
@@ -10,6 +10,7 @@
|
|||||||
#include <boost/redis/impl/ignore.ipp>
|
#include <boost/redis/impl/ignore.ipp>
|
||||||
#include <boost/redis/impl/connection.ipp>
|
#include <boost/redis/impl/connection.ipp>
|
||||||
#include <boost/redis/impl/response.ipp>
|
#include <boost/redis/impl/response.ipp>
|
||||||
|
#include <boost/redis/impl/runner.ipp>
|
||||||
#include <boost/redis/resp3/impl/type.ipp>
|
#include <boost/redis/resp3/impl/type.ipp>
|
||||||
#include <boost/redis/resp3/impl/parser.ipp>
|
#include <boost/redis/resp3/impl/parser.ipp>
|
||||||
#include <boost/redis/resp3/impl/serialization.ipp>
|
#include <boost/redis/resp3/impl/serialization.ipp>
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
* accompanying file LICENSE.txt)
|
* accompanying file LICENSE.txt)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <boost/redis/detail/runner.hpp>
|
||||||
#include <boost/redis/resp3/serialization.hpp>
|
#include <boost/redis/resp3/serialization.hpp>
|
||||||
#include <boost/redis/adapter/adapt.hpp>
|
#include <boost/redis/adapter/adapt.hpp>
|
||||||
#define BOOST_TEST_MODULE conn-quit
|
#define BOOST_TEST_MODULE conn-quit
|
||||||
@@ -11,6 +12,9 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
using boost::redis::request;
|
||||||
|
using boost::redis::config;
|
||||||
|
using boost::redis::detail::push_hello;
|
||||||
using boost::redis::adapter::adapt2;
|
using boost::redis::adapter::adapt2;
|
||||||
using boost::redis::adapter::result;
|
using boost::redis::adapter::result;
|
||||||
using boost::redis::resp3::detail::deserialize;
|
using boost::redis::resp3::detail::deserialize;
|
||||||
@@ -31,3 +35,56 @@ BOOST_AUTO_TEST_CASE(low_level_sync_sans_io)
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(config_to_hello)
|
||||||
|
{
|
||||||
|
config cfg;
|
||||||
|
cfg.clientname = "";
|
||||||
|
request req;
|
||||||
|
|
||||||
|
push_hello(cfg, req);
|
||||||
|
|
||||||
|
std::string_view const expected = "*2\r\n$5\r\nHELLO\r\n$1\r\n3\r\n";
|
||||||
|
BOOST_CHECK_EQUAL(req.payload(), expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(config_to_hello_with_select)
|
||||||
|
{
|
||||||
|
config cfg;
|
||||||
|
cfg.clientname = "";
|
||||||
|
cfg.database_index = 10;
|
||||||
|
request req;
|
||||||
|
|
||||||
|
push_hello(cfg, req);
|
||||||
|
|
||||||
|
std::string_view const expected =
|
||||||
|
"*2\r\n$5\r\nHELLO\r\n$1\r\n3\r\n"
|
||||||
|
"*2\r\n$6\r\nSELECT\r\n$2\r\n10\r\n";
|
||||||
|
|
||||||
|
BOOST_CHECK_EQUAL(req.payload(), expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(config_to_hello_cmd_clientname)
|
||||||
|
{
|
||||||
|
config cfg;
|
||||||
|
request req;
|
||||||
|
|
||||||
|
push_hello(cfg, req);
|
||||||
|
|
||||||
|
std::string_view const expected = "*4\r\n$5\r\nHELLO\r\n$1\r\n3\r\n$7\r\nSETNAME\r\n$11\r\nBoost.Redis\r\n";
|
||||||
|
BOOST_CHECK_EQUAL(req.payload(), expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(config_to_hello_cmd_auth)
|
||||||
|
{
|
||||||
|
config cfg;
|
||||||
|
cfg.clientname = "";
|
||||||
|
cfg.username = "foo";
|
||||||
|
cfg.password = "bar";
|
||||||
|
request req;
|
||||||
|
|
||||||
|
push_hello(cfg, req);
|
||||||
|
|
||||||
|
std::string_view const expected = "*5\r\n$5\r\nHELLO\r\n$1\r\n3\r\n$4\r\nAUTH\r\n$3\r\nfoo\r\n$3\r\nbar\r\n";
|
||||||
|
BOOST_CHECK_EQUAL(req.payload(), expected);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user