utils_ut.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include "gtest/gtest.h"
  2. #include "../../core/src/Utils.h"
  3. using namespace std;
  4. using namespace AlibabaCloud;
  5. namespace {
  6. TEST(UtilsTest, ComputeContentMD5) {
  7. const std::string src = "abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1";
  8. const std::string md5 = ComputeContentMD5(src.c_str(), src.size());
  9. EXPECT_TRUE(md5 == "WL7+fwrxHSlBTsfU9WCSbA==");
  10. }
  11. TEST(UtilsTest, GenerateUuid) {
  12. const std::string uuid1 = GenerateUuid();
  13. const std::string uuid2 = GenerateUuid();
  14. EXPECT_TRUE(uuid1 != uuid2);
  15. }
  16. TEST(UtilsTest, HttpMethodToString){
  17. std::string method = HttpMethodToString(HttpRequest::Method::Head);
  18. EXPECT_TRUE(method == "HEAD");
  19. method = HttpMethodToString(HttpRequest::Method::Post);
  20. EXPECT_TRUE(method == "POST");
  21. method = HttpMethodToString(HttpRequest::Method::Put);
  22. EXPECT_TRUE(method == "PUT");
  23. method = HttpMethodToString(HttpRequest::Method::Delete);
  24. EXPECT_TRUE(method == "DELETE");
  25. method = HttpMethodToString(HttpRequest::Method::Connect);
  26. EXPECT_TRUE(method == "CONNECT");
  27. method = HttpMethodToString(HttpRequest::Method::Options);
  28. EXPECT_TRUE(method == "OPTIONS");
  29. method = HttpMethodToString(HttpRequest::Method::Patch);
  30. EXPECT_TRUE(method == "PATCH");
  31. method = HttpMethodToString(HttpRequest::Method::Trace);
  32. EXPECT_TRUE(method == "TRACE");
  33. method = HttpMethodToString(HttpRequest::Method::Get);
  34. EXPECT_TRUE(method == "GET");
  35. }
  36. TEST(UtilsTest, StringReplace) {
  37. std::string src = "abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1";
  38. StringReplace(src, "abc", "http");
  39. EXPECT_TRUE(src == "http://username:password@example.com:123/path/data?key=value&key2=value2#fragid1");
  40. StringReplace(src, "value", "VALUE");
  41. EXPECT_TRUE(src == "http://username:password@example.com:123/path/data?key=VALUE&key2=VALUE2#fragid1");
  42. }
  43. TEST(UtilsTest, Encode) {
  44. const std::string url = "http://username:password@example.com:123/path/data?key=value&key2=value2#fragid1";
  45. const std::string encoded = "http%3A%2F%2Fusername%3Apassword%40example.com%3A123%2Fpath%2Fdata%3Fkey%3Dvalue%26key2%3Dvalue2%23fragid1";
  46. const std::string enc = UrlEncode(url);
  47. EXPECT_TRUE(enc == encoded);
  48. }
  49. TEST(UtilsTest, Decode) {
  50. const std::string url = "http://username:password@example.com:123/path/data?key=value&key2=value2#fragid1";
  51. const std::string encoded = "http%3A%2F%2Fusername%3Apassword%40example.com%3A123%2Fpath%2Fdata%3Fkey%3Dvalue%26key2%3Dvalue2%23fragid1";
  52. const std::string decoded = UrlDecode(encoded);
  53. EXPECT_TRUE(decoded == url);
  54. }
  55. }