Skip to content

Commit 64d6030

Browse files
committed
Added an improvement to the equality operation to be able to compare the URI query, independently of the order of the URI query arguments.
1 parent e874ef6 commit 64d6030

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

include/network/uri/uri.hpp

+10-8
Original file line numberDiff line numberDiff line change
@@ -312,29 +312,31 @@ std::size_t hash_value(const uri &uri_)
312312
return seed;
313313
}
314314

315-
inline
316-
bool operator == (const uri &lhs, const uri &rhs) {
317-
return boost::equal(lhs, rhs);
318-
}
315+
//inline
316+
//bool operator == (const uri &lhs, const uri &rhs) {
317+
// return boost::equal(lhs, rhs);
318+
//}
319+
320+
bool operator == (const uri &lhs, const uri &rhs);
319321

320322
inline
321323
bool operator == (const uri &lhs, const uri::string_type &rhs) {
322-
return boost::equal(lhs, rhs);
324+
return lhs == uri(rhs);
323325
}
324326

325327
inline
326328
bool operator == (const uri::string_type &lhs, const uri &rhs) {
327-
return boost::equal(lhs, rhs);
329+
return uri(lhs) == rhs;
328330
}
329331

330332
inline
331333
bool operator == (const uri &lhs, const uri::value_type *rhs) {
332-
return boost::equal(lhs, boost::as_literal(rhs));
334+
return lhs == uri(rhs);
333335
}
334336

335337
inline
336338
bool operator == (const uri::value_type *lhs, const uri &rhs) {
337-
return boost::equal(boost::as_literal(lhs), rhs);
339+
return uri(lhs) == rhs;
338340
}
339341

340342
inline

libs/network/src/uri/uri.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,30 @@
55

66

77
#include <network/uri/uri.ipp>
8+
#include <network/uri/uri.hpp>
9+
#include <map>
10+
11+
#include <iterator>
12+
#include <iostream>
13+
14+
namespace network {
15+
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())));
19+
if (equal)
20+
{
21+
// TODO: test normalized paths
22+
equal = boost::equal(lhs.path_range(), rhs.path_range());
23+
}
24+
25+
if (equal)
26+
{
27+
// test query order
28+
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));
30+
}
31+
32+
return equal;
33+
}
34+
} // namespace network

libs/network/test/uri/uri_test.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,12 @@ BOOST_AUTO_TEST_CASE(equality_test_4) {
363363
BOOST_CHECK(uri_1.c_str() == uri_2);
364364
}
365365

366+
BOOST_AUTO_TEST_CASE(equality_test_reordered_query) {
367+
network::uri uri_1("http://www.example.com/?a=1&b=2");
368+
network::uri uri_2("http://www.example.com/?b=2&a=1");
369+
BOOST_CHECK(uri_1 == uri_2);
370+
}
371+
366372
BOOST_AUTO_TEST_CASE(inequality_test) {
367373
network::uri uri_1("http://www.example.com/");
368374
network::uri uri_2("http://www.example.com/");
@@ -454,6 +460,7 @@ BOOST_AUTO_TEST_CASE(issue_67_test) {
454460
}
455461

456462
BOOST_AUTO_TEST_CASE(from_parts_1) {
463+
std::cout << __FUNCTION__ << std::endl;
457464
BOOST_CHECK_EQUAL(network::uri("http://www.example.com/path?query#fragment"),
458465
network::from_parts(network::uri("http://www.example.com"), "/path", "query", "fragment"));
459466
}

0 commit comments

Comments
 (0)