Skip to content

Commit

Permalink
Add alternative setters and getters to the `url' class
Browse files Browse the repository at this point in the history
Add setters with a set_ prefix, and getters with a get_ prefix, for
example: set_href and get_href. Having getter and setter functions under
different names can be useful in some cases.
  • Loading branch information
rmisev committed Sep 7, 2023
1 parent 0b19cb7 commit 23eab11
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
52 changes: 52 additions & 0 deletions include/upa/url.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ class url {
/// @return `true` - on success; `false` - on failure
template <class StrT, enable_if_str_arg_t<StrT> = 0>
bool href(StrT&& str);
/// Equivalent to @link href(StrT&& str) @endlink
template <class StrT, enable_if_str_arg_t<StrT> = 0>
bool set_href(StrT&& str) { return href(str); }

/// @brief The protocol setter
///
Expand All @@ -242,6 +245,9 @@ class url {
/// @return `true` - on success; `false` - on failure (URL protocol unchanged)
template <class StrT, enable_if_str_arg_t<StrT> = 0>
bool protocol(StrT&& str);
/// Equivalent to @link protocol(StrT&& str) @endlink
template <class StrT, enable_if_str_arg_t<StrT> = 0>
bool set_protocol(StrT&& str) { return protocol(str); }

/// @brief The username setter
///
Expand All @@ -252,6 +258,9 @@ class url {
/// @return `true` - on success; `false` - if username can not be set
template <class StrT, enable_if_str_arg_t<StrT> = 0>
bool username(StrT&& str);
/// Equivalent to @link username(StrT&& str) @endlink
template <class StrT, enable_if_str_arg_t<StrT> = 0>
bool set_username(StrT&& str) { return username(str); }

/// @brief The password setter
///
Expand All @@ -262,6 +271,9 @@ class url {
/// @return `true` - on success; `false` - if password can not be set
template <class StrT, enable_if_str_arg_t<StrT> = 0>
bool password(StrT&& str);
/// Equivalent to @link password(StrT&& str) @endlink
template <class StrT, enable_if_str_arg_t<StrT> = 0>
bool set_password(StrT&& str) { return password(str); }

/// @brief The host setter
///
Expand All @@ -272,6 +284,9 @@ class url {
/// @return `true` - on success; `false` - on failure (URL's host and port unchanged)
template <class StrT, enable_if_str_arg_t<StrT> = 0>
bool host(StrT&& str);
/// Equivalent to @link host(StrT&& str) @endlink
template <class StrT, enable_if_str_arg_t<StrT> = 0>
bool set_host(StrT&& str) { return host(str); }

/// @brief The hostname setter
///
Expand All @@ -282,6 +297,9 @@ class url {
/// @return `true` - on success; `false` - on failure (URL's host unchanged)
template <class StrT, enable_if_str_arg_t<StrT> = 0>
bool hostname(StrT&& str);
/// Equivalent to @link hostname(StrT&& str) @endlink
template <class StrT, enable_if_str_arg_t<StrT> = 0>
bool set_hostname(StrT&& str) { return hostname(str); }

/// @brief The port setter
///
Expand All @@ -292,6 +310,9 @@ class url {
/// @return `true` - on success; `false` - on failure (URL's port unchanged)
template <class StrT, enable_if_str_arg_t<StrT> = 0>
bool port(StrT&& str);
/// Equivalent to @link port(StrT&& str) @endlink
template <class StrT, enable_if_str_arg_t<StrT> = 0>
bool set_port(StrT&& str) { return port(str); }

/// @brief The pathname setter
///
Expand All @@ -302,6 +323,9 @@ class url {
/// @return `true` - on success; `false` - on failure (URL's path unchanged)
template <class StrT, enable_if_str_arg_t<StrT> = 0>
bool pathname(StrT&& str);
/// Equivalent to @link pathname(StrT&& str) @endlink
template <class StrT, enable_if_str_arg_t<StrT> = 0>
bool set_pathname(StrT&& str) { return pathname(str); }

/// @brief The search setter
///
Expand All @@ -312,6 +336,9 @@ class url {
/// @return `true` - on success; `false` - on failure (URL's query unchanged)
template <class StrT, enable_if_str_arg_t<StrT> = 0>
bool search(StrT&& str);
/// Equivalent to @link search(StrT&& str) @endlink
template <class StrT, enable_if_str_arg_t<StrT> = 0>
bool set_search(StrT&& str) { return search(str); }

/// @brief The hash setter
///
Expand All @@ -322,6 +349,9 @@ class url {
/// @return `true` - on success; `false` - on failure (URL's fragment unchanged)
template <class StrT, enable_if_str_arg_t<StrT> = 0>
bool hash(StrT&& str);
/// Equivalent to @link hash(StrT&& str) @endlink
template <class StrT, enable_if_str_arg_t<StrT> = 0>
bool set_hash(StrT&& str) { return hash(str); }

// Getters

Expand All @@ -331,6 +361,8 @@ class url {
///
/// @return serialized URL
str_view_type href() const;
/// Equivalent to @link href() const @endlink
str_view_type get_href() const { return href(); }

/// @brief The origin getter
///
Expand All @@ -348,34 +380,44 @@ class url {
///
/// @return URL's scheme, followed by U+003A (:)
str_view_type protocol() const;
/// Equivalent to @link protocol() const @endlink
str_view_type get_protocol() const { return protocol(); }

/// @brief The username getter
///
/// More info: https://url.spec.whatwg.org/#dom-url-username
///
/// @return URL’s username
str_view_type username() const;
/// Equivalent to @link username() const @endlink
str_view_type get_username() const { return username(); }

/// @brief The password getter
///
/// More info: https://url.spec.whatwg.org/#dom-url-password
///
/// @return URL’s password
str_view_type password() const;
/// Equivalent to @link password() const @endlink
str_view_type get_password() const { return password(); }

/// @brief The host getter
///
/// More info: https://url.spec.whatwg.org/#dom-url-host
///
/// @return URL’s host, serialized, followed by U+003A (:) and URL’s port, serialized
str_view_type host() const;
/// Equivalent to @link host() const @endlink
str_view_type get_host() const { return host(); }

/// @brief The hostname getter
///
/// More info: https://url.spec.whatwg.org/#dom-url-hostname
///
/// @return URL’s host, serialized
str_view_type hostname() const;
/// Equivalent to @link hostname() const @endlink
str_view_type get_hostname() const { return hostname(); }

/// @brief The host_type getter
///
Expand All @@ -388,6 +430,8 @@ class url {
///
/// @return URL’s port, serialized, if URL’s port is not null, otherwise empty string
str_view_type port() const;
/// Equivalent to @link port() const @endlink
str_view_type get_port() const { return port(); }

/// @return URL’s port, converted to `int` value, if URL’s port is not null,
/// otherwise `-1`
Expand All @@ -402,27 +446,35 @@ class url {
///
/// @return URL's path, serialized, followed by U+003F (?) and URL’s query
str_view_type path() const;
/// Equivalent to @link path() const @endlink
str_view_type get_path() const { return path(); }

/// @brief The pathname getter
///
/// More info: https://url.spec.whatwg.org/#dom-url-pathname
///
/// @return URL’s path, serialized
str_view_type pathname() const;
/// Equivalent to @link pathname() const @endlink
str_view_type get_pathname() const { return pathname(); }

/// @brief The search getter
///
/// More info: https://url.spec.whatwg.org/#dom-url-search
///
/// @return empty string or U+003F (?), followed by URL’s query
str_view_type search() const;
/// Equivalent to @link search() const @endlink
str_view_type get_search() const { return search(); }

/// @brief The hash getter
///
/// More info: https://url.spec.whatwg.org/#dom-url-hash
///
/// @return empty string or U+0023 (#), followed by URL’s fragment
str_view_type hash() const;
/// Equivalent to @link hash() const @endlink
str_view_type get_hash() const { return hash(); }

/// @brief The searchParams getter
///
Expand Down
57 changes: 57 additions & 0 deletions test/test-url-setters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,60 @@ TEST_CASE("Test setters (url_setter::start_part with use_strp_ = false)") {
CHECK(url.hash() == "");
}
}

// Test alternative getters / setters

TEST_CASE("Test url::get_... getters") {
upa::url url{ "ws://user:psw@h:54321\\p1\\p2?q#f" };

CHECK(url.get_href() == "ws://user:psw@h:54321/p1/p2?q#f");
CHECK(url.get_protocol() == "ws:");
CHECK(url.get_username() == "user");
CHECK(url.get_password() == "psw");
CHECK(url.get_host() == "h:54321");
CHECK(url.get_hostname() == "h");
CHECK(url.get_port() == "54321");
CHECK(url.get_path() == "/p1/p2?q");
CHECK(url.get_pathname() == "/p1/p2");
CHECK(url.get_search() == "?q");
CHECK(url.get_hash() == "#f");
}

TEST_CASE("Test url::set_... setters") {
upa::url url{ "ws://h" };

CHECK(url.set_href("wss://host"));
CHECK(url.get_href() == "wss://host/");

CHECK(url.set_protocol("http"));
CHECK(url.get_protocol() == "http:");

CHECK(url.set_username("user"));
CHECK(url.get_username() == "user");

CHECK(url.set_password("psw"));
CHECK(url.get_password() == "psw");

CHECK(url.set_host("h:54321"));
CHECK(url.get_host() == "h:54321");
CHECK(url.get_hostname() == "h");
CHECK(url.get_port() == "54321");

CHECK(url.set_hostname("hostname"));
CHECK(url.get_hostname() == "hostname");

CHECK(url.set_port("61234"));
CHECK(url.get_port() == "61234");

CHECK(url.set_pathname("\\p1\\p2"));
CHECK(url.get_pathname() == "/p1/p2");

CHECK(url.set_search("q"));
CHECK(url.get_search() == "?q");
CHECK(url.get_path() == "/p1/p2?q");

CHECK(url.set_hash("f"));
CHECK(url.get_hash() == "#f");

CHECK(url.href() == "http://user:psw@hostname:61234/p1/p2?q#f");
}

0 comments on commit 23eab11

Please sign in to comment.