C and C++ web framework.
http://rapida.vilor.one/docs
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
2.2 KiB
80 lines
2.2 KiB
/* SPDX-License-Identifier: GPL-3.0-or-later */ |
|
/* Copyright 2022 Ivan Polyakov */ |
|
|
|
#include "tests.hxx" |
|
|
|
using namespace rpd; |
|
|
|
TEST_CASE("Query string parsing") |
|
{ |
|
rpd_keyval query; |
|
rpd_keyval_init(&query, 0); |
|
|
|
SECTION("Query initialization") |
|
{ |
|
rpd_keyval q; |
|
rpd_keyval_init(&q, 0); |
|
|
|
REQUIRE(q.capacity == 0); |
|
REQUIRE(q.size == 0); |
|
REQUIRE(q.items == NULL); |
|
} |
|
|
|
SECTION("param=val query should be parsed into key=param and val=val") |
|
{ |
|
REQUIRE(rpd_query_parse(&query, "param=val") == 0); |
|
REQUIRE(query.size == 1); |
|
REQUIRE(std::string(query.items[0].key) == "param"); |
|
REQUIRE(std::string(query.items[0].val) == "val"); |
|
} |
|
|
|
SECTION("Parameter with empty value should be NULL") |
|
{ |
|
REQUIRE(rpd_query_parse(&query, "param=") == 0); |
|
REQUIRE(query.size == 1); |
|
REQUIRE(std::string(query.items[0].key) == "param"); |
|
REQUIRE(query.items[0].val == NULL); |
|
} |
|
|
|
SECTION("Empty parameter with value should be ignored") |
|
{ |
|
REQUIRE(rpd_query_parse(&query, "=val") == 0); |
|
REQUIRE(query.size == 0); |
|
} |
|
|
|
SECTION("Complex test with all situations") |
|
{ |
|
REQUIRE(rpd_query_parse(&query, "param=val¶m1=&=val2") == 0); |
|
REQUIRE(query.size == 2); |
|
REQUIRE(std::string(query.items[0].key) == "param"); |
|
REQUIRE(std::string(query.items[0].val) == "val"); |
|
REQUIRE(std::string(query.items[1].key) == "param1"); |
|
REQUIRE(query.items[1].val == NULL); |
|
REQUIRE(std::string(rpd_query_str(&query)) == "param=val"); |
|
} |
|
|
|
SECTION("C++ query wrapper initialization") |
|
{ |
|
Query cxxquery; |
|
|
|
REQUIRE(cxxquery.capacity() == 0); |
|
REQUIRE(cxxquery.size() == 0); |
|
REQUIRE(cxxquery.items() == NULL); |
|
} |
|
|
|
SECTION("Complex test with Query C++ wrapper") |
|
{ |
|
Query cxxquery; |
|
const char *qstr = "param=val¶m1=&=val2"; |
|
|
|
REQUIRE(cxxquery.parse(qstr) == 0); |
|
REQUIRE(cxxquery.size() == 2); |
|
REQUIRE(std::string(cxxquery["param"]) == "val"); |
|
REQUIRE(cxxquery["param1"] == NULL); |
|
} |
|
|
|
rpd_keyval_cleanup(&query); |
|
if (query.items) { |
|
free(query.items); |
|
} |
|
}
|
|
|