utils_ut.cc 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "gtest/gtest.h"
  2. #include "alibabacloud/core/Utils.h"
  3. #include <stdlib.h>
  4. using namespace std;
  5. using namespace AlibabaCloud;
  6. namespace {
  7. TEST(UtilsTest, ComputeContentMD5) {
  8. const std::string src = "abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1";
  9. const std::string md5 = ComputeContentMD5(src.c_str(), src.size());
  10. EXPECT_TRUE(md5 == "WL7+fwrxHSlBTsfU9WCSbA==");
  11. }
  12. TEST(UtilsTest, GenerateUuid) {
  13. const std::string uuid1 = GenerateUuid();
  14. const std::string uuid2 = GenerateUuid();
  15. EXPECT_TRUE(uuid1 != uuid2);
  16. }
  17. TEST(UtilsTest, HttpMethodToString){
  18. std::string method = HttpMethodToString(HttpRequest::Method::Head);
  19. EXPECT_TRUE(method == "HEAD");
  20. method = HttpMethodToString(HttpRequest::Method::Post);
  21. EXPECT_TRUE(method == "POST");
  22. method = HttpMethodToString(HttpRequest::Method::Put);
  23. EXPECT_TRUE(method == "PUT");
  24. method = HttpMethodToString(HttpRequest::Method::Delete);
  25. EXPECT_TRUE(method == "DELETE");
  26. method = HttpMethodToString(HttpRequest::Method::Connect);
  27. EXPECT_TRUE(method == "CONNECT");
  28. method = HttpMethodToString(HttpRequest::Method::Options);
  29. EXPECT_TRUE(method == "OPTIONS");
  30. method = HttpMethodToString(HttpRequest::Method::Patch);
  31. EXPECT_TRUE(method == "PATCH");
  32. method = HttpMethodToString(HttpRequest::Method::Trace);
  33. EXPECT_TRUE(method == "TRACE");
  34. method = HttpMethodToString(HttpRequest::Method::Get);
  35. EXPECT_TRUE(method == "GET");
  36. }
  37. TEST(UtilsTest, StringReplace) {
  38. std::string src = "abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1";
  39. StringReplace(src, "abc", "http");
  40. EXPECT_TRUE(src == "http://username:password@example.com:123/path/data?key=value&key2=value2#fragid1");
  41. StringReplace(src, "value", "VALUE");
  42. EXPECT_TRUE(src == "http://username:password@example.com:123/path/data?key=VALUE&key2=VALUE2#fragid1");
  43. }
  44. TEST(UtilsTest, Encode) {
  45. const std::string url = "http://username:password@example.com:123/path/data?key=value&key2=value2#fragid1";
  46. const std::string encoded = "http%3A%2F%2Fusername%3Apassword%40example.com%3A123%2Fpath%2Fdata%3Fkey%3Dvalue%26key2%3Dvalue2%23fragid1";
  47. const std::string enc = UrlEncode(url);
  48. EXPECT_TRUE(enc == encoded);
  49. }
  50. TEST(UtilsTest, Decode) {
  51. const std::string url = "http://username:password@example.com:123/path/data?key=value&key2=value2#fragid1";
  52. const std::string encoded = "http%3A%2F%2Fusername%3Apassword%40example.com%3A123%2Fpath%2Fdata%3Fkey%3Dvalue%26key2%3Dvalue2%23fragid1";
  53. const std::string decoded = UrlDecode(encoded);
  54. EXPECT_TRUE(decoded == url);
  55. }
  56. TEST(UtilsTest, MapToJson){
  57. std::map<std::string, std::string> maps;
  58. maps.insert(std::make_pair("foo", "bar"));
  59. std::string jsonStr = MapToJson(maps);
  60. EXPECT_EQ(jsonStr, "{\"foo\":\"bar\"}");
  61. }
  62. TEST(UtilsTest, JsonToMap){
  63. std::map<std::string, std::string> targetMaps;
  64. targetMaps.insert(std::make_pair("foo", "bar"));
  65. targetMaps.insert(std::make_pair("int", "1"));
  66. targetMaps.insert(std::make_pair("array","foo,bar"));
  67. std::string jsonStr = "{\"foo\":\"bar\",\"int\":1,\"array\":[\"foo\",\"bar\"]}";
  68. std::map<std::string, std::string> maps = JsonToMap(jsonStr);
  69. EXPECT_EQ(targetMaps, maps);
  70. }
  71. TEST(Utils, GetEnv) {
  72. const std::string var1 = GetEnv("PATH");
  73. EXPECT_FALSE(var1.empty());
  74. const std::string var2 = GetEnv("NOT_EXISTS_PATH");
  75. EXPECT_TRUE(var2.empty());
  76. #ifndef _WIN32
  77. setenv("SDK", "ttt", 1);
  78. EXPECT_TRUE(GetEnv("SDK") == "ttt");
  79. #endif
  80. }
  81. }