summaryrefslogtreecommitdiffstats
path: root/src/seastar/tests/unit/request_parser_test.cc
blob: e8d694561e85b042cdde7a29ad148dc2c3f9d435 (plain)
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
/*
 * This file is open source software, licensed to you under the terms
 * of the Apache License, Version 2.0 (the "License").  See the NOTICE file
 * distributed with this work for additional information regarding copyright
 * ownership.  You may not use this file except in compliance with the License.
 *
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/*
 * Copyright (C) 2020 ScyllaDB.
 */

#include <seastar/core/ragel.hh>
#include <seastar/core/sstring.hh>
#include <seastar/core/temporary_buffer.hh>
#include <seastar/http/request.hh>
#include <seastar/http/request_parser.hh>
#include <seastar/testing/test_case.hh>
#include <tuple>
#include <utility>
#include <vector>

using namespace seastar;

SEASTAR_TEST_CASE(test_header_parsing) {
    struct test_set {
        sstring msg;
        bool parsable;
        sstring header_name = "";
        sstring header_value = "";

        temporary_buffer<char> buf() {
            return temporary_buffer<char>(msg.c_str(), msg.size());
        }
    };

    std::vector<test_set> tests = {
        { "GET /test HTTP/1.1\r\nHost: test\r\n\r\n", true, "Host", "test" },
        { "GET /hello HTTP/1.0\r\nHeader: Field\r\n\r\n", true, "Header", "Field" },
        { "GET /hello HTTP/1.0\r\nHeader: \r\n\r\n", true, "Header", "" },
        { "GET /hello HTTP/1.0\r\nHeader:  f  i e l d  \r\n\r\n", true, "Header", "f  i e l d" },
        { "GET /hello HTTP/1.0\r\nHeader: fiel\r\n    d\r\n\r\n", true, "Header", "fiel d" },
        { "GET /hello HTTP/1.0\r\ntchars.^_`|123: printable!@#%^&*()obs_text\x80\x81\xff\r\n\r\n", true,
            "tchars.^_`|123", "printable!@#%^&*()obs_text\x80\x81\xff" },
        { "GET /hello HTTP/1.0\r\nHeader: Field\r\nHeader: Field2\r\n\r\n", true, "Header", "Field,Field2" },
        { "GET /hello HTTP/1.0\r\n\r\n", true },
        { "GET /hello HTTP/1.0\r\nHeader : Field\r\n\r\n", false },
        { "GET /hello HTTP/1.0\r\nHeader Field\r\n\r\n", false },
        { "GET /hello HTTP/1.0\r\nHeader@: Field\r\n\r\n", false },
        { "GET /hello HTTP/1.0\r\nHeader: fiel\r\nd \r\n\r\n", false }
    };

    http_request_parser parser;
    for (auto& tset : tests) {
        parser.init();
        BOOST_REQUIRE(parser(tset.buf()).get0().has_value());
        BOOST_REQUIRE_NE(parser.failed(), tset.parsable);
        if (tset.parsable) {
            auto req = parser.get_parsed_request();
            BOOST_REQUIRE_EQUAL(req->get_header(std::move(tset.header_name)), std::move(tset.header_value));
        }
    }
    return make_ready_future<>();
}