Menu

[r2296]: / framework / trunk / tests / session_interface_test.cpp  Maximize  Restore  History

Download this file

191 lines (177 with data), 4.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
//
// See accompanying file COPYING.TXT file for licensing details.
//
///////////////////////////////////////////////////////////////////////////////
#include <cppcms/service.h>
#include <cppcms/application.h>
#include <cppcms/applications_pool.h>
#include <cppcms/http_request.h>
#include <cppcms/http_response.h>
#include <cppcms/http_context.h>
#include <cppcms/http_cookie.h>
#include <cppcms/session_interface.h>
#include <cppcms/serialization.h>
#include <cppcms/json.h>
#include <iostream>
#include "client.h"
#include "test.h"
struct mydata : public cppcms::serializable {
int x;
int y;
void serialize(cppcms::archive &a)
{
a & x & y;
}
};
void test_set(cppcms::session_interface &interface,std::string const &values)
{
std::set<std::string> keys = interface.key_set();
std::string res;
for(std::set<std::string>::const_iterator p=keys.begin();p!=keys.end();++p) {
if(res.empty())
res = *p;
else
res += "|" + *p;
}
if(res!=values)
throw std::runtime_error("Key set is invalid:(" + res +")!=(" + values +")");
}
class adapter : public cppcms::session_interface_cookie_adapter {
public:
void set_cookie(cppcms::http::cookie const &updated_cookie)
{
if(updated_cookie.name() == ctx_->session().session_cookie_name()) {
ctx_->response().out() << updated_cookie.value();
}
}
std::string get_session_cookie(std::string const &/*name*/)
{
return ctx_->request().get("sid");
}
private:
cppcms::http::context *ctx_;
};
class unit_test : public cppcms::application {
public:
unit_test(cppcms::service &s) : cppcms::application(s)
{
}
virtual void main(std::string u)
{
if(u=="/new") {
session().set("x",1);
}
else if(u=="/update") {
session().set("x",session().get<int>("x") + 1);
}
else if(u=="/clear") {
session().clear();
response().out() << "clear";
}
else if(u=="/new_short") {
session().set("x",1);
session().age(1);
}
else if(u=="/is_expired") {
if(session().is_set("x"))
response().out() << "not expired";
else
response().out() << "expired";
}
else if(u=="/expose") {
session().expose("x");
}
else if(u=="/unexpose") {
session().hide("x");
}
else if(u=="/fixed") {
session().expiration(cppcms::session_interface::fixed);
}
else if(u=="/renew") {
session().expiration(cppcms::session_interface::renew);
}
else if(u=="/browser") {
session().expiration(cppcms::session_interface::browser);
}
else if(u=="/reset") {
session().reset_session();
}
else if(u=="/on_server") {
session().on_server(true);
}
else if(u=="/not_on_server") {
session().on_server(false);
}
else if(u=="/huge") {
std::string tmp="x";
for(int i=0;i<10;i++)
tmp=tmp+tmp;
session().set("y",tmp);
}
else if(u=="/small") {
session().erase("y");
session().set("x","1");
}
else if(u=="/info") {
bool is_set_x = session().is_set("x");
bool is_exposed_x = session().is_exposed("x");
int age = session().age();
int expiration = session().expiration();
bool on_server = session().on_server();
response().out()
<< "is_set_x=" << is_set_x << "\n"
<< "is_exposed_x=" << is_exposed_x << "\n"
<< "age="<<age<<"\n"
<< "expiration=" << expiration << "\n"
<< "on_server=" << on_server ;
}
else if(u=="/api") {
try {
// Fix Me Later
test_set(session(),"");
session().set("x","10");
test_set(session(),"x");
TEST(session().get<std::string>("x")=="10");
TEST(session().get<int>("x")==10);
TEST(session().get<double>("x")==10.0);
TEST(session().get("z","default")=="default");
TEST(session().get("x","default")=="10");
TEST(session()["x"]=="10");
TEST(session()["z"]=="");
test_set(session(),"x|z");
session()["z"]="test";
TEST(session()["z"]=="test");
mydata a,b;
a.x=10;
a.y=20;
session().store_data("tmp",a);
session().fetch_data("tmp",b);
test_set(session(),"tmp|x|z");
TEST(b.x==10);
TEST(b.y==20);
response().out() << "ok";
}
catch(std::exception const &e) {
std::cerr << "Failed " << e.what() << std::endl;
response().out() << "not ok";
}
}
}
};
int main(int argc,char **argv)
{
try {
cppcms::service srv(argc,argv);
srv.applications_pool().mount( cppcms::applications_factory<unit_test>());
srv.after_fork(submitter(srv));
srv.run();
}
catch(std::exception const &e) {
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
return run_ok ? EXIT_SUCCESS : EXIT_FAILURE;
}
MongoDB Logo MongoDB