aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/http/misc/httpreqdata_ut.cpp
blob: e7f16ef27c8b920b2dc887d2e0d04d3904adb427 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "httpreqdata.h"

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

Y_UNIT_TEST_SUITE(TRequestServerDataTest) {
    Y_UNIT_TEST(Headers) {
        TServerRequestData sd;

        sd.AddHeader("x-xx", "y-yy");
        sd.AddHeader("x-Xx", "y-yy");

        UNIT_ASSERT_VALUES_EQUAL(sd.HeadersCount(), 1);

        sd.AddHeader("x-XxX", "y-yyy");
        UNIT_ASSERT_VALUES_EQUAL(sd.HeadersCount(), 2);
        UNIT_ASSERT_VALUES_EQUAL(TStringBuf(sd.HeaderIn("X-XX")), TStringBuf("y-yy"));
        UNIT_ASSERT_VALUES_EQUAL(TStringBuf(sd.HeaderIn("X-XXX")), TStringBuf("y-yyy"));
    }

    Y_UNIT_TEST(ComplexHeaders) {
        TServerRequestData sd;
        sd.SetHost("zzz", 1);

        sd.AddHeader("x-Xx", "y-yy");
        UNIT_ASSERT_VALUES_EQUAL(sd.HeadersCount(), 1);
        UNIT_ASSERT_VALUES_EQUAL(TStringBuf(sd.HeaderIn("X-XX")), TStringBuf("y-yy"));

        sd.AddHeader("x-Xz", "y-yy");
        UNIT_ASSERT_VALUES_EQUAL(sd.HeadersCount(), 2);
        UNIT_ASSERT_VALUES_EQUAL(TStringBuf(sd.HeaderIn("X-Xz")), TStringBuf("y-yy"));

        UNIT_ASSERT_VALUES_EQUAL(sd.ServerName(), "zzz");
        UNIT_ASSERT_VALUES_EQUAL(sd.ServerPort(), "1");
        sd.AddHeader("Host", "1234");
        UNIT_ASSERT_VALUES_EQUAL(sd.HeadersCount(), 3);
        UNIT_ASSERT_VALUES_EQUAL(TStringBuf(sd.HeaderIn("Host")), TStringBuf("1234"));
        UNIT_ASSERT_VALUES_EQUAL(sd.ServerName(), "1234");
        sd.AddHeader("Host", "12345:678");
        UNIT_ASSERT_VALUES_EQUAL(sd.HeadersCount(), 3);
        UNIT_ASSERT_VALUES_EQUAL(TStringBuf(sd.HeaderIn("Host")), TStringBuf("12345:678"));
        UNIT_ASSERT_VALUES_EQUAL(sd.ServerName(), "12345");
        UNIT_ASSERT_VALUES_EQUAL(sd.ServerPort(), "678");
    }

    Y_UNIT_TEST(ParseScan) {
        TServerRequestData rd;

        // Parse parses url without host
        UNIT_ASSERT(!rd.Parse(" http://yandex.ru/yandsearch?&gta=fake&haha=da HTTP 1.1 OK"));

        // This should work
        UNIT_ASSERT(rd.Parse(" /yandsearch?&gta=fake&haha=da HTTP 1.1 OK"));

        UNIT_ASSERT_STRINGS_EQUAL(rd.QueryStringBuf(), "&gta=fake&haha=da");
        UNIT_ASSERT_STRINGS_EQUAL(rd.QueryStringBuf(), rd.OrigQueryStringBuf());

        rd.Scan();
        UNIT_ASSERT(rd.CgiParam.Has("gta", "fake"));
        UNIT_ASSERT(rd.CgiParam.Has("haha", "da"));
        UNIT_ASSERT(!rd.CgiParam.Has("no-param"));

        rd.Clear();
    }

    Y_UNIT_TEST(Ctor) {
        const TString qs("gta=fake&haha=da");
        TServerRequestData rd(qs.c_str());

        UNIT_ASSERT_STRINGS_EQUAL(rd.QueryStringBuf(), qs);
        UNIT_ASSERT_STRINGS_EQUAL(rd.OrigQueryStringBuf(), qs);

        UNIT_ASSERT(rd.CgiParam.Has("gta"));
        UNIT_ASSERT(rd.CgiParam.Has("haha"));
        UNIT_ASSERT(!rd.CgiParam.Has("no-param"));
    }

    Y_UNIT_TEST(HashCut) {
        const TString qs("&gta=fake&haha=da");
        const TString header = " /yandsearch?" + qs + "#&uberParam=yes&q=? HTTP 1.1 OK";

        TServerRequestData rd;
        rd.Parse(header.c_str());

        UNIT_ASSERT_STRINGS_EQUAL(rd.QueryStringBuf(), qs);
        UNIT_ASSERT_STRINGS_EQUAL(rd.OrigQueryStringBuf(), qs);

        rd.Scan();
        UNIT_ASSERT(rd.CgiParam.Has("gta"));
        UNIT_ASSERT(rd.CgiParam.Has("haha"));
        UNIT_ASSERT(!rd.CgiParam.Has("uberParam"));
    }

    Y_UNIT_TEST(MisplacedHashCut) {
        TServerRequestData rd;
        rd.Parse(" /y#ndsearch?&gta=fake&haha=da&uberParam=yes&q=? HTTP 1.1 OK");

        UNIT_ASSERT_STRINGS_EQUAL(rd.QueryStringBuf(), "");
        UNIT_ASSERT_STRINGS_EQUAL(rd.OrigQueryStringBuf(), "");

        rd.Scan();
        UNIT_ASSERT(rd.CgiParam.empty());
    }

    Y_UNIT_TEST(CornerCase) {
        TServerRequestData rd;
        rd.Parse(" /yandsearch?#");

        UNIT_ASSERT_STRINGS_EQUAL(rd.QueryStringBuf(), "");
        UNIT_ASSERT_STRINGS_EQUAL(rd.OrigQueryStringBuf(), "");

        rd.Scan();
        UNIT_ASSERT(rd.CgiParam.empty());
    }

    Y_UNIT_TEST(AppendQueryString) {
        const TString qs("gta=fake&haha=da");
        TServerRequestData rd(qs.c_str());

        UNIT_ASSERT(rd.CgiParam.Has("gta", "fake"));
        UNIT_ASSERT(rd.CgiParam.Has("haha", "da"));

        UNIT_ASSERT_STRINGS_EQUAL(rd.QueryStringBuf(), qs);
        UNIT_ASSERT_STRINGS_EQUAL(rd.QueryStringBuf(), rd.OrigQueryStringBuf());

        constexpr TStringBuf appendix = "gta=true&gta=new";
        rd.AppendQueryString(appendix.data(), appendix.size());

        UNIT_ASSERT_STRINGS_EQUAL(rd.QueryStringBuf(), qs + '&' + appendix);
        UNIT_ASSERT_STRINGS_EQUAL(rd.OrigQueryStringBuf(), qs);

        rd.Scan();

        UNIT_ASSERT(rd.CgiParam.Has("gta", "true"));
        UNIT_ASSERT(rd.CgiParam.Has("gta", "new"));
    }

    Y_UNIT_TEST(SetRemoteAddrSimple) {
        static const TString TEST = "abacaba.search.yandex.net";

        TServerRequestData rd;
        rd.SetRemoteAddr(TEST);
        UNIT_ASSERT_STRINGS_EQUAL(TEST, rd.RemoteAddr());
    }

    Y_UNIT_TEST(SetRemoteAddrRandom) {
        for (size_t size = 0; size < 2 * INET6_ADDRSTRLEN; ++size) {
            const TString test = NUnitTest::RandomString(size, size);
            TServerRequestData rd;
            rd.SetRemoteAddr(test);
            UNIT_ASSERT_STRINGS_EQUAL(test.substr(0, INET6_ADDRSTRLEN - 1), rd.RemoteAddr());
        }
    }

} // TRequestServerDataTest