Skip to content

Commit

Permalink
[C++ builds] Add C++ ServerSessionMiddleware cookie parsing test
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Nienaber authored and stevelorddremio committed Jan 17, 2024
1 parent 8ea8d05 commit 788bb30
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
15 changes: 13 additions & 2 deletions cpp/src/arrow/flight/sql/server_session_middleware.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,19 @@ ServerSessionMiddlewareFactory::ParseCookieString(const std::string_view& s) {
// The cookie header is somewhat malformed; ignore the key and continue parsing
continue;
}
result.emplace_back(tok.substr(0, val_pos),
tok.substr(val_pos + pair_sep.length(), std::string::npos));
const std::string_view cookie_name = tok.substr(0, val_pos);
std::string_view cookie_value = tok.substr(val_pos + pair_sep.length(), std::string::npos);
if (cookie_name.empty()) {
continue;
}
// Strip doublequotes
if (cookie_value.length() >= 2 &&
cookie_value.front() == '"' &&
cookie_value.back() == '"') {
cookie_value.remove_prefix(1);
cookie_value.remove_suffix(1);
}
result.emplace_back(cookie_name, cookie_value);
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ServerSessionMiddlewareFactory : public ServerMiddlewareFactory {
std::shared_mutex session_store_lock_;
std::function<std::string()> id_generator_;

std::vector<std::pair<std::string, std::string>> ParseCookieString(
static std::vector<std::pair<std::string, std::string>> ParseCookieString(
const std::string_view& s);

public:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
// ----------------------------------------------------------------------
// ServerSessionMiddleware{,Factory} tests not involing a client/server instance

#include <stdio.h>

#include <gtest/gtest.h>

#include <arrow/flight/sql/server_session_middleware_factory.h>
Expand All @@ -33,8 +31,13 @@ class ServerSessionMiddlewareFactoryPrivate : public ServerSessionMiddlewareFact
using ServerSessionMiddlewareFactory::ParseCookieString;
};

TEST(ServerSessionMiddleware, Dummy) {
ASSERT_EQ(1, 0);
TEST(ServerSessionMiddleware, ParseCookieString) {
std::vector<std::pair<std::string, std::string>> r1 =
ServerSessionMiddlewareFactoryPrivate::ParseCookieString(
"k1=v1; k2=\"v2\"; kempty=; k3=v3");
std::vector<std::pair<std::string, std::string>> e1 =
{{"k1", "v1"}, {"k2", "v2"}, {"kempty", ""}, {"k3", "v3"}};
ASSERT_EQ(e1, r1);
}

} // namespace sql
Expand Down

0 comments on commit 788bb30

Please sign in to comment.