Skip to content

Commit 30fcbc9

Browse files
committed
Refactors add_hello and adds unit tests.
1 parent a6b8017 commit 30fcbc9

File tree

6 files changed

+96
-15
lines changed

6 files changed

+96
-15
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,10 @@ https://lists.boost.org/Archives/boost/2023/01/253944.php.
698698
[this](https://github.com/boostorg/redis/issues/181#issuecomment-1913346983)
699699
comment.
700700

701+
* ([Issue 182](https://github.com/boostorg/redis/issues/182)).
702+
Sets `"default"` as the default value of `config::username`. This
703+
makes it simpler to use the `requirepass` configuration in Redis.
704+
701705
### Boost 1.84 (First release in Boost)
702706

703707
* Deprecates the `async_receive` overload that takes a response. Users

include/boost/redis/config.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct config {
3838
* [HELLO](https://redis.io/commands/hello/) command. If left
3939
* empty `HELLO` will be sent without authentication parameters.
4040
*/
41-
std::string username;
41+
std::string username = "default";
4242

4343
/** @brief Password passed to the
4444
* [HELLO](https://redis.io/commands/hello/) command. If left

include/boost/redis/detail/runner.hpp

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
namespace boost::redis::detail
3131
{
3232

33+
void push_hello(config const& cfg, request& req);
34+
3335
template <class Runner, class Connection, class Logger>
3436
struct hello_op {
3537
Runner* runner_ = nullptr;
@@ -42,9 +44,6 @@ struct hello_op {
4244
{
4345
BOOST_ASIO_CORO_REENTER (coro_)
4446
{
45-
runner_->hello_req_.clear();
46-
if (runner_->hello_resp_.has_value())
47-
runner_->hello_resp_.value().clear();
4847
runner_->add_hello();
4948

5049
BOOST_ASIO_CORO_YIELD
@@ -232,17 +231,10 @@ class runner {
232231

233232
void add_hello()
234233
{
235-
if (!cfg_.username.empty() && !cfg_.password.empty() && !cfg_.clientname.empty())
236-
hello_req_.push("HELLO", "3", "AUTH", cfg_.username, cfg_.password, "SETNAME", cfg_.clientname);
237-
else if (cfg_.username.empty() && cfg_.password.empty() && cfg_.clientname.empty())
238-
hello_req_.push("HELLO", "3");
239-
else if (cfg_.clientname.empty())
240-
hello_req_.push("HELLO", "3", "AUTH", cfg_.username, cfg_.password);
241-
else
242-
hello_req_.push("HELLO", "3", "SETNAME", cfg_.clientname);
243-
244-
if (cfg_.database_index && cfg_.database_index.value() != 0)
245-
hello_req_.push("SELECT", cfg_.database_index.value());
234+
hello_req_.clear();
235+
if (hello_resp_.has_value())
236+
hello_resp_.value().clear();
237+
push_hello(cfg_, hello_req_);
246238
}
247239

248240
bool has_error_in_response() const noexcept

include/boost/redis/impl/runner.ipp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* Copyright (c) 2018-2024 Marcelo Zimbres Silva ([email protected])
2+
*
3+
* Distributed under the Boost Software License, Version 1.0. (See
4+
* accompanying file LICENSE.txt)
5+
*/
6+
7+
#include <boost/redis/detail/runner.hpp>
8+
9+
namespace boost::redis::detail
10+
{
11+
12+
void push_hello(config const& cfg, request& req)
13+
{
14+
if (!cfg.username.empty() && !cfg.password.empty() && !cfg.clientname.empty())
15+
req.push("HELLO", "3", "AUTH", cfg.username, cfg.password, "SETNAME", cfg.clientname);
16+
else if (cfg.password.empty() && cfg.clientname.empty())
17+
req.push("HELLO", "3");
18+
else if (cfg.clientname.empty())
19+
req.push("HELLO", "3", "AUTH", cfg.username, cfg.password);
20+
else
21+
req.push("HELLO", "3", "SETNAME", cfg.clientname);
22+
23+
if (cfg.database_index && cfg.database_index.value() != 0)
24+
req.push("SELECT", cfg.database_index.value());
25+
}
26+
27+
} // boost::redis::detail

include/boost/redis/src.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <boost/redis/impl/ignore.ipp>
1111
#include <boost/redis/impl/connection.ipp>
1212
#include <boost/redis/impl/response.ipp>
13+
#include <boost/redis/impl/runner.ipp>
1314
#include <boost/redis/resp3/impl/type.ipp>
1415
#include <boost/redis/resp3/impl/parser.ipp>
1516
#include <boost/redis/resp3/impl/serialization.ipp>

test/test_low_level_sync_sans_io.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44
* accompanying file LICENSE.txt)
55
*/
66

7+
#include <boost/redis/detail/runner.hpp>
78
#include <boost/redis/resp3/serialization.hpp>
89
#include <boost/redis/adapter/adapt.hpp>
910
#define BOOST_TEST_MODULE conn-quit
1011
#include <boost/test/included/unit_test.hpp>
1112
#include <string>
1213
#include <iostream>
1314

15+
using boost::redis::request;
16+
using boost::redis::config;
17+
using boost::redis::detail::push_hello;
1418
using boost::redis::adapter::adapt2;
1519
using boost::redis::adapter::result;
1620
using boost::redis::resp3::detail::deserialize;
@@ -31,3 +35,56 @@ BOOST_AUTO_TEST_CASE(low_level_sync_sans_io)
3135
exit(EXIT_FAILURE);
3236
}
3337
}
38+
39+
BOOST_AUTO_TEST_CASE(config_to_hello)
40+
{
41+
config cfg;
42+
cfg.clientname = "";
43+
request req;
44+
45+
push_hello(cfg, req);
46+
47+
std::string_view const expected = "*2\r\n$5\r\nHELLO\r\n$1\r\n3\r\n";
48+
BOOST_CHECK_EQUAL(req.payload(), expected);
49+
}
50+
51+
BOOST_AUTO_TEST_CASE(config_to_hello_with_select)
52+
{
53+
config cfg;
54+
cfg.clientname = "";
55+
cfg.database_index = 10;
56+
request req;
57+
58+
push_hello(cfg, req);
59+
60+
std::string_view const expected =
61+
"*2\r\n$5\r\nHELLO\r\n$1\r\n3\r\n"
62+
"*2\r\n$6\r\nSELECT\r\n$2\r\n10\r\n";
63+
64+
BOOST_CHECK_EQUAL(req.payload(), expected);
65+
}
66+
67+
BOOST_AUTO_TEST_CASE(config_to_hello_cmd_clientname)
68+
{
69+
config cfg;
70+
request req;
71+
72+
push_hello(cfg, req);
73+
74+
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";
75+
BOOST_CHECK_EQUAL(req.payload(), expected);
76+
}
77+
78+
BOOST_AUTO_TEST_CASE(config_to_hello_cmd_auth)
79+
{
80+
config cfg;
81+
cfg.clientname = "";
82+
cfg.username = "foo";
83+
cfg.password = "bar";
84+
request req;
85+
86+
push_hello(cfg, req);
87+
88+
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";
89+
BOOST_CHECK_EQUAL(req.payload(), expected);
90+
}

0 commit comments

Comments
 (0)