/
dispatcher_test.cpp
96 lines (77 loc) · 2.18 KB
/
dispatcher_test.cpp
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
#include "dispatcher.hpp"
#include <cassert>
#include <sstream>
#include <iostream>
std::string itoa(int i)
{
std::stringstream out;
out << i;
return out.str();
}
CSGI::Response make_response(std::string body)
{
CSGI::Response ret;
ret.status = 200;
ret.headers["Content-Type"] = "text/plain";
ret.headers["Content-Length"] = itoa(body.length());
ret.content = body;
return ret;
}
CSGI::Response hello_handler(CSGI::Env& e)
{
return make_response("Hello, world!");
(void)e;
}
CSGI::Response aboot_handler(CSGI::Env& e)
{
return make_response("Could you tell us again what your argument "
"is all ABOUT?");
(void)e;
}
CSGI::Response placki_handler(CSGI::Env& e)
{
return make_response("Lubię placki!");
(void)e;
}
CSGI::Response another_handler(CSGI::Env& e)
{
return make_response("Another prick in the wall");
(void)e;
}
void assert_status(Dispatcher& d, const char * uri, int status)
{
CSGI::Env env;
env["REQUEST_URI"] = std::string(uri);
assert(d.dispatch(env).status == status);
}
void assert_found(Dispatcher& d, const char * uri)
{
assert_status(d, uri, 200);
}
int main()
{
Dispatcher dispatcher(NULL);
dispatcher.add_handler("/hello", hello_handler);
dispatcher.add_handler("aboot/", aboot_handler);
dispatcher.add_handler("/placki/", placki_handler);
dispatcher.add_handler("another", another_handler);
assert_found(dispatcher, "/hello");
assert_found(dispatcher, "/hello/");
assert_found(dispatcher, "hello/");
assert_found(dispatcher, "hello");
assert_found(dispatcher, "/aboot");
assert_found(dispatcher, "/aboot/");
assert_found(dispatcher, "aboot/");
assert_found(dispatcher, "aboot");
assert_found(dispatcher, "/placki");
assert_found(dispatcher, "/placki/");
assert_found(dispatcher, "placki/");
assert_found(dispatcher, "placki");
assert_found(dispatcher, "/another");
assert_found(dispatcher, "/another/");
assert_found(dispatcher, "another/");
assert_found(dispatcher, "another");
assert_status(dispatcher, "dupa", 404);
std::cerr << "All tests OK" << std::endl;
return 0;
}