utils_ut.cc 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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(Utils, GetEnv) {
  57. const std::string var1 = GetEnv("PATH");
  58. EXPECT_FALSE(var1.empty());
  59. const std::string var2 = GetEnv("NOT_EXISTS_PATH");
  60. EXPECT_TRUE(var2.empty());
  61. #ifndef _WIN32
  62. setenv("SDK", "ttt", 1);
  63. EXPECT_TRUE(GetEnv("SDK") == "ttt");
  64. #endif
  65. }
  66. }