format_ut.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "format.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <util/generic/string.h>
  4. #include <util/string/builder.h>
  5. #include <array>
  6. using namespace NMonitoring;
  7. Y_UNIT_TEST_SUITE(TFormatTest) {
  8. Y_UNIT_TEST(ContentTypeHeader) {
  9. UNIT_ASSERT_EQUAL(FormatFromContentType(""), EFormat::UNKNOWN);
  10. UNIT_ASSERT_EQUAL(FormatFromContentType("application/json;some=stuff"), EFormat::JSON);
  11. UNIT_ASSERT_EQUAL(FormatFromContentType("application/x-solomon-spack"), EFormat::SPACK);
  12. UNIT_ASSERT_EQUAL(FormatFromContentType("application/xml"), EFormat::UNKNOWN);
  13. UNIT_ASSERT_EQUAL(FormatFromContentType(";application/xml"), EFormat::UNKNOWN);
  14. }
  15. Y_UNIT_TEST(AcceptHeader) {
  16. UNIT_ASSERT_EQUAL(FormatFromAcceptHeader(""), EFormat::UNKNOWN);
  17. UNIT_ASSERT_EQUAL(FormatFromAcceptHeader("*/*"), EFormat::UNKNOWN);
  18. UNIT_ASSERT_EQUAL(
  19. FormatFromAcceptHeader("application/xml"),
  20. EFormat::UNKNOWN);
  21. UNIT_ASSERT_EQUAL(
  22. FormatFromAcceptHeader("application/json"),
  23. EFormat::JSON);
  24. UNIT_ASSERT_EQUAL(
  25. FormatFromAcceptHeader("application/x-solomon-spack"),
  26. EFormat::SPACK);
  27. UNIT_ASSERT_EQUAL(
  28. FormatFromAcceptHeader("application/x-solomon-pb"),
  29. EFormat::PROTOBUF);
  30. UNIT_ASSERT_EQUAL(
  31. FormatFromAcceptHeader("application/x-solomon-txt"),
  32. EFormat::TEXT);
  33. UNIT_ASSERT_EQUAL(
  34. FormatFromAcceptHeader("application/json, text/plain"),
  35. EFormat::JSON);
  36. UNIT_ASSERT_EQUAL(
  37. FormatFromAcceptHeader("application/x-solomon-spack, application/json, text/plain"),
  38. EFormat::SPACK);
  39. UNIT_ASSERT_EQUAL(
  40. FormatFromAcceptHeader(" , application/x-solomon-spack ,, application/json , text/plain"),
  41. EFormat::SPACK);
  42. UNIT_ASSERT_EQUAL(
  43. FormatFromAcceptHeader("application/xml, application/x-solomon-spack, text/plain"),
  44. EFormat::SPACK);
  45. UNIT_ASSERT_EQUAL(
  46. FormatFromAcceptHeader("text/plain"),
  47. EFormat::PROMETHEUS);
  48. UNIT_ASSERT_EQUAL(
  49. FormatFromAcceptHeader("application/openmetrics-text;version=0.0.1;q=0.75,text/plain;version=0.0.4"),
  50. EFormat::PROMETHEUS);
  51. }
  52. Y_UNIT_TEST(FormatToStrFromStr) {
  53. const std::array<EFormat, 6> formats = {{
  54. EFormat::UNKNOWN,
  55. EFormat::SPACK,
  56. EFormat::JSON,
  57. EFormat::PROTOBUF,
  58. EFormat::TEXT,
  59. EFormat::PROMETHEUS,
  60. }};
  61. for (EFormat f : formats) {
  62. TString str = (TStringBuilder() << f);
  63. EFormat g = FromString<EFormat>(str);
  64. UNIT_ASSERT_EQUAL(f, g);
  65. }
  66. }
  67. Y_UNIT_TEST(AcceptEncodingHeader) {
  68. UNIT_ASSERT_EQUAL(
  69. CompressionFromAcceptEncodingHeader(""),
  70. ECompression::UNKNOWN);
  71. UNIT_ASSERT_EQUAL(
  72. CompressionFromAcceptEncodingHeader("br"),
  73. ECompression::UNKNOWN);
  74. UNIT_ASSERT_EQUAL(
  75. CompressionFromAcceptEncodingHeader("identity"),
  76. ECompression::IDENTITY);
  77. UNIT_ASSERT_EQUAL(
  78. CompressionFromAcceptEncodingHeader("zlib"),
  79. ECompression::ZLIB);
  80. UNIT_ASSERT_EQUAL(
  81. CompressionFromAcceptEncodingHeader("lz4"),
  82. ECompression::LZ4);
  83. UNIT_ASSERT_EQUAL(
  84. CompressionFromAcceptEncodingHeader("zstd"),
  85. ECompression::ZSTD);
  86. UNIT_ASSERT_EQUAL(
  87. CompressionFromAcceptEncodingHeader("zstd, zlib"),
  88. ECompression::ZSTD);
  89. UNIT_ASSERT_EQUAL(
  90. CompressionFromAcceptEncodingHeader(" ,, , zstd , zlib"),
  91. ECompression::ZSTD);
  92. UNIT_ASSERT_EQUAL(
  93. CompressionFromAcceptEncodingHeader("br, deflate,lz4, zlib"),
  94. ECompression::LZ4);
  95. }
  96. Y_UNIT_TEST(CompressionToStrFromStr) {
  97. const std::array<ECompression, 5> algs = {{
  98. ECompression::UNKNOWN,
  99. ECompression::IDENTITY,
  100. ECompression::ZLIB,
  101. ECompression::LZ4,
  102. ECompression::ZSTD,
  103. }};
  104. for (ECompression a : algs) {
  105. TString str = (TStringBuilder() << a);
  106. ECompression b = FromString<ECompression>(str);
  107. UNIT_ASSERT_EQUAL(a, b);
  108. }
  109. }
  110. }