Skip to content

Commit 72f94ec

Browse files
committed
Added some tests to access the default port given a URI scheme.
1 parent 64d6030 commit 72f94ec

File tree

6 files changed

+118
-39
lines changed

6 files changed

+118
-39
lines changed

include/network/uri/schemes.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
#ifndef __BOOST_NETWORK_URI_SCHEMES_INC__
88
# define __BOOST_NETWORK_URI_SCHEMES_INC__
99

10-
1110
#include <string>
12-
11+
#include <boost/optional.hpp>
1312

1413
namespace network {
1514
class hierarchical_schemes {
@@ -27,6 +26,8 @@ class opaque_schemes {
2726
static bool exists(const std::string &scheme);
2827

2928
};
29+
30+
boost::optional<std::string> default_port(const std::string &scheme);
3031
} // namespace network
3132

3233

libs/network/src/uri/schemes.cpp

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,39 @@
33
// (See accompanying file LICENSE_1_0.txt or copy at
44
// http://www.boost.org/LICENSE_1_0.txt)
55

6-
76
#include <network/uri/schemes.hpp>
8-
#include <boost/unordered_set.hpp>
9-
7+
#include <boost/unordered_map.hpp>
108

119
namespace network {
1210
namespace {
13-
static boost::unordered_set<std::string> hierarchical_schemes_;
14-
static boost::unordered_set<std::string> opaque_schemes_;
11+
static boost::unordered_map<std::string, std::string> hierarchical_schemes_;
12+
static boost::unordered_map<std::string, std::string> opaque_schemes_;
1513

1614
bool register_hierarchical_schemes() {
17-
hierarchical_schemes_.insert("http");
18-
hierarchical_schemes_.insert("https");
19-
hierarchical_schemes_.insert("shttp");
20-
hierarchical_schemes_.insert("ftp");
21-
hierarchical_schemes_.insert("file");
22-
hierarchical_schemes_.insert("dns");
23-
hierarchical_schemes_.insert("nfs");
24-
hierarchical_schemes_.insert("imap");
25-
hierarchical_schemes_.insert("nntp");
26-
hierarchical_schemes_.insert("pop");
27-
hierarchical_schemes_.insert("rsync");
28-
hierarchical_schemes_.insert("snmp");
29-
hierarchical_schemes_.insert("telnet");
30-
hierarchical_schemes_.insert("svn");
31-
hierarchical_schemes_.insert("svn+ssh");
32-
hierarchical_schemes_.insert("git");
33-
hierarchical_schemes_.insert("git+ssh");
15+
hierarchical_schemes_.insert(std::make_pair(std::string("http"), std::string("80")));
16+
hierarchical_schemes_.insert(std::make_pair(std::string("https"), std::string("443")));
17+
hierarchical_schemes_.insert(std::make_pair(std::string("shttp"), std::string("")));
18+
hierarchical_schemes_.insert(std::make_pair(std::string("ftp"), std::string("21")));
19+
hierarchical_schemes_.insert(std::make_pair(std::string("file"), std::string("")));
20+
hierarchical_schemes_.insert(std::make_pair(std::string("dns"), std::string("53")));
21+
hierarchical_schemes_.insert(std::make_pair(std::string("nfs"), std::string("2049")));
22+
hierarchical_schemes_.insert(std::make_pair(std::string("imap"), std::string("143")));
23+
hierarchical_schemes_.insert(std::make_pair(std::string("nntp"), std::string("")));
24+
hierarchical_schemes_.insert(std::make_pair(std::string("pop"), std::string("119")));
25+
hierarchical_schemes_.insert(std::make_pair(std::string("rsync"), std::string("873")));
26+
hierarchical_schemes_.insert(std::make_pair(std::string("snmp"), std::string("161")));
27+
hierarchical_schemes_.insert(std::make_pair(std::string("telnet"), std::string("23")));
28+
hierarchical_schemes_.insert(std::make_pair(std::string("svn"), std::string("3690")));
29+
hierarchical_schemes_.insert(std::make_pair(std::string("svn+ssh"), std::string("")));
30+
hierarchical_schemes_.insert(std::make_pair(std::string("git"), std::string("9418")));
31+
hierarchical_schemes_.insert(std::make_pair(std::string("git+ssh"), std::string("")));
3432
return true;
3533
}
3634

3735
bool register_opaque_schemes() {
38-
opaque_schemes_.insert("mailto");
39-
opaque_schemes_.insert("news");
40-
opaque_schemes_.insert("im");
41-
opaque_schemes_.insert("sip");
42-
opaque_schemes_.insert("sms");
43-
opaque_schemes_.insert("xmpp");
36+
opaque_schemes_.insert(std::make_pair(std::string("mailto"), std::string("25")));
37+
opaque_schemes_.insert(std::make_pair(std::string("sip"), std::string("5060")));
38+
opaque_schemes_.insert(std::make_pair(std::string("xmpp"), std::string("5222")));
4439
return true;
4540
}
4641

@@ -50,10 +45,28 @@ static bool opaque = register_opaque_schemes();
5045
} // namespace
5146

5247
bool hierarchical_schemes::exists(const std::string &scheme) {
53-
return hierarchical_schemes_.end() != hierarchical_schemes_.find(scheme);
48+
return std::end(hierarchical_schemes_) != hierarchical_schemes_.find(scheme);
5449
}
5550

5651
bool opaque_schemes::exists(const std::string &scheme) {
57-
return opaque_schemes_.end() != opaque_schemes_.find(scheme);
52+
return std::end(opaque_schemes_) != opaque_schemes_.find(scheme);
53+
}
54+
55+
boost::optional<std::string> default_port(const std::string &scheme) {
56+
auto it = hierarchical_schemes_.find(scheme);
57+
if (it != std::end(hierarchical_schemes_)) {
58+
if (!it->second.empty()) {
59+
return it->second;
60+
}
61+
}
62+
63+
it = opaque_schemes_.find(scheme);
64+
if (it != std::end(opaque_schemes_)) {
65+
if (!it->second.empty()) {
66+
return it->second;
67+
}
68+
}
69+
70+
return boost::optional<std::string>();
5871
}
5972
} // namespace network

libs/network/src/uri/uri.cpp

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,49 @@
66

77
#include <network/uri/uri.ipp>
88
#include <network/uri/uri.hpp>
9+
#include <boost/algorithm/string/predicate.hpp>
910
#include <map>
1011

1112
#include <iterator>
1213
#include <iostream>
1314

1415
namespace network {
1516
bool operator == (const uri &lhs, const uri &rhs) {
16-
bool equal = boost::equal(
17-
std::make_pair(std::begin(lhs.scheme_range()), std::begin(lhs.path_range())),
18-
std::make_pair(std::begin(rhs.scheme_range()), std::begin(rhs.path_range())));
17+
// the scheme can be compared insensitive to case
18+
bool equal = boost::iequals(lhs.scheme_range(), rhs.scheme_range());
19+
if (equal)
20+
{
21+
// the user info must be case sensitive
22+
equal = boost::equals(lhs.user_info_range(), rhs.user_info_range());
23+
}
24+
25+
if (equal)
26+
{
27+
// the host can be compared insensitive to case
28+
equal = boost::iequals(
29+
std::make_pair(std::begin(lhs.host_range()), std::end(lhs.host_range())),
30+
std::make_pair(std::begin(rhs.host_range()), std::end(rhs.host_range())));
31+
}
32+
33+
if (equal)
34+
{
35+
// TODO: test default ports according to scheme
36+
equal = boost::equals(
37+
std::make_pair(std::begin(lhs.port_range()), std::end(lhs.port_range())),
38+
std::make_pair(std::begin(rhs.port_range()), std::end(rhs.port_range())));
39+
}
40+
1941
if (equal)
2042
{
2143
// TODO: test normalized paths
22-
equal = boost::equal(lhs.path_range(), rhs.path_range());
44+
equal = boost::iequals(lhs.path_range(), rhs.path_range());
2345
}
2446

2547
if (equal)
2648
{
27-
// test query order
49+
// test query, independent of order
2850
std::map<uri::string_type, uri::string_type> lhs_query_params, rhs_query_params;
29-
equal = (query_map(lhs, lhs_query_params) == query_map(rhs, rhs_query_params));
51+
equal = (query_map(lhs, lhs_query_params) == query_map(rhs, rhs_query_params));
3052
}
3153

3254
return equal;

libs/network/test/uri/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ if (Boost_FOUND)
1515
uri_builder_stream_test
1616
uri_encoding_test
1717
relative_uri_test
18+
scheme_tests
1819
)
1920
foreach (test ${TESTS})
2021
if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2012 Glyn Matthews.
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE_1_0.txt or copy at
4+
// http://www.boost.org/LICENSE_1_0.txt)
5+
6+
#define BOOST_TEST_MODULE URI Scheme Test
7+
#include <boost/config/warning_disable.hpp>
8+
#include <boost/test/unit_test.hpp>
9+
#include <network/uri.hpp>
10+
11+
BOOST_AUTO_TEST_CASE(http_has_default_port) {
12+
BOOST_CHECK(network::default_port("http"));
13+
}
14+
15+
BOOST_AUTO_TEST_CASE(http_default_port) {
16+
BOOST_CHECK_EQUAL(std::string("80"), network::default_port("http"));
17+
}
18+
19+
BOOST_AUTO_TEST_CASE(https_has_default_port) {
20+
BOOST_CHECK(network::default_port("https"));
21+
}
22+
23+
BOOST_AUTO_TEST_CASE(https_default_port) {
24+
BOOST_CHECK_EQUAL(std::string("443"), network::default_port("https"));
25+
}

libs/network/test/uri/uri_test.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,24 @@ BOOST_AUTO_TEST_CASE(equality_test_reordered_query) {
369369
BOOST_CHECK(uri_1 == uri_2);
370370
}
371371

372+
BOOST_AUTO_TEST_CASE(equality_test_capitalized_scheme) {
373+
network::uri uri_1("http://www.example.com/");
374+
network::uri uri_2("HTTP://www.example.com/");
375+
BOOST_CHECK(uri_1 == uri_2);
376+
}
377+
378+
BOOST_AUTO_TEST_CASE(equality_test_capitalized_host) {
379+
network::uri uri_1("http://www.example.com/");
380+
network::uri uri_2("http://WWW.EXAMPLE.COM/");
381+
BOOST_CHECK(uri_1 == uri_2);
382+
}
383+
384+
BOOST_AUTO_TEST_CASE(equality_test_user_info) {
385+
network::uri uri_1("ftp://john.doe@ftp.example.com/");
386+
network::uri uri_2("ftp://JOHN.DOE@ftp.example.com/");
387+
BOOST_CHECK(uri_1 != uri_2);
388+
}
389+
372390
BOOST_AUTO_TEST_CASE(inequality_test) {
373391
network::uri uri_1("http://www.example.com/");
374392
network::uri uri_2("http://www.example.com/");
@@ -460,7 +478,6 @@ BOOST_AUTO_TEST_CASE(issue_67_test) {
460478
}
461479

462480
BOOST_AUTO_TEST_CASE(from_parts_1) {
463-
std::cout << __FUNCTION__ << std::endl;
464481
BOOST_CHECK_EQUAL(network::uri("http://www.example.com/path?query#fragment"),
465482
network::from_parts(network::uri("http://www.example.com"), "/path", "query", "fragment"));
466483
}

0 commit comments

Comments
 (0)