Skip to content

Commit

Permalink
Added some tests to access the default port given a URI scheme.
Browse files Browse the repository at this point in the history
  • Loading branch information
glynos committed Jul 29, 2012
1 parent 64d6030 commit 72f94ec
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 39 deletions.
5 changes: 3 additions & 2 deletions include/network/uri/schemes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
#ifndef __BOOST_NETWORK_URI_SCHEMES_INC__
# define __BOOST_NETWORK_URI_SCHEMES_INC__


#include <string>

#include <boost/optional.hpp>

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

};

boost::optional<std::string> default_port(const std::string &scheme);
} // namespace network


Expand Down
73 changes: 43 additions & 30 deletions libs/network/src/uri/schemes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,39 @@
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)


#include <network/uri/schemes.hpp>
#include <boost/unordered_set.hpp>

#include <boost/unordered_map.hpp>

namespace network {
namespace {
static boost::unordered_set<std::string> hierarchical_schemes_;
static boost::unordered_set<std::string> opaque_schemes_;
static boost::unordered_map<std::string, std::string> hierarchical_schemes_;
static boost::unordered_map<std::string, std::string> opaque_schemes_;

bool register_hierarchical_schemes() {
hierarchical_schemes_.insert("http");
hierarchical_schemes_.insert("https");
hierarchical_schemes_.insert("shttp");
hierarchical_schemes_.insert("ftp");
hierarchical_schemes_.insert("file");
hierarchical_schemes_.insert("dns");
hierarchical_schemes_.insert("nfs");
hierarchical_schemes_.insert("imap");
hierarchical_schemes_.insert("nntp");
hierarchical_schemes_.insert("pop");
hierarchical_schemes_.insert("rsync");
hierarchical_schemes_.insert("snmp");
hierarchical_schemes_.insert("telnet");
hierarchical_schemes_.insert("svn");
hierarchical_schemes_.insert("svn+ssh");
hierarchical_schemes_.insert("git");
hierarchical_schemes_.insert("git+ssh");
hierarchical_schemes_.insert(std::make_pair(std::string("http"), std::string("80")));
hierarchical_schemes_.insert(std::make_pair(std::string("https"), std::string("443")));
hierarchical_schemes_.insert(std::make_pair(std::string("shttp"), std::string("")));
hierarchical_schemes_.insert(std::make_pair(std::string("ftp"), std::string("21")));
hierarchical_schemes_.insert(std::make_pair(std::string("file"), std::string("")));
hierarchical_schemes_.insert(std::make_pair(std::string("dns"), std::string("53")));
hierarchical_schemes_.insert(std::make_pair(std::string("nfs"), std::string("2049")));
hierarchical_schemes_.insert(std::make_pair(std::string("imap"), std::string("143")));
hierarchical_schemes_.insert(std::make_pair(std::string("nntp"), std::string("")));
hierarchical_schemes_.insert(std::make_pair(std::string("pop"), std::string("119")));
hierarchical_schemes_.insert(std::make_pair(std::string("rsync"), std::string("873")));
hierarchical_schemes_.insert(std::make_pair(std::string("snmp"), std::string("161")));
hierarchical_schemes_.insert(std::make_pair(std::string("telnet"), std::string("23")));
hierarchical_schemes_.insert(std::make_pair(std::string("svn"), std::string("3690")));
hierarchical_schemes_.insert(std::make_pair(std::string("svn+ssh"), std::string("")));
hierarchical_schemes_.insert(std::make_pair(std::string("git"), std::string("9418")));
hierarchical_schemes_.insert(std::make_pair(std::string("git+ssh"), std::string("")));
return true;
}

bool register_opaque_schemes() {
opaque_schemes_.insert("mailto");
opaque_schemes_.insert("news");
opaque_schemes_.insert("im");
opaque_schemes_.insert("sip");
opaque_schemes_.insert("sms");
opaque_schemes_.insert("xmpp");
opaque_schemes_.insert(std::make_pair(std::string("mailto"), std::string("25")));
opaque_schemes_.insert(std::make_pair(std::string("sip"), std::string("5060")));
opaque_schemes_.insert(std::make_pair(std::string("xmpp"), std::string("5222")));
return true;
}

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

bool hierarchical_schemes::exists(const std::string &scheme) {
return hierarchical_schemes_.end() != hierarchical_schemes_.find(scheme);
return std::end(hierarchical_schemes_) != hierarchical_schemes_.find(scheme);
}

bool opaque_schemes::exists(const std::string &scheme) {
return opaque_schemes_.end() != opaque_schemes_.find(scheme);
return std::end(opaque_schemes_) != opaque_schemes_.find(scheme);
}

boost::optional<std::string> default_port(const std::string &scheme) {
auto it = hierarchical_schemes_.find(scheme);
if (it != std::end(hierarchical_schemes_)) {
if (!it->second.empty()) {
return it->second;
}
}

it = opaque_schemes_.find(scheme);
if (it != std::end(opaque_schemes_)) {
if (!it->second.empty()) {
return it->second;
}
}

return boost::optional<std::string>();
}
} // namespace network
34 changes: 28 additions & 6 deletions libs/network/src/uri/uri.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,49 @@

#include <network/uri/uri.ipp>
#include <network/uri/uri.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <map>

#include <iterator>
#include <iostream>

namespace network {
bool operator == (const uri &lhs, const uri &rhs) {
bool equal = boost::equal(
std::make_pair(std::begin(lhs.scheme_range()), std::begin(lhs.path_range())),
std::make_pair(std::begin(rhs.scheme_range()), std::begin(rhs.path_range())));
// the scheme can be compared insensitive to case
bool equal = boost::iequals(lhs.scheme_range(), rhs.scheme_range());
if (equal)
{
// the user info must be case sensitive
equal = boost::equals(lhs.user_info_range(), rhs.user_info_range());
}

if (equal)
{
// the host can be compared insensitive to case
equal = boost::iequals(
std::make_pair(std::begin(lhs.host_range()), std::end(lhs.host_range())),
std::make_pair(std::begin(rhs.host_range()), std::end(rhs.host_range())));
}

if (equal)
{
// TODO: test default ports according to scheme
equal = boost::equals(
std::make_pair(std::begin(lhs.port_range()), std::end(lhs.port_range())),
std::make_pair(std::begin(rhs.port_range()), std::end(rhs.port_range())));
}

if (equal)
{
// TODO: test normalized paths
equal = boost::equal(lhs.path_range(), rhs.path_range());
equal = boost::iequals(lhs.path_range(), rhs.path_range());
}

if (equal)
{
// test query order
// test query, independent of order
std::map<uri::string_type, uri::string_type> lhs_query_params, rhs_query_params;
equal = (query_map(lhs, lhs_query_params) == query_map(rhs, rhs_query_params));
equal = (query_map(lhs, lhs_query_params) == query_map(rhs, rhs_query_params));
}

return equal;
Expand Down
1 change: 1 addition & 0 deletions libs/network/test/uri/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ if (Boost_FOUND)
uri_builder_stream_test
uri_encoding_test
relative_uri_test
scheme_tests
)
foreach (test ${TESTS})
if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
Expand Down
25 changes: 25 additions & 0 deletions libs/network/test/uri/scheme_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2012 Glyn Matthews.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#define BOOST_TEST_MODULE URI Scheme Test
#include <boost/config/warning_disable.hpp>
#include <boost/test/unit_test.hpp>
#include <network/uri.hpp>

BOOST_AUTO_TEST_CASE(http_has_default_port) {
BOOST_CHECK(network::default_port("http"));
}

BOOST_AUTO_TEST_CASE(http_default_port) {
BOOST_CHECK_EQUAL(std::string("80"), network::default_port("http"));
}

BOOST_AUTO_TEST_CASE(https_has_default_port) {
BOOST_CHECK(network::default_port("https"));
}

BOOST_AUTO_TEST_CASE(https_default_port) {
BOOST_CHECK_EQUAL(std::string("443"), network::default_port("https"));
}
19 changes: 18 additions & 1 deletion libs/network/test/uri/uri_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,24 @@ BOOST_AUTO_TEST_CASE(equality_test_reordered_query) {
BOOST_CHECK(uri_1 == uri_2);
}

BOOST_AUTO_TEST_CASE(equality_test_capitalized_scheme) {
network::uri uri_1("http://www.example.com/");
network::uri uri_2("HTTP://www.example.com/");
BOOST_CHECK(uri_1 == uri_2);
}

BOOST_AUTO_TEST_CASE(equality_test_capitalized_host) {
network::uri uri_1("http://www.example.com/");
network::uri uri_2("http://WWW.EXAMPLE.COM/");
BOOST_CHECK(uri_1 == uri_2);
}

BOOST_AUTO_TEST_CASE(equality_test_user_info) {
network::uri uri_1("ftp://john.doe@ftp.example.com/");
network::uri uri_2("ftp://JOHN.DOE@ftp.example.com/");
BOOST_CHECK(uri_1 != uri_2);
}

BOOST_AUTO_TEST_CASE(inequality_test) {
network::uri uri_1("http://www.example.com/");
network::uri uri_2("http://www.example.com/");
Expand Down Expand Up @@ -460,7 +478,6 @@ BOOST_AUTO_TEST_CASE(issue_67_test) {
}

BOOST_AUTO_TEST_CASE(from_parts_1) {
std::cout << __FUNCTION__ << std::endl;
BOOST_CHECK_EQUAL(network::uri("http://www.example.com/path?query#fragment"),
network::from_parts(network::uri("http://www.example.com"), "/path", "query", "fragment"));
}
Expand Down

0 comments on commit 72f94ec

Please sign in to comment.