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 all commits
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
1 change: 1 addition & 0 deletions test/uri/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ add_executable(sourcemeta_jsontoolkit_uri_unit
uri_is_tag_test.cc
uri_recompose_test.cc
uri_recompose_without_fragment_test.cc
uri_normalize_test.cc
uri_canonicalize_test.cc
uri_resolve_from_test.cc
uri_relative_to_test.cc
Expand Down
90 changes: 90 additions & 0 deletions test/uri/uri_normalize_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include <gtest/gtest.h>
#include <sourcemeta/jsontoolkit/uri.h>

/*
* RFC 3986 normalization tests
*
* We test the normalization through the .recompose() API, as the
* URI is normalized during construction.
*/

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

TEST(URI_normalize, rfc3986_1) {
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