Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(uri): add normalization tests according to RFC #958

Merged
merged 3 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/uri/uri.cc
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,11 @@ auto URI::recompose_without_fragment() const -> std::optional<std::string> {
}
}

const auto user_info{this->userinfo()};
if (user_info.has_value()) {
result << user_info.value() << "@";
}
Comment on lines +319 to +322
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One bug spotted! 🎉


// Host
const auto result_host{this->host()};
if (result_host.has_value()) {
Expand Down
85 changes: 85 additions & 0 deletions test/uri/uri_recompose_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,88 @@ TEST(URI_recompose, empty_fragment) {
const sourcemeta::jsontoolkit::URI uri{"#"};
EXPECT_EQ(uri.recompose(), "#");
}

/*
* RFC 3986 normalization tests
*/

// Inspired from https://cr.openjdk.org/~dfuchs/writeups/updating-uri/

TEST(URI_normalize, rfc3986_1) {
tony-go marked this conversation as resolved.
Show resolved Hide resolved
const sourcemeta::jsontoolkit::URI uri{"s://h/a/../../b"};
EXPECT_EQ(uri.recompose(), "s://h/b");
}

// Inspired from
// https://github.com/uriparser/uriparser/blob/master/test/test.cpp#L1438

TEST(URI_normalize, rfc3986_2) {
const sourcemeta::jsontoolkit::URI uri{"eXAMPLE://a/./b/../b/%63/%7bfoo%7d"};
EXPECT_EQ(uri.recompose(), "example://a/b/c/%7Bfoo%7D");
}

TEST(URI_normalize, percent_encoding) {
const sourcemeta::jsontoolkit::URI uri{"http://examp%4Ce.com/"};
EXPECT_EQ(uri.recompose(), "http://example.com/");
}

TEST(URI_normalize, dot_segments) {
const sourcemeta::jsontoolkit::URI uri{"http://example.com/a/b/%2E%2E/"};
EXPECT_EQ(uri.recompose(), "http://example.com/a/");
}

TEST(URI_normalize, case_normalization) {
const sourcemeta::jsontoolkit::URI uri{"http://user:[email protected]:123"};
EXPECT_EQ(uri.recompose(), "http://user:[email protected]:123");
}

TEST(URI_normalize, complex_case) {
const sourcemeta::jsontoolkit::URI uri{
"HTTP://a:b@HOST:123/./1/2/../%41?abc#def"};
EXPECT_EQ(uri.recompose(), "http://a:b@host:123/1/A?abc#def");
}

TEST(URI_normalize, relative_path_1) {
const sourcemeta::jsontoolkit::URI uri{"../../abc"};
EXPECT_EQ(uri.recompose(), "../../abc");
}

TEST(URI_normalize, relative_path_2) {
const sourcemeta::jsontoolkit::URI uri{"../../abc/.."};
EXPECT_EQ(uri.recompose(), "../../");
}

TEST(URI_normalize, relative_path_3) {
const sourcemeta::jsontoolkit::URI uri{"../../abc/../def"};
EXPECT_EQ(uri.recompose(), "../../def");
}

TEST(URI_normalize, relative_path_4) {
const sourcemeta::jsontoolkit::URI uri{"abc/.."};
EXPECT_EQ(uri.recompose(), "");
}

TEST(URI_normalize, relative_path_5) {
const sourcemeta::jsontoolkit::URI uri{"abc/../"};
EXPECT_EQ(uri.recompose(), "");
}

TEST(URI_normalize, relative_path_6) {
const sourcemeta::jsontoolkit::URI uri{"../../abc/./def"};
EXPECT_EQ(uri.recompose(), "../../abc/def");
}

TEST(URI_normalize, relative_path_7) {
const sourcemeta::jsontoolkit::URI uri{"./def"};
EXPECT_EQ(uri.recompose(), "def");
}

TEST(URI_normalize, relative_path_8) {
const sourcemeta::jsontoolkit::URI uri{"def/."};
EXPECT_EQ(uri.recompose(), "def/");
}

TEST(URI_normalize, relative_path_9) {
const sourcemeta::jsontoolkit::URI uri{"./abc:def"};
EXPECT_EQ(uri.recompose(), "./abc:def");
}
Loading