2022-05-08 05:47:18 +00:00
|
|
|
#include <drogon/Cookie.h>
|
|
|
|
#include <drogon/drogon_test.h>
|
|
|
|
#include <drogon/HttpAppFramework.h>
|
|
|
|
#include <drogon/HttpClient.h>
|
|
|
|
#include <drogon/HttpRequest.h>
|
|
|
|
#include <drogon/HttpResponse.h>
|
|
|
|
#include <drogon/HttpTypes.h>
|
|
|
|
|
|
|
|
using namespace drogon;
|
|
|
|
|
|
|
|
struct CookieSameSiteSequence
|
|
|
|
{
|
|
|
|
CookieSameSiteSequence()
|
|
|
|
{
|
|
|
|
i = valid_sameSite_values.begin();
|
|
|
|
sessionCookie.setKey("JSESSIONID");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> valid_sameSite_values{"Null",
|
|
|
|
"Lax",
|
|
|
|
"None",
|
|
|
|
"Strict"};
|
|
|
|
|
|
|
|
std::vector<std::string>::const_iterator i;
|
|
|
|
Cookie sessionCookie;
|
|
|
|
};
|
|
|
|
|
|
|
|
DROGON_TEST(CookieSameSite)
|
|
|
|
{
|
|
|
|
auto client =
|
2022-07-31 04:56:49 +00:00
|
|
|
HttpClient::newHttpClient("https://127.0.0.1:8855",
|
2022-05-08 05:47:18 +00:00
|
|
|
HttpAppFramework::instance().getLoop(),
|
|
|
|
false,
|
|
|
|
false);
|
|
|
|
auto req = HttpRequest::newHttpRequest();
|
|
|
|
|
|
|
|
CookieSameSiteSequence seq;
|
|
|
|
|
|
|
|
while (seq.i != seq.valid_sameSite_values.end())
|
|
|
|
{
|
|
|
|
std::promise<void> p1;
|
|
|
|
std::future<void> f1 = p1.get_future();
|
|
|
|
req->setPath(std::string("/CookieSameSiteController/set/") + *seq.i);
|
|
|
|
if (seq.sessionCookie.getValue() != "")
|
|
|
|
{
|
|
|
|
// add session cookie
|
|
|
|
req->addCookie(seq.sessionCookie.getKey(),
|
|
|
|
seq.sessionCookie.getValue());
|
|
|
|
} // endif
|
|
|
|
|
|
|
|
client->sendRequest(
|
|
|
|
req,
|
|
|
|
[TEST_CTX, &seq, &p1](ReqResult res, const HttpResponsePtr &resp) {
|
|
|
|
REQUIRE(res == ReqResult::Ok);
|
|
|
|
REQUIRE(resp != nullptr);
|
|
|
|
|
|
|
|
CHECK(resp->getStatusCode() == HttpStatusCode::k200OK);
|
|
|
|
CHECK(resp->contentType() == CT_APPLICATION_JSON);
|
|
|
|
|
|
|
|
auto json = resp->getJsonObject();
|
2022-05-16 03:18:10 +00:00
|
|
|
auto cookie = resp->getCookie("JSESSIONID");
|
2022-05-08 05:47:18 +00:00
|
|
|
|
2022-05-16 03:18:10 +00:00
|
|
|
LOG_INFO << "Client: cookie-value == " << cookie.value()
|
|
|
|
<< ", requested value == " << (*seq.i)
|
|
|
|
<< ", new value == "
|
|
|
|
<< (*json)["new value"].asString()
|
|
|
|
<< ", received value == "
|
|
|
|
<< Cookie::convertSameSite2String(
|
|
|
|
cookie.getSameSite());
|
2022-05-08 05:47:18 +00:00
|
|
|
seq.sessionCookie = resp->getCookie("JSESSIONID");
|
2022-05-16 03:18:10 +00:00
|
|
|
CHECK(*seq.i ==
|
|
|
|
Cookie::convertSameSite2String(cookie.getSameSite()));
|
2022-05-08 05:47:18 +00:00
|
|
|
|
|
|
|
p1.set_value();
|
|
|
|
});
|
|
|
|
f1.get();
|
|
|
|
seq.i++;
|
|
|
|
}
|
2022-07-26 04:16:48 +00:00
|
|
|
}
|