Skip to content

Commit

Permalink
Update test to properly evaluate websocket redirections; added one mo…
Browse files Browse the repository at this point in the history
…re test
  • Loading branch information
Michael Wilkerson-Barker committed May 16, 2023
1 parent 32616bf commit 919604a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 21 deletions.
3 changes: 3 additions & 0 deletions test/object-store/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,12 @@ if(REALM_TEST_LOGGING)
)

if(REALM_TEST_LOGGING_LEVEL)
message(STATUS "Test logging level: ${REALM_TEST_LOGGING_LEVEL}")
target_compile_definitions(ObjectStoreTests PRIVATE
TEST_LOGGING_LEVEL=${REALM_TEST_LOGGING_LEVEL}
)
else()
message(STATUS "Test logging enabled")
endif()
endif()

Expand Down
84 changes: 63 additions & 21 deletions test/object-store/sync/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2213,7 +2213,7 @@ TEST_CASE("app: sync integration", "[sync][app]") {
logger->trace("Received request[%1]: %2", request_count, request.url);
if (request_count == 0) {
// First request should be to location
REQUIRE(request.url.find_first_of("/location") != std::string::npos);
REQUIRE(request.url.find("/location") != std::string::npos);
if (request.url.find("https://") != std::string::npos) {
redirect_scheme = "https://";
}
Expand Down Expand Up @@ -2361,10 +2361,10 @@ TEST_CASE("app: sync integration", "[sync][app]") {
std::string websocket_url = "ws://some-websocket:9090";
std::string original_url;
redir_transport->request_hook = [&](const Request& request) {
logger->trace("Received request[%1]: %2", request_count, request.url);
logger->trace("request.url (%1): %2", request_count, request.url);
if (request_count == 0) {
// First request should be to location
REQUIRE(request.url.find_first_of("/location") != std::string::npos);
REQUIRE(request.url.find("/location") != std::string::npos);
if (request.url.find("https://") != std::string::npos) {
original_scheme = "https://";
}
Expand All @@ -2380,7 +2380,6 @@ TEST_CASE("app: sync integration", "[sync][app]") {
logger->trace("original_url (%1): %2", request_count, original_url);
}
else if (request_count == 1) {
logger->trace("request.url (%1): %2", request_count, request.url);
REQUIRE(!request.redirect_count);
redir_transport->simulated_response = {
308,
Expand All @@ -2389,7 +2388,6 @@ TEST_CASE("app: sync integration", "[sync][app]") {
"Some body data"};
}
else if (request_count == 2) {
logger->trace("request.url (%1): %2", request_count, request.url);
REQUIRE(request.url.find("http://somehost:9090") != std::string::npos);
REQUIRE(request.url.find("location") != std::string::npos);
// app hostname will be updated via the metadata info
Expand All @@ -2402,7 +2400,6 @@ TEST_CASE("app: sync integration", "[sync][app]") {
original_url, websocket_url)};
}
else {
logger->trace("request.url (%1): %2", request_count, request.url);
REQUIRE(request.url.find(original_url) != std::string::npos);
redir_transport->simulated_response.reset();
}
Expand Down Expand Up @@ -2507,19 +2504,19 @@ TEST_CASE("app: sync integration", "[sync][app]") {
};
int request_count = 0;
redir_transport->request_hook = [&](const Request& request) {
logger->trace("request.url (%1): %2", request_count, request.url);
if (request_count++ == 0) {
logger->trace("request.url (%1): %2", request_count, request.url);
// First request should be to location
REQUIRE(request.url.find_first_of("/location") != std::string::npos);
// First request should be a location request against the original URL
REQUIRE(request.url.find(original_host) != std::string::npos);
REQUIRE(request.url.find("/location") != std::string::npos);
REQUIRE(request.redirect_count == 0);
redir_transport->simulated_response = {
static_cast<int>(sync::HTTPStatus::PermanentRedirect),
0,
{{"Location", redirect_url}, {"Content-Type", "application/json"}},
"Some body data"};
}
else if (request.url.find("location") != std::string::npos) {
logger->trace("request.url (%1): %2", request_count, request.url);
else if (request.url.find("/location") != std::string::npos) {
redir_transport->simulated_response = {
static_cast<int>(sync::HTTPStatus::Ok),
0,
Expand All @@ -2530,7 +2527,6 @@ TEST_CASE("app: sync integration", "[sync][app]") {
redirect_host, redirect_scheme, websocket_scheme)};
}
else {
logger->trace("request.url (%1): %2", request_count, request.url);
redir_transport->simulated_response.reset();
}
};
Expand Down Expand Up @@ -2561,19 +2557,18 @@ TEST_CASE("app: sync integration", "[sync][app]") {
};
int request_count = 0;
redir_transport->request_hook = [&](const Request& request) {
logger->trace("request.url (%1): %2", request_count, request.url);
if (request_count++ == 0) {
logger->trace("request.url (%1): %2", request_count, request.url);
// First request should be to location
REQUIRE(request.url.find_first_of("/location") != std::string::npos);
// First request should be a location request against the original URL
REQUIRE(request.url.find(original_host) != std::string::npos);
REQUIRE(request.redirect_count == 0);
redir_transport->simulated_response = {
static_cast<int>(sync::HTTPStatus::MovedPermanently),
0,
{{"Location", redirect_url}, {"Content-Type", "application/json"}},
"Some body data"};
}
else if (request.url.find("location") != std::string::npos) {
logger->trace("request.url (%1): %2", request_count, request.url);
else if (request.url.find("/location") != std::string::npos) {
redir_transport->simulated_response = {
static_cast<int>(sync::HTTPStatus::Ok),
0,
Expand All @@ -2584,14 +2579,12 @@ TEST_CASE("app: sync integration", "[sync][app]") {
redirect_host, redirect_scheme, websocket_scheme)};
}
else if (request.url.find("auth/session") != std::string::npos) {
logger->trace("request.url (%1): %2", request_count, request.url);
redir_transport->simulated_response = {static_cast<int>(sync::HTTPStatus::Unauthorized),
0,
{{"Content-Type", "application/json"}},
""};
}
else {
logger->trace("request.url (%1): %2", request_count, request.url);
redir_transport->simulated_response.reset();
}
};
Expand All @@ -2600,9 +2593,58 @@ TEST_CASE("app: sync integration", "[sync][app]") {
sync_session->resume();
REQUIRE(wait_for_download(*r));
std::unique_lock lk(logout_mutex);
REQUIRE(logout_cv.wait_for(lk, std::chrono::seconds(15), [&]() {
auto result = logout_cv.wait_for(lk, std::chrono::seconds(15), [&]() {
return logged_out;
}));
});
REQUIRE(result);
REQUIRE(!user1->is_logged_in());
}
SECTION("Too many websocket redirects logs out user") {
auto sync_manager = test_session.app()->sync_manager();
auto sync_session = sync_manager->get_existing_session(r->config().path);
sync_session->pause();

int connect_count = 0;
redir_provider->websocket_connect_func = [&connect_count](int& status_code, std::string& body) {
if (connect_count++ > 0)
return false;

status_code = static_cast<int>(sync::HTTPStatus::MovedPermanently);
body = "";
return true;
};
int request_count = 0;
int max_http_redirects = 20; // from app.cpp in object-store
redir_transport->request_hook = [&](const Request& request) {
logger->trace("request.url (%1): %2", request_count, request.url);
if (request_count++ == 0) {
// First request should be a location request against the original URL
REQUIRE(request.url.find(original_host) != std::string::npos);
REQUIRE(request.redirect_count == 0);
}
if (request.url.find("/location") != std::string::npos) {
// Keep returning the redirected response
REQUIRE(request.redirect_count < 20);
redir_transport->simulated_response = {
static_cast<int>(sync::HTTPStatus::MovedPermanently),
0,
{{"Location", redirect_url}, {"Content-Type", "application/json"}},
"Some body data"};
}
else {
// should not get any other types of requests during the test - the log out is local
REQUIRE(false);
}
};

SyncManager::OnlyForTesting::voluntary_disconnect_all_connections(*sync_manager);
sync_session->resume();
REQUIRE(wait_for_download(*r));
std::unique_lock lk(logout_mutex);
auto result = logout_cv.wait_for(lk, std::chrono::seconds(15), [&]() {
return logged_out;
});
REQUIRE(result);
REQUIRE(!user1->is_logged_in());
}
}
Expand Down

0 comments on commit 919604a

Please sign in to comment.