24#include <catch2/catch.hpp>
33TEST_CASE(
"nawa::Session class",
"[unit][session]") {
37 SECTION(
"Basic session handling") {
41 auto& session = connection.
session();
43 CHECK_NOTHROW(session.set(
"testKey",
"testVal"));
44 CHECK(any_cast<string>(session[
"testKey"]) ==
"testVal");
45 sessionId = session.getID();
51 auto& session = connection.
session();
53 REQUIRE(session.getID() == sessionId);
54 CHECK(any_cast<string>(session[
"testKey"]) ==
"testVal");
58 SECTION(
"Basic session handling without cookies") {
62 auto& session = connection.
session();
63 sessionId = session.
start(
"");
64 CHECK_NOTHROW(session.set(
"testKey",
"testVal"));
65 CHECK(any_cast<string>(session[
"testKey"]) ==
"testVal");
69 auto& session = connection.
session();
70 string newSessionId = session.
start(sessionId);
71 REQUIRE(newSessionId == sessionId);
72 REQUIRE(session.getID() == sessionId);
73 CHECK(any_cast<string>(session[
"testKey"]) ==
"testVal");
77 SECTION(
"Session autostart") {
79 connectionInit1.
config.
set({
"session",
"autostart"},
"on");
83 auto& session = connection.
session();
84 CHECK_NOTHROW(session.set(
"testKey",
"testVal"));
85 CHECK(any_cast<string>(session[
"testKey"]) ==
"testVal");
86 sessionId = session.getID();
91 auto& session = connection.
session();
92 REQUIRE(session.getID() == sessionId);
93 CHECK(any_cast<string>(session[
"testKey"]) ==
"testVal");
97 SECTION(
"Attempt to access and modify inactive session") {
99 auto& session = connection.
session();
100 CHECK_THROWS_AS(session.set(
"testKey",
"testVal"),
Exception);
101 CHECK_THROWS_AS(session.unset(
"testKey"),
Exception);
102 CHECK_NOTHROW(session[
"testKey"]);
105 SECTION(
"Invalidation of sessions") {
107 auto& session = connection.
session();
109 CHECK_NOTHROW(session.set(
"testKey",
"testVal"));
110 session.invalidate();
111 CHECK_THROWS_AS(session.set(
"testKey",
"testVal"),
Exception);
114 SECTION(
"Client IP check: same IP") {
116 string clientIPCheckMode = GENERATE(
"lax",
"strict");
117 connectionInit1.
config.
set({
"session",
"validate_ip"}, clientIPCheckMode);
121 auto& session = connection.
session();
123 CHECK_NOTHROW(session.set(
"testKey",
"testVal"));
124 CHECK(any_cast<string>(session[
"testKey"]) ==
"testVal");
125 sessionId = session.getID();
130 auto& session = connection.
session();
132 REQUIRE(session.getID() == sessionId);
133 CHECK(any_cast<string>(session[
"testKey"]) ==
"testVal");
137 SECTION(
"Client IP check: different IP (lax)") {
139 connectionInit1.
config.
set({
"session",
"validate_ip"},
"lax");
143 auto& session = connection.
session();
145 CHECK_NOTHROW(session.set(
"testKey",
"testVal"));
146 CHECK(any_cast<string>(session[
"testKey"]) ==
"testVal");
147 sessionId = session.getID();
154 auto& session = connection.
session();
156 CHECK_FALSE(session.getID() == sessionId);
157 CHECK_FALSE(session.isSet(
"testKey"));
162 auto& session = connection.
session();
164 REQUIRE(session.getID() == sessionId);
165 CHECK(any_cast<string>(session[
"testKey"]) ==
"testVal");
169 SECTION(
"Client IP check: different IP (strict)") {
171 connectionInit1.
config.
set({
"session",
"validate_ip"},
"strict");
175 auto& session = connection.
session();
177 CHECK_NOTHROW(session.set(
"testKey",
"testVal"));
178 CHECK(any_cast<string>(session[
"testKey"]) ==
"testVal");
179 sessionId = session.getID();
186 auto& session = connection.
session();
188 CHECK_FALSE(session.getID() == sessionId);
189 CHECK_FALSE(session.isSet(
"testKey"));
194 auto& session = connection.
session();
196 CHECK_FALSE(session.getID() == sessionId);
197 CHECK_FALSE(session.isSet(
"testKey"));
Container used by request handlers to initiate the nawa::Connection object.
Response object to be passed back to NAWA and accessor to the request.
Exception class that can be used by apps to catch errors resulting from nawa function calls.
Class for managing sessions and getting and setting connection-independent session data.
void set(std::pair< std::string, std::string > key, std::string value)
nawa::Session & session() noexcept
std::string start(std::string sessionId, std::optional< unsigned long > keepalive=std::nullopt)
std::unordered_map< std::string, std::string > environment
std::unordered_multimap< std::string, std::string > cookieVars
RequestInitContainer requestInit
TEST_CASE("nawa::Session class", "[unit][session]")