Menu

[r2318]: / framework / trunk / tests / response_test.cpp  Maximize  Restore  History

Download this file

164 lines (148 with data), 4.2 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
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
//
// See accompanying file COPYING.TXT file for licensing details.
//
///////////////////////////////////////////////////////////////////////////////
#include "tc_test_content.h"
#include <cppcms/service.h>
#include <cppcms/http_response.h>
#include <cppcms/json.h>
#include <cppcms/cache_interface.h>
#include <cppcms/url_mapper.h>
#include "dummy_api.h"
#include "test.h"
#include <iomanip>
#include <sstream>
void compare_strings(std::string const &l,std::string const &r,int file_line)
{
if(l==r) {
//std::cerr << "[" << l << "] == [" << r << "]" << " at " << file_line << " OK !" << std::endl;
return;
}
/*
size_t m = l.size();
if(r.size() > m) m = r.size();
int line = 1;
for(size_t i=0;i<m;i++) {
std::string lstr = conv(l,i);
std::string rstr = conv(r,i);
if(lstr=="\\n")
line++;
std::cerr << std::setw(4) << line << " [" << lstr << '|' << rstr << "] ";
if(lstr!=rstr)
std::cerr << "<----------" << std::endl;
else
std::cerr << std::endl;
}*/
std::cerr << "[" << l << "]!=[" << r << "]" << " at " << file_line << std::endl;
throw std::runtime_error("Failed test");
}
#define TEQ(l,r) compare_strings(l,r,__LINE__)
class test_app : public cppcms::application {
public:
test_app(cppcms::service &srv) :
cppcms::application(srv),
srv_(srv)
{
mapper().assign("foo","/foo");
mapper().assign("/");
mapper().assign("/{1}");
mapper().assign("/{1}/{2}");
}
~test_app()
{
release_context();
}
void set_context(bool mark_chunks=false,bool mark_eof=false)
{
std::map<std::string,std::string> env;
env["HTTP_HOST"]="www.example.com";
env["SCRIPT_NAME"]="/foo";
env["PATH_INFO"]="/bar";
env["REQUEST_METHOD"]="GET";
env["HTTP_ACCEPT_ENCODING"]=="gzip";
booster::shared_ptr<dummy_api> api(new dummy_api(srv_,env,output_,mark_chunks,mark_eof));
booster::shared_ptr<cppcms::http::context> cnt(new cppcms::http::context(api));
assign_context(cnt);
output_.clear();
}
std::string str()
{
std::string result = output_;
output_.clear();
return result;
}
void test_buffer_size(bool async)
{
std::cout << "- Test setbuf/flush " << (async ? "async" : "sync")<< std::endl;
set_context(true,true);
if(async) {
response().io_mode(cppcms::http::response::asynchronous);
}
else {
response().io_mode(cppcms::http::response::nogzip);
}
response().full_asynchronous_buffering(false);
response().out();
response().setbuf(0);
str();
response().out() << "x";
TEQ(str(),"[x]");
response().out() << 123;
TEQ(str(),"[123]");
response().setbuf(4);
response().out() << "abcdefg";
TEQ(str(),"[abcdefg]");
response().out() << 124;
TEQ(str(),"");
response().out() << std::flush;
TEQ(str(),"[124]");
response().out() << "xxx";
TEQ(str(),"");
response().setbuf(0);
TEQ(str(),"[xxx]");
if(async) {
response().setbuf(4);
std::cout<< "-- fully/partially buffered mode" << std::endl;
response().full_asynchronous_buffering(true);
response().out() << "12345678";
TEQ(str(),"");
response().out() << std::flush;
TEQ(str(),"");
response().full_asynchronous_buffering(false);
TEQ(str(),"[12345678]");
response().full_asynchronous_buffering(true);
response().out() << "123";
TEQ(str(),"");
response().full_asynchronous_buffering(false);
response().out() << std::flush;
TEQ(str(),"[123]");
}
response().finalize();
TEQ(str(),"[EOF]");
}
private:
std::string output_;
cppcms::service &srv_;
};
int main()
{
try {
cppcms::json::value cfg;
cfg["cache"]["backend"]="thread_shared";
cfg["cache"]["limit"]=100;
cppcms::service srv(cfg);
test_app app(srv);
app.test_buffer_size(false);
app.test_buffer_size(true);
}
catch(std::exception const &e)
{
std::cerr << "Fail " << e.what() << std::endl;
return 1;
}
std::cout << "Ok" << std::endl;
return 0;
}
MongoDB Logo MongoDB