Skip to content

Commit

Permalink
Add url::parse function with URL string and base URL string parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
rmisev committed Jul 13, 2024
1 parent 80448b9 commit 5c29303
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
14 changes: 14 additions & 0 deletions include/upa/url.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,20 @@ class url {
return parse(std::forward<T>(str_url), &base);
}

/// @brief Parses given URL string against base URL.
///
/// @param[in] str_url URL string to parse
/// @param[in] str_base base URL string
/// @return error code (@a validation_errc::ok on success)
template <class T, class TB, enable_if_str_arg_t<T> = 0, enable_if_str_arg_t<TB> = 0>
validation_errc parse(T&& str_url, TB&& str_base) {
upa::url base;
const auto res = base.parse(std::forward<TB>(str_base), nullptr);
return res == validation_errc::ok
? parse(std::forward<T>(str_url), &base)
: res;
}

/// @brief Checks if a given URL string can be successfully parsed
///
/// If @a pbase is not nullptr, then try to parse against *pbase URL.
Expand Down
8 changes: 7 additions & 1 deletion test/test-url.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ TEST_CASE("url parsing constructor with base URL string") {

// Parse URL

TEST_CASE("Two url::parse functions") {
TEST_CASE("url::parse functions") {
upa::url url;
upa::url url_base;

Expand All @@ -194,6 +194,12 @@ TEST_CASE("Two url::parse functions") {
// second url::parse function
CHECK(url.parse("/path", url_base) == upa::validation_errc::ok);
CHECK(url.href() == "http://example.org/path");

// third url::parse function
CHECK(upa::success(url.parse("/path", "http://example.org")));
CHECK(url.href() == "http://example.org/path");
// third url::parse function with invalid base URL string
CHECK_FALSE(upa::success(url.parse("http://example.org", ":123")));
}

TEST_CASE("url::parse must clear old URL data") {
Expand Down
12 changes: 3 additions & 9 deletions test/wpt-url.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,9 @@ void test_parser(DataDrivenTest& ddt, ParserObj& obj)
ddt.test_case(str_case, [&](DataDrivenTest::TestCase& tc) {
upa::url url;

bool parse_success;
if (!base.empty()) {
upa::url url_base;
parse_success =
upa::success(url_base.parse(base)) &&
upa::success(url.parse(input, url_base));
} else {
parse_success = upa::success(url.parse(input));
}
bool parse_success = base.empty()
? upa::success(url.parse(input))
: upa::success(url.parse(input, base));

// check "failure"
tc.assert_equal(obj.failure, !parse_success, "parse failure");
Expand Down

0 comments on commit 5c29303

Please sign in to comment.