diff options
Diffstat (limited to 'Swiften/Base')
| -rw-r--r-- | Swiften/Base/URL.cpp | 22 | ||||
| -rw-r--r-- | Swiften/Base/URL.h | 22 | ||||
| -rw-r--r-- | Swiften/Base/UnitTest/URLTest.cpp | 10 |
3 files changed, 33 insertions, 21 deletions
diff --git a/Swiften/Base/URL.cpp b/Swiften/Base/URL.cpp index c36863f..28fe6d3 100644 --- a/Swiften/Base/URL.cpp +++ b/Swiften/Base/URL.cpp @@ -86,8 +86,30 @@ URL URL::fromString(const std::string& urlString) { return URL(); } } +// FIXME: Escape non-ascii characters +std::string URL::toString() const { + if (empty) { + return ""; + } + std::string result = scheme + "://"; + if (!user.empty()) { + result += user; + if (!password.empty()) { + result += ":" + password; + } + result += "@"; + } + result += host; + if (port) { + result += ":"; + result += boost::lexical_cast<std::string>(*port); + } + result += path; + return result; +} + // Disabling this code for now, since GCC4.5+boost1.42 (on ubuntu) seems to // result in a bug. Replacing it with naive code. #if 0 // Should be in anonymous namespace, but older GCCs complain if we do that diff --git a/Swiften/Base/URL.h b/Swiften/Base/URL.h index 00d58f1..75cf1a6 100644 --- a/Swiften/Base/URL.h +++ b/Swiften/Base/URL.h @@ -59,29 +59,9 @@ class SWIFTEN_API URL { const std::string& getPath() const { return path; } - const std::string toString() const { - if (empty) { - return ""; - } - std::string result = scheme + "://"; - if (!user.empty()) { - result += user; - if (!password.empty()) { - result += ":" + password; - } - result += "@"; - } - result += host; - if (port > 0) { - result += ":"; - result += boost::lexical_cast<std::string>(port); - } - result += "/"; - result += path; - return result; - } + std::string toString() const; static int getPortOrDefaultPort(const URL& url); static URL fromString(const std::string&); static std::string unescape(const std::string&); diff --git a/Swiften/Base/UnitTest/URLTest.cpp b/Swiften/Base/UnitTest/URLTest.cpp index 4de1d33..55c81f6 100644 --- a/Swiften/Base/UnitTest/URLTest.cpp +++ b/Swiften/Base/UnitTest/URLTest.cpp @@ -20,8 +20,10 @@ class URLTest : public CppUnit::TestFixture { CPPUNIT_TEST(testFromString_WithPortWithoutPath); CPPUNIT_TEST(testFromString_WithUserInfo); CPPUNIT_TEST(testFromString_NonASCIIHost); CPPUNIT_TEST(testFromString_NonASCIIPath); + CPPUNIT_TEST(testToString); + CPPUNIT_TEST(testToString_WithPort); CPPUNIT_TEST_SUITE_END(); public: void testFromString() { @@ -78,7 +80,15 @@ class URLTest : public CppUnit::TestFixture { URL url = URL::fromString("http://foo.bar/baz/tron%C3%A7on/bam"); CPPUNIT_ASSERT_EQUAL(std::string("/baz/tron\xc3\xa7on/bam"), url.getPath()); } + + void testToString() { + CPPUNIT_ASSERT_EQUAL(std::string("http://foo.bar/baz/bam"), URL("http", "foo.bar", "/baz/bam").toString()); + } + + void testToString_WithPort() { + CPPUNIT_ASSERT_EQUAL(std::string("http://foo.bar:1234/baz/bam"), URL("http", "foo.bar", 1234, "/baz/bam").toString()); + } }; CPPUNIT_TEST_SUITE_REGISTRATION(URLTest); |
Swift