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 more normalize tests #981

Merged
merged 2 commits into from
Aug 16, 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
6 changes: 6 additions & 0 deletions src/uri/uri.cc
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,13 @@ auto URI::host() const -> std::optional<std::string_view> {
auto URI::port() const -> std::optional<std::uint32_t> { return this->port_; }

auto URI::path() const -> std::optional<std::string> {
// NOTE: This is a workaround for the fact that `uriparser` does not
// parse /.. as a segment, then we store nothing in the path_ field.
// By that we can't add the initial slash to the URI.
if (!this->path_.has_value()) {
if (this->data == "/..") {
return "/";
}
tony-go marked this conversation as resolved.
Show resolved Hide resolved
return std::nullopt;
}

Expand Down
43 changes: 43 additions & 0 deletions test/uri/uri_normalize_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,46 @@ TEST(URI_normalize, relative_path_9) {
const sourcemeta::jsontoolkit::URI uri{"./abc:def"};
EXPECT_EQ(uri.recompose(), "./abc:def");
}

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

TEST(URI_normalize, path_1) {
const sourcemeta::jsontoolkit::URI uri{"http://a/b/c/../../.."};
EXPECT_EQ(uri.recompose(), "http://a/");
}

TEST(URI_normalize, path_2) {
const sourcemeta::jsontoolkit::URI uri{"http://a/b/../c/../.."};
EXPECT_EQ(uri.recompose(), "http://a/");
}

TEST(URI_normalize, path_3) {
const sourcemeta::jsontoolkit::URI uri{"http://a/.."};
EXPECT_EQ(uri.recompose(), "http://a/");
}

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

TEST(URI_normalize, path_5) {
const sourcemeta::jsontoolkit::URI uri{"http://a/..///"};
EXPECT_EQ(uri.recompose(), "http://a///");
}

TEST(URI_normalize, path_6) {
const sourcemeta::jsontoolkit::URI uri{"http://a/..///.."};
EXPECT_EQ(uri.recompose(), "http://a//");
}

TEST(URI_normalize, path_7) {
const sourcemeta::jsontoolkit::URI uri{"a/b/c/../../.."};
EXPECT_EQ(uri.recompose(), "");
}

TEST(URI_normalize, path_8) {
const sourcemeta::jsontoolkit::URI uri{"a/b/../../c/.."};
EXPECT_EQ(uri.recompose(), "");
}
Loading