Audacity 3.2.0
UriParserTests.cpp
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-or-later
3 * SPDX-FileName: UriParser.cpp
4 * SPDX-FileContributor: Dmitry Vedenko
5 */
6
7
8#include <catch2/catch.hpp>
9
10#include "UriParser.h"
11
12TEST_CASE("ParseUri", "[StringUtils]")
13{
14 SECTION("Empty URI")
15 {
16 auto result = ParseUri("");
17 REQUIRE(result.Scheme.empty());
18 REQUIRE(result.UserInfo.empty());
19 REQUIRE(result.Host.empty());
20 REQUIRE(result.Port.empty());
21 REQUIRE(result.Path.empty());
22 REQUIRE(result.Query.empty());
23 REQUIRE(result.Fragment.empty());
24 }
25
26 SECTION("URI without scheme")
27 {
28 auto result = ParseUri("test");
29 REQUIRE(result.Scheme.empty());
30 REQUIRE(result.UserInfo.empty());
31 REQUIRE(result.Host == "test");
32 REQUIRE(result.Port.empty());
33 REQUIRE(result.Path.empty());
34 REQUIRE(result.Query.empty());
35 REQUIRE(result.Fragment.empty());
36 }
37
38 SECTION("URI with scheme")
39 {
40 auto result = ParseUri("http://test");
41 REQUIRE(result.Scheme == "http");
42 REQUIRE(result.UserInfo.empty());
43 REQUIRE(result.Host == "test");
44 REQUIRE(result.Port.empty());
45 REQUIRE(result.Path.empty());
46 REQUIRE(result.Query.empty());
47 REQUIRE(result.Fragment.empty());
48 }
49
50 SECTION("URI with scheme and fragment")
51 {
52 auto result = ParseUri("http://test#fragment");
53 REQUIRE(result.Scheme == "http");
54 REQUIRE(result.UserInfo.empty());
55 REQUIRE(result.Host == "test");
56 REQUIRE(result.Port.empty());
57 REQUIRE(result.Path.empty());
58 REQUIRE(result.Query.empty());
59 REQUIRE(result.Fragment == "fragment");
60 }
61
62 SECTION("URI with scheme and query")
63 {
64 auto result = ParseUri("http://test?query");
65 REQUIRE(result.Scheme == "http");
66 REQUIRE(result.UserInfo.empty());
67 REQUIRE(result.Host == "test");
68 REQUIRE(result.Port.empty());
69 REQUIRE(result.Path.empty());
70 REQUIRE(result.Query == "query");
71 REQUIRE(result.Fragment.empty());
72 }
73
74 SECTION("URI with scheme and path")
75 {
76 auto result = ParseUri("http://test/path");
77 REQUIRE(result.Scheme == "http");
78 REQUIRE(result.UserInfo.empty());
79 REQUIRE(result.Host == "test");
80 REQUIRE(result.Port.empty());
81 REQUIRE(result.Path == "path");
82 REQUIRE(result.Query.empty());
83 REQUIRE(result.Fragment.empty());
84 }
85
86 SECTION("URI with scheme, path and query")
87 {
88 auto result = ParseUri("http://test/path?query");
89 REQUIRE(result.Scheme == "http");
90 REQUIRE(result.UserInfo.empty());
91 REQUIRE(result.Host == "test");
92 REQUIRE(result.Port.empty());
93 REQUIRE(result.Path == "path");
94 REQUIRE(result.Query == "query");
95 REQUIRE(result.Fragment.empty());
96 }
97
98 SECTION("URI with scheme, path and fragment")
99 {
100 auto result = ParseUri("http://test/path#fragment");
101 REQUIRE(result.Scheme == "http");
102 REQUIRE(result.UserInfo.empty());
103 REQUIRE(result.Host == "test");
104 REQUIRE(result.Port.empty());
105 REQUIRE(result.Path == "path");
106 REQUIRE(result.Query.empty());
107 REQUIRE(result.Fragment == "fragment");
108 }
109
110 SECTION("URI with scheme, path, query and fragment")
111 {
112 auto result = ParseUri("http://test/path?query#fragment");
113 REQUIRE(result.Scheme == "http");
114 REQUIRE(result.UserInfo.empty());
115 REQUIRE(result.Host == "test");
116 REQUIRE(result.Port.empty());
117 REQUIRE(result.Path == "path");
118 REQUIRE(result.Query == "query");
119 REQUIRE(result.Fragment == "fragment");
120 }
121
122 SECTION("URI with scheme, user info, host, port, path, query and fragment")
123 {
124 auto result =
125 ParseUri("http://user:password@test:1234/path?query#fragment");
126 REQUIRE(result.Scheme == "http");
127 REQUIRE(result.UserInfo == "user:password");
128 REQUIRE(result.Host == "test");
129 REQUIRE(result.Port == "1234");
130 REQUIRE(result.Path == "path");
131 REQUIRE(result.Query == "query");
132 REQUIRE(result.Fragment == "fragment");
133 }
134}
135
136
137TEST_CASE("ParseUriQuery", "[StringUtils]")
138{
139 SECTION("Empty query")
140 {
141 auto result = ParseUriQuery("");
142 REQUIRE(result.empty());
143 }
144
145 SECTION("Query without delimiter")
146 {
147 auto result = ParseUriQuery("test");
148 REQUIRE(result.size() == 1);
149 REQUIRE(result["test"].empty());
150 }
151
152 SECTION("Query with single item")
153 {
154 auto result = ParseUriQuery("test=value");
155 REQUIRE(result.size() == 1);
156 REQUIRE(result["test"] == "value");
157 }
158
159 SECTION("Query with multiple items")
160 {
161 auto result = ParseUriQuery("test1=value1&test2=value2");
162 REQUIRE(result.size() == 2);
163 REQUIRE(result["test1"] == "value1");
164 REQUIRE(result["test2"] == "value2");
165 }
166
167 SECTION("Query with multiple items without values")
168 {
169 auto result = ParseUriQuery("test1&test2");
170 REQUIRE(result.size() == 2);
171 REQUIRE(result["test1"].empty());
172 REQUIRE(result["test2"].empty());
173 }
174
175 SECTION("Query with multiple mixed items")
176 {
177 auto result = ParseUriQuery("test1&test2=value&test3");
178 REQUIRE(result.size() == 3);
179 REQUIRE(result["test1"].empty());
180 REQUIRE(result["test2"] == "value");
181 REQUIRE(result["test3"].empty());
182 }
183
184 SECTION("Query with multiple items and custom delimiter")
185 {
186 auto result = ParseUriQuery("test1=value1;test2=value2", ";");
187 REQUIRE(result.size() == 2);
188 REQUIRE(result["test1"] == "value1");
189 REQUIRE(result["test2"] == "value2");
190 }
191}
QueryFields ParseUriQuery(std::string_view query, std::string_view delimiter) noexcept
Parses URI query and returns QueryFields structure with parsed fields.
Definition: UriParser.cpp:59
UriFields ParseUri(std::string_view uri) noexcept
Definition: UriParser.cpp:9
TEST_CASE("ParseUri", "[StringUtils]")