changeset 707:203157cb4fde

unit tests of httpclient
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 13 Feb 2014 14:37:35 +0100
parents 2d829ea1db96
children 38e2883e096f
files CMakeLists.txt Core/HttpClient.cpp Core/HttpClient.h Core/Toolbox.cpp Core/Toolbox.h UnitTestsSources/RestApi.cpp UnitTestsSources/ServerIndex.cpp UnitTestsSources/main.cpp
diffstat 8 files changed, 89 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Thu Feb 13 12:46:39 2014 +0100
+++ b/CMakeLists.txt	Thu Feb 13 14:37:35 2014 +0100
@@ -17,6 +17,7 @@
 SET(BUILD_CLIENT_LIBRARY ON CACHE BOOL "Build the client library")
 SET(DCMTK_DICTIONARY_DIR "/usr/share/dcmtk" CACHE PATH "Directory containing the DCMTK dictionaries \"dicom.dic\" and \"private.dic\" (only when using system version of DCMTK)") 
 SET(ALLOW_DOWNLOADS OFF CACHE BOOL "Allow CMake to download packages")
+SET(UNIT_TESTS_WITH_HTTP_CONNEXIONS ON CACHE BOOL "Allow unit tests to make HTTP requests")
 
 # Advanced parameters to fine-tune linking against system libraries
 SET(USE_SYSTEM_JSONCPP ON CACHE BOOL "Use the system version of JsonCpp")
@@ -241,6 +242,12 @@
 ## Build the unit tests
 #####################################################################
 
+if (UNIT_TESTS_WITH_HTTP_CONNEXIONS)
+  add_definitions(-DUNIT_TESTS_WITH_HTTP_CONNEXIONS=1)
+else()
+  add_definitions(-DUNIT_TESTS_WITH_HTTP_CONNEXIONS=0)
+endif()
+
 add_definitions(-DORTHANC_BUILD_UNIT_TESTS=1)
 include(${CMAKE_SOURCE_DIR}/Resources/CMake/GoogleTestConfiguration.cmake)
 add_executable(UnitTests
--- a/Core/HttpClient.cpp	Thu Feb 13 12:46:39 2014 +0100
+++ b/Core/HttpClient.cpp	Thu Feb 13 14:37:35 2014 +0100
@@ -254,9 +254,4 @@
   {
     curl_global_cleanup();
   }
-
-  const char* HttpClient::GetLastStatusText() const
-  {
-    return EnumerationToString(lastStatus_);
-  }
 }
--- a/Core/HttpClient.h	Thu Feb 13 12:46:39 2014 +0100
+++ b/Core/HttpClient.h	Thu Feb 13 14:37:35 2014 +0100
@@ -115,8 +115,6 @@
       return lastStatus_;
     }
 
-    const char* GetLastStatusText() const;
-
     void SetCredentials(const char* username,
                         const char* password);
 
--- a/Core/Toolbox.cpp	Thu Feb 13 12:46:39 2014 +0100
+++ b/Core/Toolbox.cpp	Thu Feb 13 14:37:35 2014 +0100
@@ -157,17 +157,6 @@
   }
 #endif
 
-  void Toolbox::Sleep(uint32_t seconds)
-  {
-#if defined(_WIN32)
-    ::Sleep(static_cast<DWORD>(seconds) * static_cast<DWORD>(1000));
-#elif defined(__linux)
-    usleep(static_cast<uint64_t>(seconds) * static_cast<uint64_t>(1000000));
-#else
-#error Support your platform here
-#endif
-  }
-
   void Toolbox::USleep(uint64_t microSeconds)
   {
 #if defined(_WIN32)
--- a/Core/Toolbox.h	Thu Feb 13 12:46:39 2014 +0100
+++ b/Core/Toolbox.h	Thu Feb 13 14:37:35 2014 +0100
@@ -66,8 +66,6 @@
     void WriteFile(const std::string& content,
                    const std::string& path);
 
-    void Sleep(uint32_t seconds);
-
     void USleep(uint64_t microSeconds);
 
     void RemoveFile(const std::string& path);
--- a/UnitTestsSources/RestApi.cpp	Thu Feb 13 12:46:39 2014 +0100
+++ b/UnitTestsSources/RestApi.cpp	Thu Feb 13 14:37:35 2014 +0100
@@ -3,6 +3,8 @@
 #include <ctype.h>
 #include <glog/logging.h>
 
+#include "../Core/ChunkedBuffer.h"
+#include "../Core/HttpClient.h"
 #include "../Core/RestApi/RestApi.h"
 #include "../Core/Uuid.h"
 #include "../Core/OrthancException.h"
@@ -10,6 +12,51 @@
 
 using namespace Orthanc;
 
+#if !defined(UNIT_TESTS_WITH_HTTP_CONNEXIONS)
+#error "Please set UNIT_TESTS_WITH_HTTP_CONNEXIONS"
+#endif
+
+TEST(HttpClient, Basic)
+{
+  HttpClient c;
+  ASSERT_FALSE(c.IsVerbose());
+  c.SetVerbose(true);
+  ASSERT_TRUE(c.IsVerbose());
+  c.SetVerbose(false);
+  ASSERT_FALSE(c.IsVerbose());
+
+#if UNIT_TESTS_WITH_HTTP_CONNEXIONS == 1
+  Json::Value v;
+  c.SetUrl("http://orthanc.googlecode.com/hg/Resources/Configuration.json");
+  c.Apply(v);
+  ASSERT_TRUE(v.isMember("StorageDirectory"));
+  //ASSERT_EQ(GetLastStatusText());
+
+  v = Json::nullValue;
+
+  HttpClient cc(c);
+  cc.SetUrl("https://orthanc.googlecode.com/hg/Resources/Configuration.json");
+  cc.Apply(v);
+  ASSERT_TRUE(v.isMember("LuaScripts"));
+#endif
+}
+
+TEST(RestApi, ChunkedBuffer)
+{
+  ChunkedBuffer b;
+  ASSERT_EQ(0, b.GetNumBytes());
+
+  b.AddChunk("hello", 5);
+  ASSERT_EQ(5, b.GetNumBytes());
+
+  b.AddChunk("world", 5);
+  ASSERT_EQ(10, b.GetNumBytes());
+
+  std::string s;
+  b.Flatten(s);
+  ASSERT_EQ("helloworld", s);
+}
+
 TEST(RestApi, ParseCookies)
 {
   HttpHandler::Arguments headers;
--- a/UnitTestsSources/ServerIndex.cpp	Thu Feb 13 12:46:39 2014 +0100
+++ b/UnitTestsSources/ServerIndex.cpp	Thu Feb 13 14:37:35 2014 +0100
@@ -517,9 +517,9 @@
 {
   const std::string path = "OrthancStorageUnitTests";
   Toolbox::RemoveFile(path + "/index");
-  ServerContext context(path, ":memory:");
+  ServerContext context(path, ":memory:");   // The SQLite DB is in memory
   ServerIndex& index = context.GetIndex();
-  
+
   index.SetMaximumStorageSize(10);
 
   Json::Value tmp;
@@ -534,4 +534,7 @@
   instance.SetValue(DICOM_TAG_SERIES_INSTANCE_UID, "series1");
   instance.SetValue(DICOM_TAG_SOP_INSTANCE_UID, "instance1");
   ASSERT_EQ(StoreStatus_Success, index.Store(instance, attachments, ""));
+
+  // Because the DB is in memory, the SQLite index must not have been created
+  ASSERT_THROW(Toolbox::GetFileSize(path + "/index"), OrthancException);  
 }
--- a/UnitTestsSources/main.cpp	Thu Feb 13 12:46:39 2014 +0100
+++ b/UnitTestsSources/main.cpp	Thu Feb 13 14:37:35 2014 +0100
@@ -31,6 +31,7 @@
   ASSERT_FALSE(Toolbox::IsUuid(""));
   ASSERT_FALSE(Toolbox::IsUuid("012345678901234567890123456789012345"));
   ASSERT_TRUE(Toolbox::IsUuid("550e8400-e29b-41d4-a716-446655440000"));
+  ASSERT_FALSE(Toolbox::IsUuid("01234567890123456789012345678901234_"));
   ASSERT_FALSE(Toolbox::StartsWithUuid("550e8400-e29b-41d4-a716-44665544000"));
   ASSERT_TRUE(Toolbox::StartsWithUuid("550e8400-e29b-41d4-a716-446655440000"));
   ASSERT_TRUE(Toolbox::StartsWithUuid("550e8400-e29b-41d4-a716-446655440000 ok"));
@@ -48,6 +49,8 @@
   Toolbox::ComputeSHA1(s, "The quick brown fox jumps over the lazy dog");
   ASSERT_TRUE(Toolbox::IsSHA1(s));
   ASSERT_EQ("2fd4e1c6-7a2d28fc-ed849ee1-bb76e739-1b93eb12", s);
+
+  ASSERT_FALSE(Toolbox::IsSHA1("b5ed549f-956400ce-69a8c063-bf5b78be-2732a4b_"));
 }
 
 TEST(Zlib, Basic)
@@ -170,6 +173,10 @@
   ASSERT_THROW(Toolbox::SplitUriComponents(c, ""), OrthancException);
   ASSERT_THROW(Toolbox::SplitUriComponents(c, "a"), OrthancException);
   ASSERT_THROW(Toolbox::SplitUriComponents(c, "/coucou//coucou"), OrthancException);
+
+  c.clear();
+  c.push_back("test");
+  ASSERT_EQ("/", Toolbox::FlattenUri(c, 10));
 }
 
 
@@ -263,7 +270,10 @@
 {
   ASSERT_EQ("", Toolbox::EncodeBase64(""));
   ASSERT_EQ("YQ==", Toolbox::EncodeBase64("a"));
-  ASSERT_EQ("SGVsbG8gd29ybGQ=", Toolbox::EncodeBase64("Hello world"));
+
+  const std::string hello = "SGVsbG8gd29ybGQ=";
+  ASSERT_EQ(hello, Toolbox::EncodeBase64("Hello world"));
+  ASSERT_EQ("Hello world", Toolbox::DecodeBase64(hello));
 }
 
 TEST(Toolbox, PathToExecutable)
@@ -280,6 +290,25 @@
   ASSERT_EQ("c", Toolbox::StripSpaces("    \n\t c\r    \n  "));
 }
 
+TEST(Toolbox, Case)
+{
+  std::string s = "CoU";
+  std::string ss;
+
+  Toolbox::ToUpperCase(ss, s);
+  ASSERT_EQ("COU", ss);
+  Toolbox::ToLowerCase(ss, s);
+  ASSERT_EQ("cou", ss); 
+
+  s = "CoU";
+  Toolbox::ToUpperCase(s);
+  ASSERT_EQ("COU", s);
+
+  s = "CoU";
+  Toolbox::ToLowerCase(s);
+  ASSERT_EQ("cou", s);
+}
+
 
 #include <glog/logging.h>