[gnote] more uri handling. add unit tests.



commit cbdf079e6bfe85a790fa3628a1776b09134c59c5
Author: Hubert Figuiere <hub figuiere net>
Date:   Thu May 14 13:33:11 2009 -0400

    more uri handling. add unit tests.
---
 src/Makefile.am         |    8 +++++-
 src/sharp/uri.cpp       |   32 ++++++++++++++++++++++++++--
 src/sharp/uri.hpp       |    2 +
 src/test/stringtest.cpp |   19 ++++++++++++++++-
 src/test/uritest.cpp    |   52 +++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 107 insertions(+), 6 deletions(-)

diff --git a/src/Makefile.am b/src/Makefile.am
index 4096e48..9b288fa 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -20,8 +20,8 @@ GNOTE_LIBS = libgnote.a @LIBGLIBMM_LIBS@ @LIBGTKMM_LIBS@ @LIBXMLPP_LIBS@ \
 
 noinst_LIBRARIES = libgnote.a
 bin_PROGRAMS = gnote
-check_PROGRAMS = trietest stringtest notetest dttest
-TESTS = trietest stringtest notetest dttest
+check_PROGRAMS = trietest stringtest notetest dttest uritest
+TESTS = trietest stringtest notetest dttest uritest
 
 
 trietest_SOURCES = test/trietest.cpp \
@@ -36,6 +36,10 @@ stringtest_SOURCES = test/stringtest.cpp \
 	sharp/string.cpp debug.cpp
 stringtest_LDADD =  @BOOST_REGEX_LIBS@ @LIBGLIBMM_LIBS@
 
+uritest_SOURCES = test/uritest.cpp \
+	sharp/string.cpp  sharp/uri.cpp debug.cpp
+uritest_LDADD =  @BOOST_REGEX_LIBS@ @LIBGLIBMM_LIBS@
+
 notetest_SOURCES = test/notetest.cpp
 notetest_LDADD =  $(GNOTE_LIBS)
 
diff --git a/src/sharp/uri.cpp b/src/sharp/uri.cpp
index 07221a8..e991184 100644
--- a/src/sharp/uri.cpp
+++ b/src/sharp/uri.cpp
@@ -28,7 +28,11 @@
 #include "sharp/string.hpp"
 #include "sharp/uri.hpp"
 
-#define FILE_URI_SCHEME "file://"
+#define FILE_URI_SCHEME   "file:"
+#define MAILTO_URI_SCHEME "mailto:";
+#define HTTP_URI_SCHEME   "http:"
+#define HTTPS_URI_SCHEME  "https:"
+#define FTP_URI_SCHEME    "ftp:"
 
 namespace sharp {
 
@@ -38,18 +42,40 @@ namespace sharp {
   }
 
 
+  /// TODO this function does not behave as expected.
+  // it does not handle local_path for non file URI.
   std::string Uri::local_path() const
   {
     if(!is_file()) {
       return m_uri;
     }
-    return string_replace_first(m_uri, FILE_URI_SCHEME, "");
+    return string_replace_first(m_uri, std::string(FILE_URI_SCHEME) + "//", "");
+  }
+
+  bool Uri::_is_scheme(const std::string & scheme) const
+  {
+    return string_starts_with(m_uri, scheme);
   }
 
   std::string Uri::get_host() const
   {
     std::string host;
-    
+
+    if(!is_file()) {
+      if(_is_scheme(HTTP_URI_SCHEME) || _is_scheme(HTTPS_URI_SCHEME)
+         || _is_scheme(FTP_URI_SCHEME)) {
+        int idx = string_index_of(m_uri, "://");
+        if(idx != -1) {
+          std::string sub(m_uri.begin() + idx + 3, m_uri.end());
+          idx = string_index_of(sub, "/");
+          if(idx != -1) {
+            sub.erase(sub.begin() + idx, sub.end());
+            host = sub;
+          }
+        }
+      }
+    }
+
     return host;
   }
 
diff --git a/src/sharp/uri.hpp b/src/sharp/uri.hpp
index cee8d2b..392222d 100644
--- a/src/sharp/uri.hpp
+++ b/src/sharp/uri.hpp
@@ -49,6 +49,8 @@ namespace sharp {
     std::string get_host() const;
     static std::string escape_uri_string(const std::string &);
   private:
+    bool _is_scheme(const std::string & scheme) const;
+
     std::string m_uri;
   };
 
diff --git a/src/test/stringtest.cpp b/src/test/stringtest.cpp
index 5834e36..25f9656 100644
--- a/src/test/stringtest.cpp
+++ b/src/test/stringtest.cpp
@@ -1,4 +1,21 @@
-
+/*
+ * gnote
+ *
+ * Copyright (C) 2009 Hubert Figuiere
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
 
 #include <iostream>
 
diff --git a/src/test/uritest.cpp b/src/test/uritest.cpp
new file mode 100644
index 0000000..b580630
--- /dev/null
+++ b/src/test/uritest.cpp
@@ -0,0 +1,52 @@
+/*
+ * gnote
+ *
+ * Copyright (C) 2009 Hubert Figuiere
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+
+
+#include <boost/test/minimal.hpp>
+
+#include "sharp/uri.hpp"
+
+using namespace sharp;
+
+int test_main(int /*argc*/, char ** /*argv*/)
+{
+
+  Uri uri1("http://bugzilla.gnome.org/";);
+  
+  BOOST_CHECK(!uri1.is_file());
+  BOOST_CHECK(uri1.local_path() == "http://bugzilla.gnome.org/";);
+  BOOST_CHECK(uri1.get_host() == "bugzilla.gnome.org");
+
+  Uri uri2("https://bugzilla.gnome.org/";);
+  
+  BOOST_CHECK(!uri2.is_file());
+  BOOST_CHECK(uri2.local_path() == "https://bugzilla.gnome.org/";);
+  BOOST_CHECK(uri2.get_host() == "bugzilla.gnome.org");
+
+
+  Uri uri3("file:///tmp/foo.txt");
+  BOOST_CHECK(uri3.is_file());
+  BOOST_CHECK(uri3.local_path() == "/tmp/foo.txt");
+  BOOST_CHECK(uri3.get_host() == "");  
+
+  return 0;
+}
+



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]