Skip to content

Commit

Permalink
Simplify some calls to url::parse functions
Browse files Browse the repository at this point in the history
  • Loading branch information
rmisev committed Oct 19, 2023
1 parent 9eec5ee commit bf847aa
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 41 deletions.
6 changes: 3 additions & 3 deletions examples/urlparse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ void read_samples(const char* file_name, SamplesOutput& out)
if (icolon != line.npos) {
string_view val{line.data() + icolon + 1, line.length() - (icolon + 1) };
if (line.compare(0, icolon, "BASE") == 0) {
const auto res = url_base.parse(val, nullptr);
const auto res = url_base.parse(val);
ok = upa::success(res);
} else if (line.compare(0, icolon, "COMMENT") == 0) {
out.comment(val);
Expand Down Expand Up @@ -303,7 +303,7 @@ bool read_setter(std::ifstream& file, const char* name, const char* name_end) {
string_view val{ line.data() + icolon + 1, line.length() - (icolon + 1) };
if (line.compare(0, icolon, "url") == 0) {
std::cout << "URL=" << val << std::endl;
ok = upa::success(url.parse(val, nullptr));
ok = upa::success(url.parse(val));
} else if (line.compare(0, icolon, "val") == 0) {
// set value
if (strName == "protocol") {
Expand Down Expand Up @@ -381,7 +381,7 @@ void test_interactive(const char* szBaseUrl)
// parse base URL
upa::url url_base;
if (szBaseUrl) {
if (!upa::success(url_base.parse(szBaseUrl, nullptr))) {
if (!upa::success(url_base.parse(szBaseUrl))) {
std::cout << szBaseUrl << "\n";
std::cout << " ^-BASE-PARSE-FAILURE\n";
return;
Expand Down
4 changes: 2 additions & 2 deletions test/test-ipv4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ TEST_CASE("127.0.0.1 percent encoded") {
upa::url url;

// 127.0.0.1 percent encoded
CHECK(upa::success(url.parse("http://12%37.0.0.1/", nullptr)));
CHECK(upa::success(url.parse("http://12%37.0.0.1/")));
CHECK(url.hostname() == "127.0.0.1");

// 0x7f.0.0.1 percent encoded
CHECK(upa::success(url.parse("http://%30%78%37%66.0.0.1/", nullptr)));
CHECK(upa::success(url.parse("http://%30%78%37%66.0.0.1/")));
CHECK(url.hostname() == "127.0.0.1");
}
16 changes: 8 additions & 8 deletions test/test-url-port.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@

TEST_CASE("http scheme default port") {
upa::url url;
CHECK(upa::success(url.parse("http://aaa/", nullptr)));
CHECK(upa::success(url.parse("http://aaa/")));
CHECK(url.port_int() == -1);
CHECK(url.real_port_int() == 80);
}

TEST_CASE("http scheme 8080 port") {
upa::url url;
CHECK(upa::success(url.parse("http://aaa:8080/", nullptr)));
CHECK(upa::success(url.parse("http://aaa:8080/")));
CHECK(url.port_int() == 8080);
CHECK(url.real_port_int() == 8080);
}

TEST_CASE("non-special scheme default port") {
upa::url url;
CHECK(upa::success(url.parse("non-special://aaa/", nullptr)));
CHECK(upa::success(url.parse("non-special://aaa/")));
CHECK(url.port_int() == -1);
CHECK(url.real_port_int() == -1);
}

TEST_CASE("non-special scheme 123 port") {
upa::url url;
CHECK(upa::success(url.parse("non-special://aaa:123/", nullptr)));
CHECK(upa::success(url.parse("non-special://aaa:123/")));
CHECK(url.port_int() == 123);
CHECK(url.real_port_int() == 123);
}
Expand All @@ -39,11 +39,11 @@ TEST_CASE("port overflow") {
upa::url url;

// https://github.com/whatwg/url/issues/257#issuecomment-285553590
CHECK(upa::success(url.parse("http://example.net:65535", nullptr)));
CHECK(upa::success(url.parse("http://example.net:65535")));
CHECK(url.port_int() == 65535);
CHECK_FALSE(upa::success(url.parse("http://example.net:65536", nullptr)));
CHECK_FALSE(upa::success(url.parse("http://example.net:65536")));

CHECK(upa::success(url.parse("asdf://host:65535", nullptr)));
CHECK(upa::success(url.parse("asdf://host:65535")));
CHECK(url.port_int() == 65535);
CHECK_FALSE(upa::success(url.parse("asdf://host:65536", nullptr)));
CHECK_FALSE(upa::success(url.parse("asdf://host:65536")));
}
6 changes: 3 additions & 3 deletions test/test-url-setters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
TEST_CASE("Test setters with special URL's") {
upa::url url;

REQUIRE(upa::success(url.parse("ws://example.org/foo/bar", nullptr)));
REQUIRE(upa::success(url.parse("ws://example.org/foo/bar")));

SUBCASE("Check getters") {
CHECK_EQ(url.href(), "ws://example.org/foo/bar");
Expand Down Expand Up @@ -86,7 +86,7 @@ TEST_CASE("Test setters with non-special URL's") {
upa::url url;

SUBCASE("non-special: protocol") {
REQUIRE(upa::success(url.parse("non-special:/path", nullptr)));
REQUIRE(upa::success(url.parse("non-special:/path")));
CHECK_EQ(url.href(), "non-special:/path");

CHECK(url.hostname("example.net"));
Expand All @@ -97,7 +97,7 @@ TEST_CASE("Test setters with non-special URL's") {
}

SUBCASE("javascript: protocol") {
REQUIRE(upa::success(url.parse("JavaScript:alert(1)", nullptr)));
REQUIRE(upa::success(url.parse("JavaScript:alert(1)")));
CHECK_EQ(url.href(), "javascript:alert(1)");

CHECK(url.hash("#frag"));
Expand Down
38 changes: 19 additions & 19 deletions test/test-url.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@ TEST_CASE("Two url::parse functions") {
TEST_CASE("url::parse must clear old URL data") {
upa::url url;

CHECK(upa::success(url.parse("about:blank", nullptr)));
CHECK(upa::success(url.parse("about:blank")));
CHECK_FALSE(url.empty());

CHECK(upa::success(url.parse("http://host-1/", nullptr)));
CHECK(upa::success(url.parse("http://host-1/")));
CHECK(url.host("host-2"));

CHECK(url.host() == "host-2");
Expand Down Expand Up @@ -286,7 +286,7 @@ TEST_CASE("url::is_valid()") {
CHECK_FALSE(url.is_valid());

// parse valid URL
CHECK(url.parse("wss://host:88/path", nullptr) == upa::validation_errc::ok);
CHECK(url.parse("wss://host:88/path") == upa::validation_errc::ok);
CHECK(url.href() == "wss://host:88/path");
CHECK(url.is_valid());

Expand All @@ -296,7 +296,7 @@ TEST_CASE("url::is_valid()") {
CHECK(url.is_valid());

// url::parse must reset VALID_FLAG on failure
CHECK(url.parse("http://h:8a/p", nullptr) == upa::validation_errc::port_invalid);
CHECK(url.parse("http://h:8a/p") == upa::validation_errc::port_invalid);
CHECK_FALSE(url.is_valid());

// invalid URL must ignore setters (except href)
Expand Down Expand Up @@ -356,29 +356,29 @@ TEST_CASE("Parse URL with invalid base") {
CHECK_FALSE(base.is_valid());

upa::url url;
CHECK(url.parse("https://h/", &base) == upa::validation_errc::invalid_base);
CHECK(url.parse("https://h/", base) == upa::validation_errc::invalid_base);
CHECK_FALSE(url.is_valid());

CHECK(url.parse("http://host/", nullptr) == upa::validation_errc::ok);
CHECK(url.parse("http://host/") == upa::validation_errc::ok);
CHECK(url.is_valid());
CHECK(url.parse("https://h/", &base) == upa::validation_errc::invalid_base);
CHECK(url.parse("https://h/", base) == upa::validation_errc::invalid_base);
CHECK_FALSE(url.is_valid());
}
SUBCASE("Invalid base") {
upa::url base;
CHECK_FALSE(upa::success(base.parse("http://h:65616/p", nullptr)));
CHECK_FALSE(upa::success(base.parse("http://h:65616/p")));
CHECK_FALSE(base.is_valid());

upa::url url;
CHECK(url.parse("https://h/", &base) == upa::validation_errc::invalid_base);
CHECK(url.parse("https://h/", base) == upa::validation_errc::invalid_base);
CHECK_FALSE(url.is_valid());

CHECK(url.parse("/path", &base) == upa::validation_errc::invalid_base);
CHECK(url.parse("/path", base) == upa::validation_errc::invalid_base);
CHECK_FALSE(url.is_valid());

CHECK(url.parse("http://host/", nullptr) == upa::validation_errc::ok);
CHECK(url.parse("http://host/") == upa::validation_errc::ok);
CHECK(url.is_valid());
CHECK(url.parse("https://h/", &base) == upa::validation_errc::invalid_base);
CHECK(url.parse("https://h/", base) == upa::validation_errc::invalid_base);
CHECK_FALSE(url.is_valid());
}
}
Expand Down Expand Up @@ -438,10 +438,10 @@ TEST_CASE("url::has_opaque_path") {
upa::url url{};
CHECK_FALSE(url.has_opaque_path());

url.parse("about:blank", nullptr);
url.parse("about:blank");
CHECK(url.has_opaque_path());

url.parse("non-spec:/path", nullptr);
url.parse("non-spec:/path");
CHECK_FALSE(url.has_opaque_path());
}

Expand All @@ -453,7 +453,7 @@ TEST_CASE("url::is_empty and url::is_null") {
CHECK(url.is_empty(upa::url::SCHEME));
CHECK(url.is_null(upa::url::HOST));

CHECK(upa::success(url.parse("http://example.org/", nullptr)));
CHECK(upa::success(url.parse("http://example.org/")));
CHECK_FALSE(url.is_empty(upa::url::SCHEME));
CHECK_FALSE(url.is_null(upa::url::HOST));
}
Expand Down Expand Up @@ -534,14 +534,14 @@ TEST_CASE("Valid UTF-8 in hostname") {
static const char szUrl[] = { 'h', 't', 't', 'p', ':', '/', '/', char(0xC4), char(0x84), '/', '\0' }; // valid

upa::url url;
REQUIRE(upa::success(url.parse(szUrl, nullptr)));
REQUIRE(upa::success(url.parse(szUrl)));
CHECK(url.hostname() == "xn--2da");
}
TEST_CASE("Valid percent encoded utf-8 in hostname") {
static const char szUrl[] = { 'h', 't', 't', 'p', ':', '/', '/', '%', 'C', '4', '%', '8', '4', '/', '\0' }; // valid

upa::url url;
REQUIRE(upa::success(url.parse(szUrl, nullptr)));
REQUIRE(upa::success(url.parse(szUrl)));
CHECK(url.hostname() == "xn--2da");
}
TEST_CASE("Invalid utf-8 in hostname") {
Expand All @@ -558,7 +558,7 @@ TEST_CASE("Valid UTF-16 in hostname") {
static const char16_t szUrl[] = { 'h', 't', 't', 'p', ':', '/', '/', char16_t(0xD800), char16_t(0xDC00), '/', '\0' };

upa::url url;
REQUIRE(upa::success(url.parse(szUrl, nullptr)));
REQUIRE(upa::success(url.parse(szUrl)));
CHECK(url.hostname() == "xn--2n7c");
}
TEST_CASE("Invalid UTF-16 in hostname") {
Expand All @@ -575,7 +575,7 @@ TEST_CASE("Valid UTF-32 in hostname") {
static const char32_t szUrl[] = { 'h', 't', 't', 'p', ':', '/', '/', char32_t(0x10000u), '/', '\0' };

upa::url url;
REQUIRE(upa::success(url.parse(szUrl, nullptr)));
REQUIRE(upa::success(url.parse(szUrl)));
CHECK(url.hostname() == "xn--2n7c");
}
TEST_CASE("Invalid UTF-32 in hostname") {
Expand Down
12 changes: 6 additions & 6 deletions test/wpt-url.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ void test_parser(DataDrivenTest& ddt, ParserObj& obj)
if (!base.empty()) {
upa::url url_base;
parse_success =
upa::success(url_base.parse(base, nullptr)) &&
upa::success(url.parse(input, &url_base));
upa::success(url_base.parse(base)) &&
upa::success(url.parse(input, url_base));
} else {
parse_success = upa::success(url.parse(input, nullptr));
parse_success = upa::success(url.parse(input));
}

// check "failure"
Expand Down Expand Up @@ -137,7 +137,7 @@ void test_parser(DataDrivenTest& ddt, ParserObj& obj)
// If a URL fails to parse with any valid base, it must also fail to parse with no base,
// i.e. when used as a base URL itself.
if (obj.failure && !base.empty()) {
parse_success = upa::success(url.parse(input, nullptr));
parse_success = upa::success(url.parse(input));
// check "failure"
tc.assert_equal(obj.failure, !parse_success, "parse failure WITH NO BASE");
}
Expand Down Expand Up @@ -166,7 +166,7 @@ void test_host_parser(DataDrivenTest& ddt, ParserObj& obj)
const std::string input_url(make_url(input));

upa::url url;
const bool parse_success = upa::success(url.parse(input_url, nullptr));
const bool parse_success = upa::success(url.parse(input_url));

// check "failure"
tc.assert_equal(obj.failure, !parse_success, "parse failure");
Expand Down Expand Up @@ -241,7 +241,7 @@ void test_idna_v2(DataDrivenTest& ddt, ParserObj& obj)
const std::string input_url(make_url(encodeHostEndingCodePoints(input)));

upa::url url;
const bool parse_success = upa::success(url.parse(input_url, nullptr));
const bool parse_success = upa::success(url.parse(input_url));

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

0 comments on commit bf847aa

Please sign in to comment.