diff --git a/doc/Changelog.md b/doc/Changelog.md index caf736a..db4902c 100644 --- a/doc/Changelog.md +++ b/doc/Changelog.md @@ -2,6 +2,8 @@ # Changelog {#Changelog} # git master {#master} +* [43](https://github.com/HBPVis/Servus/pull/43): + Allow URI queries without value # Release 1.3 (07-03-2015) diff --git a/servus/serializable.h b/servus/serializable.h index 997dc3a..fb7d75b 100644 --- a/servus/serializable.h +++ b/servus/serializable.h @@ -75,7 +75,7 @@ class Serializable */ bool fromJSON( const std::string& json ) { return _fromJSON( json ); } - /** @return the JSON representation of this object. */ + /** @return the JSON representation of this serializable. */ std::string toJSON() const { return _toJSON(); } //@} diff --git a/servus/uri.cpp b/servus/uri.cpp index eea0d3c..72d6245 100644 --- a/servus/uri.cpp +++ b/servus/uri.cpp @@ -1,6 +1,6 @@ -/* Copyright (c) 2013-2015, Human Brain Project +/* Copyright (c) 2013-2016, Human Brain Project * Ahmet.Bilgili@epfl.ch - * 2015, Juan Hernando + * Juan Hernando * * This file is part of Servus * @@ -167,9 +167,12 @@ void _parseQueryMap( URIData& data ) query = query.substr( nextPair + 1 ); const size_t eq = pair.find( '=' ); - if( eq == std::string::npos || eq == 0 ) + if( eq == 0 ) // empty key continue; - data.queryMap[ pair.substr( 0, eq ) ] = pair.substr( eq + 1 ); + if( eq == std::string::npos ) // empty value + data.queryMap[ pair ] = std::string(); + else + data.queryMap[ pair.substr( 0, eq ) ] = pair.substr( eq + 1 ); } } diff --git a/tests/uri.cpp b/tests/uri.cpp index 54e1200..5ae4a27 100644 --- a/tests/uri.cpp +++ b/tests/uri.cpp @@ -1,5 +1,5 @@ -/* Copyright (c) 2013-2014, ahmet.bilgili@epfl.ch - * 2015, Juan Hernando +/* Copyright (c) 2013-2016, Ahmet.Bilgili@epfl.ch + * Juan Hernando * * This file is part of Servus * @@ -23,7 +23,7 @@ #include -BOOST_AUTO_TEST_CASE(test_uri_parts) +BOOST_AUTO_TEST_CASE(uri_parts) { const std::string uriStr = "http://bob@www.example.com:8080/path/?key=value,foo=bar#fragment"; @@ -79,7 +79,7 @@ BOOST_AUTO_TEST_CASE(test_uri_parts) BOOST_CHECK_EQUAL( fragment.getFragment(), "fragment,no,query" ); } -BOOST_AUTO_TEST_CASE(test_setters) +BOOST_AUTO_TEST_CASE(setters) { servus::URI uri; uri.setScheme( "foo" ); @@ -96,7 +96,7 @@ BOOST_AUTO_TEST_CASE(test_setters) // The query setter is tested independently. } -BOOST_AUTO_TEST_CASE(test_empty_uri) +BOOST_AUTO_TEST_CASE(empty_uri) { servus::URI empty; BOOST_CHECK( empty.getScheme().empty( )); @@ -108,7 +108,7 @@ BOOST_AUTO_TEST_CASE(test_empty_uri) BOOST_CHECK( empty.getFragment().empty( )); } -BOOST_AUTO_TEST_CASE(test_file_uris) +BOOST_AUTO_TEST_CASE(file_uris) { servus::URI file1( "/bla.txt" ); BOOST_CHECK_EQUAL( file1.getPath(), "/bla.txt" ); @@ -147,7 +147,7 @@ BOOST_AUTO_TEST_CASE(test_file_uris) BOOST_CHECK( file5.getFragment().empty( )); } -BOOST_AUTO_TEST_CASE(test_uri_query) +BOOST_AUTO_TEST_CASE(uri_query) { const std::string uriStr = "http://bob@www.example.com:8080/path/?key=value,foo=bar#fragment"; @@ -166,7 +166,7 @@ BOOST_AUTO_TEST_CASE(test_uri_query) BOOST_CHECK( uri.getQuery().find( "hans=dampf" ) != std::string::npos ); } -BOOST_AUTO_TEST_CASE(test_uri_comparisons) +BOOST_AUTO_TEST_CASE(uri_comparisons) { const std::string uriStr = "http://bob@www.example.com:8080/path/?key=value,foo=bar#fragment"; @@ -194,7 +194,7 @@ BOOST_AUTO_TEST_CASE(test_uri_comparisons) BOOST_CHECK_EQUAL( sstr.str(), "/path" ); } -BOOST_AUTO_TEST_CASE(test_invalid_uri) +BOOST_AUTO_TEST_CASE(invalid_uri) { BOOST_CHECK_THROW( servus::URI uri( "bad_schema://" ), std::exception ); @@ -209,7 +209,7 @@ BOOST_AUTO_TEST_CASE(test_invalid_uri) std::exception ); } -BOOST_AUTO_TEST_CASE(test_corner_cases) +BOOST_AUTO_TEST_CASE(corner_cases) { servus::URI uri1( "path/foo:bar" ); BOOST_CHECK_EQUAL( uri1.getPath(), "path/foo:bar" ); @@ -228,7 +228,7 @@ BOOST_AUTO_TEST_CASE(test_corner_cases) BOOST_CHECK_EQUAL( uri6.getHost(), "*" ); } -BOOST_AUTO_TEST_CASE(test_print) +BOOST_AUTO_TEST_CASE(print) { servus::URI uri; BOOST_CHECK_EQUAL( std::to_string( uri ), "" ); @@ -269,7 +269,7 @@ BOOST_AUTO_TEST_CASE(test_print) "foo://user@localhost:1024/path?key=value#fragment" ); } -BOOST_AUTO_TEST_CASE(test_host_port_without_schema) +BOOST_AUTO_TEST_CASE(host_port_without_schema) { const servus::URI uri( "host:12345" ); BOOST_CHECK_EQUAL( uri.getHost(), "" ); @@ -288,3 +288,13 @@ BOOST_AUTO_TEST_CASE(test_host_port_without_schema) BOOST_CHECK_EQUAL( uri3.getHost(), "host" ); BOOST_CHECK_EQUAL( uri3.getPort(), 12345 ); } + +BOOST_AUTO_TEST_CASE(query_without_value) +{ + const servus::URI uri( "?foo=,bar=foo,blubb" ); + BOOST_CHECK( uri.findQuery( "foo" ) != uri.queryEnd( )); + BOOST_CHECK( uri.findQuery( "blubb" ) != uri.queryEnd( )); + BOOST_CHECK_EQUAL( uri.findQuery( "bar" )->second, "foo" ); + BOOST_CHECK( uri.findQuery( "foo" )->second.empty( )); + BOOST_CHECK( uri.findQuery( "blubb" )->second.empty( )); +}