Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Copyright (c) 2024 Muhammad Nawaz | ||
3 | * Licensed under the MIT License. See LICENSE file for more information. | ||
4 | */ | ||
5 | // [ END OF LICENSE c6bd0f49d040fca8d8a9cb05868e66aa63f0e2e0 ] | ||
6 | |||
7 | #include "validators/param_validators.hpp" | ||
8 | #include <gtest/gtest.h> | ||
9 | |||
10 | enum : size_t | ||
11 | { | ||
12 | INPUT, | ||
13 | STYLE, | ||
14 | EXPLODE, | ||
15 | TYPE, | ||
16 | EXPECTED_ERROR | ||
17 | }; | ||
18 | |||
19 | class QueryPrimitiveParam | ||
20 | : public ::testing::TestWithParam<std::tuple<std::string, std::string, bool, std::string, ValidationError>> | ||
21 | { | ||
22 | protected: | ||
23 | 19 | void SetUp() override | |
24 | { | ||
25 |
2/4✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
|
19 | auto params = GetParam(); |
26 |
1/2✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
|
19 | input_ = "/pets/xyz?" + std::get<INPUT>(params); |
27 |
1/2✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
|
19 | std::string style = std::get<STYLE>(params); |
28 | 19 | bool explode = std::get<EXPLODE>(params); | |
29 |
1/2✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
|
19 | std::string type = std::get<TYPE>(params); |
30 | 19 | expected_error_ = std::get<EXPECTED_ERROR>(params); | |
31 | |||
32 | std::string json = R"({ | ||
33 | "name": "param", | ||
34 | "in": "query", | ||
35 | "required": true, | ||
36 |
2/4✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
|
38 | "style": ")" + style + |
37 | R"(", | ||
38 |
4/6✓ Branch 0 taken 9 times.
✓ Branch 1 taken 10 times.
✓ Branch 3 taken 19 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 19 times.
✗ Branch 7 not taken.
|
57 | "explode": )" + (explode ? "true" : "false") + |
39 | R"(, | ||
40 | "schema": { | ||
41 |
1/2✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
|
38 | "type": ")" + |
42 |
1/2✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
|
19 | type + R"(" |
43 | } | ||
44 | })"; | ||
45 |
1/2✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
|
19 | doc_.Parse(json.c_str()); |
46 | |||
47 |
1/2✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
|
19 | validator_ = std::make_unique<QueryParamValidator>(doc_, keys_); |
48 | 19 | } | |
49 | |||
50 | std::unique_ptr<QueryParamValidator> validator_; | ||
51 | std::string input_; | ||
52 | rapidjson::Document doc_; | ||
53 | std::vector<std::string> keys_{"paths", "/pets/xyz", "get", "parameters", "param"}; | ||
54 | std::string error_msg_; | ||
55 | ValidationError expected_error_; | ||
56 | }; | ||
57 | |||
58 |
6/12✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 9 taken 1 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 1 times.
✗ Branch 13 not taken.
✓ Branch 16 taken 1 times.
✗ Branch 17 not taken.
✓ Branch 20 taken 1 times.
✗ Branch 21 not taken.
✓ Branch 24 taken 1 times.
✗ Branch 25 not taken.
|
78 | TEST_P(QueryPrimitiveParam, ValidateParam) |
59 | { | ||
60 |
1/2✓ Branch 3 taken 19 times.
✗ Branch 4 not taken.
|
38 | const char* beg = input_.c_str() + std::string("/pets/xyz?").size(); |
61 | 38 | const char* end = input_.c_str() + input_.size(); | |
62 |
1/2✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
|
38 | auto result = validator_->ValidateParam(beg, end, error_msg_); |
63 |
2/10✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 19 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
|
38 | EXPECT_EQ(result, expected_error_); |
64 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 8 times.
|
38 | if (result != ValidationError::NONE) { |
65 |
1/10✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
|
22 | EXPECT_FALSE(error_msg_.empty()); |
66 |
1/2✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
|
44 | rapidjson::Document doc; |
67 |
1/2✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
|
22 | doc.Parse(error_msg_.c_str()); |
68 |
1/10✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
|
22 | EXPECT_FALSE(doc.HasParseError()); |
69 | } | ||
70 | 38 | } | |
71 | |||
72 |
22/64✓ Branch 1 taken 1 times.
✓ Branch 2 taken 19 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 1 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✓ Branch 16 taken 1 times.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✓ Branch 19 taken 1 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✓ Branch 22 taken 1 times.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✓ Branch 25 taken 1 times.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✓ Branch 28 taken 1 times.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✓ Branch 31 taken 1 times.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
✓ Branch 34 taken 1 times.
✗ Branch 35 not taken.
✗ Branch 36 not taken.
✓ Branch 37 taken 1 times.
✗ Branch 38 not taken.
✗ Branch 39 not taken.
✓ Branch 40 taken 1 times.
✗ Branch 41 not taken.
✗ Branch 42 not taken.
✓ Branch 43 taken 1 times.
✗ Branch 44 not taken.
✗ Branch 45 not taken.
✓ Branch 46 taken 1 times.
✗ Branch 47 not taken.
✗ Branch 48 not taken.
✓ Branch 49 taken 1 times.
✗ Branch 50 not taken.
✗ Branch 51 not taken.
✓ Branch 52 taken 1 times.
✗ Branch 53 not taken.
✗ Branch 54 not taken.
✓ Branch 55 taken 1 times.
✗ Branch 56 not taken.
✗ Branch 57 not taken.
✓ Branch 58 taken 1 times.
✗ Branch 59 not taken.
✗ Branch 60 not taken.
✓ Branch 61 taken 1 times.
✗ Branch 62 not taken.
✗ Branch 63 not taken.
✗ Branch 65 not taken.
✗ Branch 66 not taken.
|
42 | INSTANTIATE_TEST_SUITE_P( |
73 | QueryPrimitiveParamTests, QueryPrimitiveParam, | ||
74 | ::testing::Values(std::make_tuple("param=123", "form", false, "integer", ValidationError::NONE), | ||
75 | std::make_tuple("param=123", "form", true, "integer", ValidationError::NONE), | ||
76 | std::make_tuple("param=123.123", "form", false, "number", ValidationError::NONE), | ||
77 | std::make_tuple("param=123.123", "form", true, "number", ValidationError::NONE), | ||
78 | std::make_tuple("param=abc%20xyz", "form", false, "string", ValidationError::NONE), | ||
79 | std::make_tuple("param=abc", "form", true, "string", ValidationError::NONE), | ||
80 | std::make_tuple("param=true", "form", false, "boolean", ValidationError::NONE), | ||
81 | std::make_tuple("param=true", "form", true, "boolean", ValidationError::NONE), | ||
82 | std::make_tuple("param=string", "form", false, "integer", ValidationError::INVALID_QUERY_PARAM), | ||
83 | std::make_tuple("param=string", "form", true, "integer", ValidationError::INVALID_QUERY_PARAM), | ||
84 | std::make_tuple("param=123.123", "form", false, "integer", ValidationError::INVALID_QUERY_PARAM), | ||
85 | std::make_tuple("param=123.123", "form", true, "integer", ValidationError::INVALID_QUERY_PARAM), | ||
86 | std::make_tuple("param=true", "form", false, "number", ValidationError::INVALID_QUERY_PARAM), | ||
87 | std::make_tuple("param=true", "form", true, "number", ValidationError::INVALID_QUERY_PARAM), | ||
88 | std::make_tuple("param=123", "form", false, "boolean", ValidationError::INVALID_QUERY_PARAM), | ||
89 | std::make_tuple("param=123", "form", true, "boolean", ValidationError::INVALID_QUERY_PARAM), | ||
90 | std::make_tuple("param=123.123", "form", false, "boolean", ValidationError::INVALID_QUERY_PARAM), | ||
91 | std::make_tuple("param=abc%2yz", "form", true, "string", ValidationError::INVALID_QUERY_PARAM), | ||
92 | std::make_tuple("param=abc%2yz", "form", false, "string", ValidationError::INVALID_QUERY_PARAM))); | ||
93 | |||
94 | class QueryArrayParam | ||
95 | : public ::testing::TestWithParam<std::tuple<std::string, std::string, bool, std::string, ValidationError>> | ||
96 | { | ||
97 | protected: | ||
98 | 34 | void SetUp() override | |
99 | { | ||
100 |
2/4✓ Branch 1 taken 34 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 34 times.
✗ Branch 5 not taken.
|
34 | auto params = GetParam(); |
101 |
1/2✓ Branch 2 taken 34 times.
✗ Branch 3 not taken.
|
34 | input_ = "/pets/xyz?" + std::get<INPUT>(params); |
102 |
1/2✓ Branch 2 taken 34 times.
✗ Branch 3 not taken.
|
34 | std::string style = std::get<STYLE>(params); |
103 | 34 | bool explode = std::get<EXPLODE>(params); | |
104 |
1/2✓ Branch 2 taken 34 times.
✗ Branch 3 not taken.
|
34 | std::string type = std::get<TYPE>(params); |
105 | 34 | expected_error_ = std::get<EXPECTED_ERROR>(params); | |
106 | |||
107 | std::string json = R"({ | ||
108 | "name": "param", | ||
109 | "in": "query", | ||
110 | "required": true, | ||
111 |
2/4✓ Branch 1 taken 34 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 34 times.
✗ Branch 5 not taken.
|
68 | "style": ")" + style + |
112 | R"(", | ||
113 |
4/6✓ Branch 0 taken 16 times.
✓ Branch 1 taken 18 times.
✓ Branch 3 taken 34 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 34 times.
✗ Branch 7 not taken.
|
102 | "explode": )" + (explode ? "true" : "false") + |
114 | R"(, | ||
115 | "schema": { | ||
116 | "type": "array", | ||
117 | "items": { | ||
118 |
1/2✓ Branch 1 taken 34 times.
✗ Branch 2 not taken.
|
68 | "type": ")" + |
119 |
1/2✓ Branch 1 taken 34 times.
✗ Branch 2 not taken.
|
34 | type + R"(" |
120 | } | ||
121 | } | ||
122 | })"; | ||
123 |
1/2✓ Branch 2 taken 34 times.
✗ Branch 3 not taken.
|
34 | doc_.Parse(json.c_str()); |
124 | |||
125 |
1/2✓ Branch 1 taken 34 times.
✗ Branch 2 not taken.
|
34 | validator_ = std::make_unique<QueryParamValidator>(doc_, keys_); |
126 | 34 | } | |
127 | |||
128 | std::unique_ptr<QueryParamValidator> validator_; | ||
129 | std::string input_; | ||
130 | rapidjson::Document doc_; | ||
131 | std::vector<std::string> keys_{"paths", "/pets/xyz", "get", "parameters", "param"}; | ||
132 | std::string error_msg_; | ||
133 | ValidationError expected_error_; | ||
134 | }; | ||
135 | |||
136 |
6/12✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 9 taken 1 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 1 times.
✗ Branch 13 not taken.
✓ Branch 16 taken 1 times.
✗ Branch 17 not taken.
✓ Branch 20 taken 1 times.
✗ Branch 21 not taken.
✓ Branch 24 taken 1 times.
✗ Branch 25 not taken.
|
138 | TEST_P(QueryArrayParam, ValidateParam) |
137 | { | ||
138 |
1/2✓ Branch 3 taken 34 times.
✗ Branch 4 not taken.
|
68 | const char* beg = input_.c_str() + std::string("/pets/xyz?").size(); |
139 | 68 | const char* end = input_.c_str() + input_.size(); | |
140 |
1/2✓ Branch 2 taken 34 times.
✗ Branch 3 not taken.
|
68 | auto result = validator_->ValidateParam(beg, end, error_msg_); |
141 |
2/10✓ Branch 1 taken 34 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 34 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
|
68 | EXPECT_EQ(result, expected_error_); |
142 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 24 times.
|
68 | if (result != ValidationError::NONE) { |
143 |
1/10✗ Branch 3 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
|
20 | EXPECT_FALSE(error_msg_.empty()); |
144 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
40 | rapidjson::Document doc; |
145 |
1/2✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
|
20 | doc.Parse(error_msg_.c_str()); |
146 |
1/10✗ Branch 3 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
|
20 | EXPECT_FALSE(doc.HasParseError()); |
147 | } | ||
148 | 68 | } | |
149 | |||
150 |
37/109✓ Branch 1 taken 1 times.
✓ Branch 2 taken 34 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 1 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✓ Branch 16 taken 1 times.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✓ Branch 19 taken 1 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✓ Branch 22 taken 1 times.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✓ Branch 25 taken 1 times.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✓ Branch 28 taken 1 times.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✓ Branch 31 taken 1 times.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
✓ Branch 34 taken 1 times.
✗ Branch 35 not taken.
✗ Branch 36 not taken.
✓ Branch 37 taken 1 times.
✗ Branch 38 not taken.
✗ Branch 39 not taken.
✓ Branch 40 taken 1 times.
✗ Branch 41 not taken.
✗ Branch 42 not taken.
✓ Branch 43 taken 1 times.
✗ Branch 44 not taken.
✗ Branch 45 not taken.
✓ Branch 46 taken 1 times.
✗ Branch 47 not taken.
✗ Branch 48 not taken.
✓ Branch 49 taken 1 times.
✗ Branch 50 not taken.
✗ Branch 51 not taken.
✓ Branch 52 taken 1 times.
✗ Branch 53 not taken.
✗ Branch 54 not taken.
✓ Branch 55 taken 1 times.
✗ Branch 56 not taken.
✗ Branch 57 not taken.
✓ Branch 58 taken 1 times.
✗ Branch 59 not taken.
✗ Branch 60 not taken.
✓ Branch 61 taken 1 times.
✗ Branch 62 not taken.
✗ Branch 63 not taken.
✓ Branch 64 taken 1 times.
✗ Branch 65 not taken.
✗ Branch 66 not taken.
✓ Branch 67 taken 1 times.
✗ Branch 68 not taken.
✗ Branch 69 not taken.
✓ Branch 70 taken 1 times.
✗ Branch 71 not taken.
✗ Branch 72 not taken.
✓ Branch 73 taken 1 times.
✗ Branch 74 not taken.
✗ Branch 75 not taken.
✓ Branch 76 taken 1 times.
✗ Branch 77 not taken.
✗ Branch 78 not taken.
✓ Branch 79 taken 1 times.
✗ Branch 80 not taken.
✗ Branch 81 not taken.
✓ Branch 82 taken 1 times.
✗ Branch 83 not taken.
✗ Branch 84 not taken.
✓ Branch 85 taken 1 times.
✗ Branch 86 not taken.
✗ Branch 87 not taken.
✓ Branch 88 taken 1 times.
✗ Branch 89 not taken.
✗ Branch 90 not taken.
✓ Branch 91 taken 1 times.
✗ Branch 92 not taken.
✗ Branch 93 not taken.
✓ Branch 94 taken 1 times.
✗ Branch 95 not taken.
✗ Branch 96 not taken.
✓ Branch 97 taken 1 times.
✗ Branch 98 not taken.
✗ Branch 99 not taken.
✓ Branch 100 taken 1 times.
✗ Branch 101 not taken.
✗ Branch 102 not taken.
✓ Branch 103 taken 1 times.
✗ Branch 104 not taken.
✗ Branch 105 not taken.
✓ Branch 106 taken 1 times.
✗ Branch 107 not taken.
✗ Branch 108 not taken.
✗ Branch 110 not taken.
✗ Branch 111 not taken.
|
72 | INSTANTIATE_TEST_SUITE_P( |
151 | QueryArrayParamTests, QueryArrayParam, | ||
152 | ::testing::Values( | ||
153 | std::make_tuple("param=3,4,5", "form", false, "integer", ValidationError::NONE), | ||
154 | std::make_tuple("param=3¶m=4¶m=5", "form", true, "integer", ValidationError::NONE), | ||
155 | std::make_tuple("param=3.3,4.4,5.5", "form", false, "number", ValidationError::NONE), | ||
156 | std::make_tuple("param=3.3¶m=4.4¶m=5.5", "form", true, "number", ValidationError::NONE), | ||
157 | std::make_tuple("param=abc%20xyz,def%20xyz,ghi%20xyz", "form", false, "string", ValidationError::NONE), | ||
158 | std::make_tuple("param=abc%20xyz¶m=def%20xyz¶m=ghi%20xyz", "form", true, "string", | ||
159 | ValidationError::NONE), | ||
160 | std::make_tuple("param=true,false,true", "form", false, "boolean", ValidationError::NONE), | ||
161 | std::make_tuple("param=true¶m=false¶m=true", "form", true, "boolean", ValidationError::NONE), | ||
162 | std::make_tuple("param=string,abc,xyz", "form", false, "integer", ValidationError::INVALID_QUERY_PARAM), | ||
163 | std::make_tuple("param=string¶m=abc¶m=xyz", "form", true, "integer", | ||
164 | ValidationError::INVALID_QUERY_PARAM), | ||
165 | std::make_tuple("param=3.3,4.4,5.5", "form", false, "integer", ValidationError::INVALID_QUERY_PARAM), | ||
166 | std::make_tuple("param=3.3¶m=4.4¶m=5.5", "form", true, "integer", ValidationError::INVALID_QUERY_PARAM), | ||
167 | std::make_tuple("param=true,false,true", "form", false, "number", ValidationError::INVALID_QUERY_PARAM), | ||
168 | std::make_tuple("param=true¶m=false¶m=true", "form", true, "number", | ||
169 | ValidationError::INVALID_QUERY_PARAM), | ||
170 | std::make_tuple("param=3,4,5", "form", false, "boolean", ValidationError::INVALID_QUERY_PARAM), | ||
171 | std::make_tuple("param=3%204%205", "spaceDelimited", false, "integer", ValidationError::NONE), | ||
172 | std::make_tuple("param=3¶m=4¶m=5", "spaceDelimited", true, "integer", ValidationError::NONE), | ||
173 | std::make_tuple("param=3.3%204.4%205.5", "spaceDelimited", false, "number", ValidationError::NONE), | ||
174 | std::make_tuple("param=3.3¶m=4.4¶m=5.5", "spaceDelimited", true, "number", ValidationError::NONE), | ||
175 | std::make_tuple("param=abc%20xyz%20def%20xyz%20ghi%20xyz", "spaceDelimited", false, "string", | ||
176 | ValidationError::NONE), | ||
177 | std::make_tuple("param=abc%20xyz¶m=def%20xyz¶m=ghi%20xyz", "spaceDelimited", true, "string", | ||
178 | ValidationError::NONE), | ||
179 | std::make_tuple("param=true%20false%20true", "spaceDelimited", false, "boolean", ValidationError::NONE), | ||
180 | std::make_tuple("param=true¶m=false¶m=true", "spaceDelimited", true, "boolean", ValidationError::NONE), | ||
181 | std::make_tuple("param=string%20abc%20xyz", "spaceDelimited", false, "integer", | ||
182 | ValidationError::INVALID_QUERY_PARAM), | ||
183 | std::make_tuple("param=string¶m=abc¶m=xyz", "spaceDelimited", true, "integer", | ||
184 | ValidationError::INVALID_QUERY_PARAM), | ||
185 | std::make_tuple("param=3|4|5", "pipeDelimited", false, "integer", ValidationError::NONE), | ||
186 | std::make_tuple("param=3¶m=4¶m=5", "pipeDelimited", true, "integer", ValidationError::NONE), | ||
187 | std::make_tuple("param=3.3|4.4|5.5", "pipeDelimited", false, "number", ValidationError::NONE), | ||
188 | std::make_tuple("param=3.3¶m=4.4¶m=5.5", "pipeDelimited", true, "number", ValidationError::NONE), | ||
189 | std::make_tuple("param=abc%20xyz|def%20xyz|ghi%20xyz", "pipeDelimited", false, "string", ValidationError::NONE), | ||
190 | std::make_tuple("param=abc%20xyz¶m=def%20xyz¶m=ghi%20xyz", "pipeDelimited", true, "string", | ||
191 | ValidationError::NONE), | ||
192 | std::make_tuple("param=true|false|true", "pipeDelimited", false, "boolean", ValidationError::NONE), | ||
193 | std::make_tuple("param=true¶m=false¶m=true", "pipeDelimited", true, "boolean", ValidationError::NONE), | ||
194 | std::make_tuple("param=string|abc|xyz", "pipeDelimited", false, "integer", | ||
195 | ValidationError::INVALID_QUERY_PARAM))); | ||
196 | |||
197 | class QueryObjectParam: public ::testing::TestWithParam<std::tuple<std::string, std::string, bool, ValidationError>> | ||
198 | { | ||
199 | protected: | ||
200 | 3 | void SetUp() override | |
201 | { | ||
202 |
2/4✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
|
3 | auto params = GetParam(); |
203 |
1/2✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 | input_ = "/pets/xyz?" + std::get<0>(params); |
204 |
1/2✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 | std::string style = std::get<1>(params); |
205 | 3 | bool explode = std::get<2>(params); | |
206 | 3 | expected_error_ = std::get<3>(params); | |
207 | |||
208 | std::string json = R"({ | ||
209 | "name": "param", | ||
210 | "in": "query", | ||
211 | "required": true, | ||
212 |
2/4✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
|
6 | "style": ")" + style + |
213 | R"(", | ||
214 |
3/4✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
|
9 | "explode": )" + (explode ? "true" : "false") + |
215 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | R"(, |
216 | "schema": { | ||
217 | "type": "object", | ||
218 | "properties": { | ||
219 | "boolTrue": { | ||
220 | "type": "boolean" | ||
221 | }, | ||
222 | "boolFalse": { | ||
223 | "type": "boolean" | ||
224 | }, | ||
225 | "int": { | ||
226 | "type": "integer" | ||
227 | }, | ||
228 | "number": { | ||
229 | "type": "number" | ||
230 | }, | ||
231 | "string": { | ||
232 | "type": "string" | ||
233 | } | ||
234 | } | ||
235 | } | ||
236 | })"; | ||
237 |
1/2✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 | doc_.Parse(json.c_str()); |
238 | |||
239 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | validator_ = std::make_unique<QueryParamValidator>(doc_, keys_); |
240 | 3 | } | |
241 | |||
242 | std::unique_ptr<QueryParamValidator> validator_; | ||
243 | std::string input_; | ||
244 | rapidjson::Document doc_; | ||
245 | std::vector<std::string> keys_{"paths", "/pets/xyz", "get", "parameters", "param"}; | ||
246 | std::string error_msg_; | ||
247 | ValidationError expected_error_; | ||
248 | }; | ||
249 | |||
250 |
6/12✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 9 taken 1 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 1 times.
✗ Branch 13 not taken.
✓ Branch 16 taken 1 times.
✗ Branch 17 not taken.
✓ Branch 20 taken 1 times.
✗ Branch 21 not taken.
✓ Branch 24 taken 1 times.
✗ Branch 25 not taken.
|
14 | TEST_P(QueryObjectParam, ValidateParam) |
251 | { | ||
252 |
1/2✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
|
6 | const char* beg = input_.c_str() + std::string("/pets/xyz?").size(); |
253 | 6 | const char* end = input_.c_str() + input_.size(); | |
254 |
1/2✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
6 | auto result = validator_->ValidateParam(beg, end, error_msg_); |
255 |
2/10✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
|
6 | EXPECT_EQ(result, expected_error_); |
256 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
6 | if (result != ValidationError::NONE) { |
257 | ✗ | EXPECT_FALSE(error_msg_.empty()); | |
258 | ✗ | rapidjson::Document doc; | |
259 | ✗ | doc.Parse(error_msg_.c_str()); | |
260 | ✗ | EXPECT_FALSE(doc.HasParseError()); | |
261 | } | ||
262 | 6 | } | |
263 | |||
264 |
6/16✓ Branch 1 taken 1 times.
✓ Branch 2 taken 3 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 1 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
|
10 | INSTANTIATE_TEST_SUITE_P( |
265 | QueryObjectParamTests, QueryObjectParam, | ||
266 | ::testing::Values( | ||
267 | std::make_tuple("param=boolTrue,true,boolFalse,false,int,123,number,123.456,string,abc%20xyz", "form", false, | ||
268 | ValidationError::NONE), | ||
269 | std::make_tuple("boolTrue=true&boolFalse=false&int=123&number=123.456&string=abc%20xyz", "form", true, | ||
270 | ValidationError::NONE), | ||
271 | std::make_tuple( | ||
272 | "param[boolTrue]=true¶m[boolFalse]=false¶m[int]=123¶m[number]=123.456¶m[string]=abc%20xyz", | ||
273 | "deepObject", true, ValidationError::NONE))); | ||
274 |