Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion include/ada/url_search_params-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ inline std::string url_search_params::to_string() const {
return out;
}

inline std::string url_search_params::to_raw_string() const {
inline std::string url_search_params::to_unsafe_string() const {
std::string out{};
for (const auto &[key, value] : params) {
if (!out.empty()) {
Expand Down
5 changes: 3 additions & 2 deletions include/ada/url_search_params.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,10 @@ struct url_search_params {
/**
* Returns a serialized query string without normalizing the key-value pairs.
* Unlike to_string(), this method does not apply additional transformations
* to the percent-encoded output.
* to the percent-encoded output. The result is not standard compliant and
* is therefore unsafe.
*/
inline std::string to_raw_string() const;
inline std::string to_unsafe_string() const;

/**
* Returns a simple JS-style iterator over all of the keys in this
Expand Down
2 changes: 1 addition & 1 deletion include/ada_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ void ada_free_search_params(ada_url_search_params result);
size_t ada_search_params_size(ada_url_search_params result);
void ada_search_params_sort(ada_url_search_params result);
ada_owned_string ada_search_params_to_string(ada_url_search_params result);
ada_owned_string ada_search_params_to_raw_string(ada_url_search_params result);
ada_owned_string ada_search_params_to_unsafe_string(ada_url_search_params result);

void ada_search_params_append(ada_url_search_params result, const char* key,
size_t key_length, const char* value,
Expand Down
4 changes: 2 additions & 2 deletions src/ada_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,11 +487,11 @@ ada_owned_string ada_search_params_to_string(ada_url_search_params result) {
return owned;
}

ada_owned_string ada_search_params_to_raw_string(ada_url_search_params result) {
ada_owned_string ada_search_params_to_unsafe_string(ada_url_search_params result) {
ada::result<ada::url_search_params>& r =
*(ada::result<ada::url_search_params>*)result;
if (!r) return ada_owned_string{nullptr, 0};
std::string out = r->to_raw_string();
std::string out = r->to_unsafe_string();
ada_owned_string owned{};
owned.length = out.size();
owned.data = new char[owned.length];
Expand Down
12 changes: 6 additions & 6 deletions tests/ada_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ TEST(ada_c, ada_url_search_params) {
SUCCEED();
}

TEST(ada_c, ada_search_params_to_raw_string) {
TEST(ada_c, ada_search_params_to_unsafe_string) {
std::string input("a=b c&d=e+f");
auto out = ada_parse_search_params(input.c_str(), input.length());

Expand All @@ -367,8 +367,8 @@ TEST(ada_c, ada_search_params_to_raw_string) {
ASSERT_EQ(convert_string(str), "a=b+c&d=e+f");
ada_free_owned_string(str);

// to_raw_string outputs raw key/value without any encoding
ada_owned_string raw_str = ada_search_params_to_raw_string(out);
// to_unsafe_string outputs raw key/value without any encoding
ada_owned_string raw_str = ada_search_params_to_unsafe_string(out);
ASSERT_EQ(convert_string(raw_str), "a=b c&d=e f");
ada_free_owned_string(raw_str);

Expand All @@ -377,7 +377,7 @@ TEST(ada_c, ada_search_params_to_raw_string) {
SUCCEED();
}

TEST(ada_c, ada_search_params_to_raw_string_remove) {
TEST(ada_c, ada_search_params_to_unsafe_string_remove) {
std::string input("a=%20&b=remove&c=2");
auto params = ada_parse_search_params(input.c_str(), input.length());

Expand All @@ -389,8 +389,8 @@ TEST(ada_c, ada_search_params_to_raw_string_remove) {
ASSERT_EQ(convert_string(str), "a=+&c=2");
ada_free_owned_string(str);

// to_raw_string outputs raw key/value without any encoding
ada_owned_string raw_str = ada_search_params_to_raw_string(params);
// to_unsafe_string outputs raw key/value without any encoding
ada_owned_string raw_str = ada_search_params_to_unsafe_string(params);
ASSERT_EQ(convert_string(raw_str), "a= &c=2");
ada_free_owned_string(raw_str);

Expand Down
24 changes: 12 additions & 12 deletions tests/url_search_params.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,47 +449,47 @@ TEST(url_search_params, sort_unicode_code_units_edge_case) {
SUCCEED();
}

TEST(url_search_params, to_raw_string_no_normalization) {
TEST(url_search_params, to_unsafe_string_no_normalization) {
auto params = ada::url_search_params();
params.append("a", "b c");
// to_string normalizes space to +
ASSERT_EQ(params.to_string(), "a=b+c");
// to_raw_string outputs raw key/value without any encoding
ASSERT_EQ(params.to_raw_string(), "a=b c");
// to_unsafe_string outputs raw key/value without any encoding
ASSERT_EQ(params.to_unsafe_string(), "a=b c");
SUCCEED();
}

TEST(url_search_params, to_raw_string_with_special_chars) {
TEST(url_search_params, to_unsafe_string_with_special_chars) {
auto params = ada::url_search_params();
params.append("key1", "value with spaces");
params.append("key2", "another value");
// to_string normalizes spaces to +
ASSERT_EQ(params.to_string(), "key1=value+with+spaces&key2=another+value");
// to_raw_string outputs raw key/value without any encoding
ASSERT_EQ(params.to_raw_string(),
// to_unsafe_string outputs raw key/value without any encoding
ASSERT_EQ(params.to_unsafe_string(),
"key1=value with spaces&key2=another value");
SUCCEED();
}

TEST(url_search_params, to_raw_string_with_accents) {
TEST(url_search_params, to_unsafe_string_with_accents) {
auto params = ada::url_search_params();
params.append("key1", "\u00E9t\u00E9");
params.append("key2", "C\u00E9line Dion++");
// to_string percent-encodes and normalizes spaces to +
ASSERT_EQ(params.to_string(),
"key1=%C3%A9t%C3%A9&key2=C%C3%A9line+Dion%2B%2B");
// to_raw_string outputs raw key/value without any encoding
ASSERT_EQ(params.to_raw_string(),
// to_unsafe_string outputs raw key/value without any encoding
ASSERT_EQ(params.to_unsafe_string(),
"key1=\u00E9t\u00E9&key2=C\u00E9line Dion++");
SUCCEED();
}

TEST(url_search_params, to_raw_string_empty_values) {
TEST(url_search_params, to_unsafe_string_empty_values) {
auto params = ada::url_search_params();
params.append("a", "");
params.append("", "b");
params.append("", "");
ASSERT_EQ(params.to_raw_string(), "a=&=b&=");
ASSERT_EQ(params.to_unsafe_string(), "a=&=b&=");
ASSERT_EQ(params.to_string(), "a=&=b&=");
SUCCEED();
}
Expand All @@ -500,6 +500,6 @@ TEST(url_search_params, with_ampersands) {
params.append("b", "?");
params.append("b", "+");
ASSERT_EQ(params.to_string(), "a=%26&b=%3F&b=%2B");
ASSERT_EQ(params.to_raw_string(), "a=&&b=?&b=+");
ASSERT_EQ(params.to_unsafe_string(), "a=&&b=?&b=+");
SUCCEED();
}
Loading