Skip to content

Commit

Permalink
test: authority_view host functions
Browse files Browse the repository at this point in the history
This commit includes tests for the authority_view host functions when the host type is invalid.

This is a partial solution to #828, where authority_view.cpp has low coverage.
  • Loading branch information
alandefreitas committed Mar 15, 2024
1 parent 0f97a6d commit 9d4a4a5
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/authority_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,6 @@ encoded_host_address() const noexcept
std::size_t n;
switch(u_.host_type_)
{
default:
case urls::host_type::none:
BOOST_ASSERT(s.empty());
n = 0;
break;

case urls::host_type::name:
case urls::host_type::ipv4:
n = u_.decoded_[id_host];
Expand All @@ -224,6 +218,22 @@ encoded_host_address() const noexcept
n = u_.decoded_[id_host] - 2;
break;
}
// LCOV_EXCL_START
default:
case urls::host_type::none:
/*
* This condition is for correctness
* only.
* This should never happen, because
* the `host_rule` will set the host
* type to `name` when it's empty.
* This is correct because `reg-name`
* accepts empty strings.
*/
BOOST_ASSERT(s.empty());
n = 0;
break;
// LCOV_EXCL_STOP
}
return make_pct_string_view_unsafe(
s.data(), s.size(), n);
Expand Down
35 changes: 35 additions & 0 deletions test/unit/authority_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ class authority_view_test
yes(":%61@x", ":%61", ":a");
yes("%61%3a%62@x", "%61%3a%62", "a:b");

// issue 828
{
auto a = parse_authority("").value();
BOOST_TEST_NOT(a.has_userinfo());
BOOST_TEST(a.encoded_userinfo() == "");
BOOST_TEST(a.userinfo() == "");
}

{
auto a = parse_authority("@").value();
BOOST_TEST(a.has_userinfo());
Expand Down Expand Up @@ -178,6 +186,9 @@ class authority_view_test
host_type::name);
BOOST_TEST(a.encoded_host() ==
"");
BOOST_TEST(a.encoded_host_address() ==
"");
BOOST_TEST(a.encoded_host_name() == "");
BOOST_TEST(a.host_ipv4_address()
== ipv4_address());
BOOST_TEST(a.host_ipv6_address()
Expand All @@ -191,20 +202,27 @@ class authority_view_test
host_type::name);
BOOST_TEST(a.encoded_host() ==
"");
BOOST_TEST(a.encoded_host_address() ==
"");
BOOST_TEST(a.encoded_host_name() == "");
}
{
auto a = parse_authority("").value();
BOOST_TEST(a.host_type() ==
host_type::name);
BOOST_TEST(a.encoded_host() ==
"");
BOOST_TEST(a.encoded_host_address() ==
"");
}
{
auto a = parse_authority("www.example.com").value();
BOOST_TEST(a.host_type() ==
host_type::name);
BOOST_TEST(a.encoded_host() ==
"www.example.com");
BOOST_TEST(a.encoded_host_address() ==
"www.example.com");
BOOST_TEST(a.host() ==
"www.example.com");
}
Expand All @@ -214,6 +232,9 @@ class authority_view_test
host_type::ipv4);
BOOST_TEST(a.encoded_host() ==
"192.168.0.1");
BOOST_TEST(a.encoded_host_address() ==
"192.168.0.1");
BOOST_TEST(a.encoded_host_name() == "");
BOOST_TEST(a.host() ==
"192.168.0.1");
BOOST_TEST(
Expand All @@ -227,6 +248,8 @@ class authority_view_test
host_type::ipv6);
BOOST_TEST(a.encoded_host() ==
"[1::6:192.168.0.1]");
BOOST_TEST(a.encoded_host_address() ==
"1::6:192.168.0.1");
BOOST_TEST(a.host() ==
"[1::6:192.168.0.1]");
BOOST_TEST(a.host_ipv6_address() ==
Expand All @@ -239,6 +262,8 @@ class authority_view_test
host_type::ipvfuture);
BOOST_TEST(a.encoded_host() ==
"[v1.x]");
BOOST_TEST(a.encoded_host_address() ==
"v1.x");
BOOST_TEST(a.host() ==
"[v1.x]");
BOOST_TEST(a.host_ipvfuture() == "v1.x");
Expand Down Expand Up @@ -334,6 +359,15 @@ class authority_view_test
}
}

void
testOStream()
{
authority_view a( "user:pass@www.example.com:8080" );
std::ostringstream os;
os << a;
BOOST_TEST_EQ( os.str(), "user:pass@www.example.com:8080" );
}

void
run()
{
Expand All @@ -357,6 +391,7 @@ class authority_view_test
testHost();
testPort();
testHostAndPort();
testOStream();
}
};

Expand Down

0 comments on commit 9d4a4a5

Please sign in to comment.