summaryrefslogtreecommitdiffstats
path: root/library/cpp/http/io/compression_ut.cpp
blob: bde9de4c8854a888728160cfad146c1b52749b7e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "stream.h"
#include "compression.h" 

#include <library/cpp/testing/unittest/registar.h>
#include <library/cpp/testing/unittest/tests_data.h>

#include <util/stream/zlib.h>
#include <util/generic/hash_set.h>

Y_UNIT_TEST_SUITE(THttpCompressionTest) { 
    static const TString DATA = "I'm a teapot"; 

    Y_UNIT_TEST(TestGetBestCodecs) { 
        UNIT_ASSERT(TCompressionCodecFactory::Instance().GetBestCodecs().size() > 0); 
    }

    Y_UNIT_TEST(TestEncoder) { 
        TStringStream buffer; 

        {
            auto encoder = TCompressionCodecFactory::Instance().FindEncoder("gzip"); 
            UNIT_ASSERT(encoder); 

            auto encodedStream = (*encoder)(&buffer); 
            encodedStream->Write(DATA); 
        }

        TZLibDecompress decompressor(&buffer); 
        UNIT_ASSERT_EQUAL(decompressor.ReadAll(), DATA); 
    }

    Y_UNIT_TEST(TestDecoder) { 
        TStringStream buffer; 

        {
            TZLibCompress compressor(TZLibCompress::TParams(&buffer).SetType(ZLib::GZip)); 
            compressor.Write(DATA); 
        }

        auto decoder = TCompressionCodecFactory::Instance().FindDecoder("gzip"); 
        UNIT_ASSERT(decoder); 

        auto decodedStream = (*decoder)(&buffer); 
        UNIT_ASSERT_EQUAL(decodedStream->ReadAll(), DATA); 
    }

    Y_UNIT_TEST(TestChooseBestCompressionScheme) {
        THashSet<TString> accepted;

        auto checkAccepted = [&accepted](const TString& v) {
            return accepted.contains(v);
        };

        UNIT_ASSERT_VALUES_EQUAL("identity", NHttp::ChooseBestCompressionScheme(checkAccepted, {"gzip", "deflate"}));
        accepted.insert("deflate");
        UNIT_ASSERT_VALUES_EQUAL("deflate", NHttp::ChooseBestCompressionScheme(checkAccepted, {"gzip", "deflate"}));
        accepted.insert("*");
        UNIT_ASSERT_VALUES_EQUAL("gzip", NHttp::ChooseBestCompressionScheme(checkAccepted, {"gzip", "deflate"}));
    }
} // THttpCompressionTest suite