aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/http/fetch/httpparser_ut.cpp
blob: 1ccc667bb40c8f2db90fe62220e6e39f767460a8 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include "httpparser.h"

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

#define ENUM_OUT(arg)  \
    case type ::arg: { \
        out << #arg;   \
        return;        \
    }

template <>
void Out<THttpParserBase::States>(IOutputStream& out, THttpParserBase::States st) {
    using type = THttpParserBase::States;
    switch (st) {
        ENUM_OUT(hp_error)
        ENUM_OUT(hp_eof)
        ENUM_OUT(hp_in_header)
        ENUM_OUT(hp_read_alive)
        ENUM_OUT(hp_read_closed)
        ENUM_OUT(hp_begin_chunk_header)
        ENUM_OUT(hp_chunk_header)
        ENUM_OUT(hp_read_chunk)
    }
}

namespace {
    class TSomethingLikeFakeCheck;

    using TTestHttpParser = THttpParser<TSomethingLikeFakeCheck>;

    class TSomethingLikeFakeCheck {
        TString Body_;

    public:
        const TString& Body() const {
            return Body_;
        }

        // other functions are not really called by THttpParser
        void CheckDocPart(const void* buf, size_t len, THttpHeader* /* header */) {
            TString s(static_cast<const char*>(buf), len);
            Cout << "State = " << static_cast<TTestHttpParser*>(this)->GetState() << ", CheckDocPart(" << s.Quote() << ")\n";
            Body_ += s;
        }
    };

}

Y_UNIT_TEST_SUITE(TestHttpParser) {
    Y_UNIT_TEST(TestTrivialRequest) {
        const TString blob{ 
            "GET /search?q=hi HTTP/1.1\r\n"
            "Host:  www.google.ru:8080 \r\n"
            "\r\n"};
        THttpHeader hdr;
        THttpParser<> parser;
        parser.Init(&hdr);
        parser.Parse((void*)blob.data(), blob.size());
        UNIT_ASSERT_EQUAL(parser.GetState(), parser.hp_error); // can't parse request as response
    }

    // XXX: `entity_size` is i32 and `content_length` is i64!
    Y_UNIT_TEST(TestTrivialResponse) {
        const TString blob{ 
            "HTTP/1.1 200 Ok\r\n"
            "Content-Length: 2\r\n"
            "\r\n"
            "OK"};
        THttpHeader hdr;
        TTestHttpParser parser;
        parser.Init(&hdr);
        parser.Parse((void*)blob.data(), blob.size());
        UNIT_ASSERT_EQUAL(parser.GetState(), parser.hp_eof);
        UNIT_ASSERT_EQUAL(parser.Body(), "OK");
        UNIT_ASSERT_EQUAL(hdr.header_size, strlen(
                                               "HTTP/1.1 200 Ok\r\n"
                                               "Content-Length: 2\r\n"
                                               "\r\n"));
        UNIT_ASSERT_EQUAL(hdr.entity_size, strlen("OK"));
    }

    // XXX: `entity_size` is off by one in TE:chunked case.
    Y_UNIT_TEST(TestChunkedResponse) {
        const TString blob{ 
            "HTTP/1.1 200 OK\r\n"
            "Transfer-Encoding: chunked\r\n"
            "\r\n"
            "2\r\n"
            "Ok\r\n"
            "8\r\n"
            "AllRight\r\n"
            "0\r\n"
            "\r\n"};
        THttpHeader hdr;
        TTestHttpParser parser;
        parser.Init(&hdr);
        parser.Parse((void*)blob.data(), blob.size());
        UNIT_ASSERT_EQUAL(parser.GetState(), parser.hp_eof);
        UNIT_ASSERT_EQUAL(parser.Body(), "OkAllRight");
        UNIT_ASSERT_EQUAL(hdr.header_size, strlen(
                                               "HTTP/1.1 200 OK\r\n"
                                               "Transfer-Encoding: chunked\r\n"
                                               "\r\n"));
        const int off_by_one_err = -1; // XXX: it really looks so
        UNIT_ASSERT_EQUAL(hdr.entity_size + off_by_one_err, strlen(
                                                                "2\r\n"
                                                                "Ok\r\n"
                                                                "8\r\n"
                                                                "AllRight\r\n"
                                                                "0\r\n"
                                                                "\r\n"));
    }

    static const TString PipelineClenBlob_{ 
        "HTTP/1.1 200 Ok\r\n"
        "Content-Length: 4\r\n"
        "\r\n"
        "OK\r\n"
        "HTTP/1.1 200 Zz\r\n"
        "Content-Length: 4\r\n"
        "\r\n"
        "ZZ\r\n"};

    void AssertPipelineClen(TTestHttpParser & parser, const THttpHeader& hdr) {
        UNIT_ASSERT_EQUAL(parser.GetState(), parser.hp_eof);
        UNIT_ASSERT_EQUAL(4, hdr.content_length);
        UNIT_ASSERT_EQUAL(hdr.header_size, strlen(
                                               "HTTP/1.1 200 Ok\r\n"
                                               "Content-Length: 4\r\n"
                                               "\r\n"));
    }

    Y_UNIT_TEST(TestPipelineClenByteByByte) {
        const TString& blob = PipelineClenBlob_; 
        THttpHeader hdr;
        TTestHttpParser parser;
        parser.Init(&hdr);
        for (size_t i = 0; i < blob.size(); ++i) {
            const TStringBuf d{blob, i, 1};
            parser.Parse((void*)d.data(), d.size());
            Cout << TString(d).Quote() << " -> " << parser.GetState() << Endl; 
        }
        AssertPipelineClen(parser, hdr);
        UNIT_ASSERT_EQUAL(parser.Body(), "OK\r\n");
        UNIT_ASSERT_EQUAL(hdr.entity_size, hdr.content_length);
    }

    // XXX: Content-Length is ignored, Body() looks unexpected!
    Y_UNIT_TEST(TestPipelineClenOneChunk) {
        const TString& blob = PipelineClenBlob_; 
        THttpHeader hdr;
        TTestHttpParser parser;
        parser.Init(&hdr);
        parser.Parse((void*)blob.data(), blob.size());
        AssertPipelineClen(parser, hdr);
        UNIT_ASSERT_EQUAL(parser.Body(),
                          "OK\r\n"
                          "HTTP/1.1 200 Zz\r\n"
                          "Content-Length: 4\r\n"
                          "\r\n"
                          "ZZ\r\n");
        UNIT_ASSERT_EQUAL(hdr.entity_size, strlen(
                                               "OK\r\n"
                                               "HTTP/1.1 200 Zz\r\n"
                                               "Content-Length: 4\r\n"
                                               "\r\n"
                                               "ZZ\r\n"));
    }

    static const TString PipelineChunkedBlob_{ 
        "HTTP/1.1 200 OK\r\n"
        "Transfer-Encoding: chunked\r\n"
        "\r\n"
        "2\r\n"
        "Ok\r\n"
        "8\r\n"
        "AllRight\r\n"
        "0\r\n"
        "\r\n"
        "HTTP/1.1 200 OK\r\n"
        "Transfer-Encoding: chunked\r\n"
        "\r\n"
        "2\r\n"
        "Yo\r\n"
        "8\r\n"
        "uWin!Iam\r\n"
        "0\r\n"
        "\r\n"};

    void AssertPipelineChunked(TTestHttpParser & parser, const THttpHeader& hdr) {
        UNIT_ASSERT_EQUAL(parser.GetState(), parser.hp_eof);
        UNIT_ASSERT_EQUAL(parser.Body(), "OkAllRight");
        UNIT_ASSERT_EQUAL(-1, hdr.content_length);
        UNIT_ASSERT_EQUAL(hdr.header_size, strlen(
                                               "HTTP/1.1 200 OK\r\n"
                                               "Transfer-Encoding: chunked\r\n"
                                               "\r\n"));
        const int off_by_one_err = -1;
        UNIT_ASSERT_EQUAL(hdr.entity_size + off_by_one_err, strlen(
                                                                "2\r\n"
                                                                "Ok\r\n"
                                                                "8\r\n"
                                                                "AllRight\r\n"
                                                                "0\r\n"
                                                                "\r\n"));
    }

    Y_UNIT_TEST(TestPipelineChunkedByteByByte) {
        const TString& blob = PipelineChunkedBlob_; 
        THttpHeader hdr;
        TTestHttpParser parser;
        parser.Init(&hdr);
        for (size_t i = 0; i < blob.size(); ++i) {
            const TStringBuf d{blob, i, 1};
            parser.Parse((void*)d.data(), d.size());
            Cout << TString(d).Quote() << " -> " << parser.GetState() << Endl; 
            if (blob.size() / 2 - 1 <= i) // last \n sets EOF
                UNIT_ASSERT_EQUAL(parser.GetState(), parser.hp_eof);
        }
        AssertPipelineChunked(parser, hdr);
    }

    Y_UNIT_TEST(TestPipelineChunkedOneChunk) {
        const TString& blob = PipelineChunkedBlob_; 
        THttpHeader hdr;
        TTestHttpParser parser;
        parser.Init(&hdr);
        parser.Parse((void*)blob.data(), blob.size());
        AssertPipelineChunked(parser, hdr);
    }
}