compression_ut.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include "stream.h"
  2. #include "compression.h"
  3. #include <library/cpp/testing/unittest/registar.h>
  4. #include <library/cpp/testing/unittest/tests_data.h>
  5. #include <util/stream/zlib.h>
  6. #include <util/generic/hash_set.h>
  7. Y_UNIT_TEST_SUITE(THttpCompressionTest) {
  8. static const TString DATA = "I'm a teapot";
  9. Y_UNIT_TEST(TestGetBestCodecs) {
  10. UNIT_ASSERT(TCompressionCodecFactory::Instance().GetBestCodecs().size() > 0);
  11. }
  12. Y_UNIT_TEST(TestEncoder) {
  13. TStringStream buffer;
  14. {
  15. auto encoder = TCompressionCodecFactory::Instance().FindEncoder("gzip");
  16. UNIT_ASSERT(encoder);
  17. auto encodedStream = (*encoder)(&buffer);
  18. encodedStream->Write(DATA);
  19. }
  20. TZLibDecompress decompressor(&buffer);
  21. UNIT_ASSERT_EQUAL(decompressor.ReadAll(), DATA);
  22. }
  23. Y_UNIT_TEST(TestDecoder) {
  24. TStringStream buffer;
  25. {
  26. TZLibCompress compressor(TZLibCompress::TParams(&buffer).SetType(ZLib::GZip));
  27. compressor.Write(DATA);
  28. }
  29. auto decoder = TCompressionCodecFactory::Instance().FindDecoder("gzip");
  30. UNIT_ASSERT(decoder);
  31. auto decodedStream = (*decoder)(&buffer);
  32. UNIT_ASSERT_EQUAL(decodedStream->ReadAll(), DATA);
  33. }
  34. Y_UNIT_TEST(TestChooseBestCompressionScheme) {
  35. THashSet<TString> accepted;
  36. auto checkAccepted = [&accepted](const TString& v) {
  37. return accepted.contains(v);
  38. };
  39. UNIT_ASSERT_VALUES_EQUAL("identity", NHttp::ChooseBestCompressionScheme(checkAccepted, {"gzip", "deflate"}));
  40. accepted.insert("deflate");
  41. UNIT_ASSERT_VALUES_EQUAL("deflate", NHttp::ChooseBestCompressionScheme(checkAccepted, {"gzip", "deflate"}));
  42. accepted.insert("*");
  43. UNIT_ASSERT_VALUES_EQUAL("gzip", NHttp::ChooseBestCompressionScheme(checkAccepted, {"gzip", "deflate"}));
  44. }
  45. } // THttpCompressionTest suite