diff --git a/src/URL.cpp b/src/URL.cpp index 235681b..0642060 100644 --- a/src/URL.cpp +++ b/src/URL.cpp @@ -61,9 +61,12 @@ namespace fr pos = url.find('/', parse_offset); pos = (pos != std::string::npos) ? pos : url.find('?', parse_offset); pos = (pos != std::string::npos) ? pos : url.find('#', parse_offset); - pos = (pos != std::string::npos) ? pos : url.size(); - host = url.substr(parse_offset, pos - parse_offset); - parse_offset = pos + 1; + if(pos != std::string::npos || parse_offset != 0) + { + pos = (pos != std::string::npos) ? pos : url.size(); + host = url.substr(parse_offset, pos - parse_offset); + parse_offset = pos + 1; + } //Guess port based on scheme, if it's not explicitly provided. switch(scheme) diff --git a/tests/URLTest.cpp b/tests/URLTest.cpp index 0cf3cc9..173a109 100644 --- a/tests/URLTest.cpp +++ b/tests/URLTest.cpp @@ -88,4 +88,28 @@ TEST(URLTest, test_modify_url) fr::URL url("https://example.com:2020"); url.set_host("example.co.uk"); ASSERT_EQ(url.get_url(), "https://example.co.uk:2020"); +} + +TEST(URLTest, test_path_only_parse) +{ + { + fr::URL url("/bob"); + ASSERT_TRUE(url.get_host().empty()); + ASSERT_EQ(url.get_path(), "/bob"); + } + { + fr::URL url("bob"); + ASSERT_TRUE(url.get_host().empty()); + ASSERT_EQ(url.get_path(), "/bob"); + } + { + fr::URL url("http://bob"); + ASSERT_EQ(url.get_host(), "bob"); + ASSERT_TRUE(url.get_path().empty()); + } + { + fr::URL url("bob.com/trob"); + ASSERT_EQ(url.get_host(), "bob.com"); + ASSERT_EQ(url.get_path(), "/trob"); + } } \ No newline at end of file