-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_test.cpp
More file actions
166 lines (131 loc) · 4.16 KB
/
parse_test.cpp
File metadata and controls
166 lines (131 loc) · 4.16 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
#define private public
#include "server_ws.hpp"
#include "client_ws.hpp"
#include <iostream>
using namespace std;
using namespace SimpleWeb;
class SocketServerTest : public SocketServerBase<WS> {
public:
SocketServerTest() :
SocketServerBase<WS>::SocketServerBase(8080, 1, 5, 300) {}
void accept() {}
bool parse_request_test() {
std::shared_ptr<Connection> connection(new Connection(new WS(asio_io_service)));
stringstream ss;
ss << "GET /test/ HTTP/1.1\r\n";
ss << "TestHeader: test\r\n";
ss << "TestHeader2:test2\r\n";
ss << "\r\n";
parse_handshake(connection, ss);
if(connection->method!="GET")
return 0;
if(connection->path!="/test/")
return 0;
if(connection->http_version!="1.1")
return 0;
if(connection->header.size()!=2)
return 0;
if(connection->header.count("TestHeader")==0)
return 0;
if(connection->header["TestHeader"]!="test")
return 0;
if(connection->header.count("TestHeader2")==0)
return 0;
if(connection->header["TestHeader2"]!="test2")
return 0;
return 1;
}
};
class SocketClientTest : public SocketClientBase<WS> {
public:
SocketClientTest(const std::string& server_port_path) : SocketClientBase<WS>::SocketClientBase(server_port_path, 80) {}
void connect() {}
bool constructor_parse_test1() {
if(path!="/test")
return 0;
if(host!="test.org")
return 0;
if(port!=8080)
return 0;
return 1;
}
bool constructor_parse_test2() {
if(path!="/test")
return 0;
if(host!="test.org")
return 0;
if(port!=80)
return 0;
return 1;
}
bool constructor_parse_test3() {
if(path!="/")
return 0;
if(host!="test.org")
return 0;
if(port!=80)
return 0;
return 1;
}
bool constructor_parse_test4() {
if(path!="/")
return 0;
if(host!="test.org")
return 0;
if(port!=8080)
return 0;
return 1;
}
bool parse_response_header_test() {
connection=std::unique_ptr<Connection>(new Connection(new WS(asio_io_service)));
stringstream ss;
ss << "HTTP/1.1 200 OK\r\n";
ss << "TestHeader: test\r\n";
ss << "TestHeader2:test2\r\n";
ss << "\r\n";
parse_handshake(ss);
if(connection->header.size()!=2)
return 0;
if(connection->header.count("TestHeader")==0)
return 0;
if(connection->header["TestHeader"]!="test")
return 0;
if(connection->header.count("TestHeader2")==0)
return 0;
if(connection->header["TestHeader2"]!="test2")
return 0;
return 1;
}
};
int main(int argc, char** argv) {
SocketServerTest serverTest;
if(!serverTest.parse_request_test()) {
cerr << "FAIL SocketServer::parse_request" << endl;
return 1;
}
SocketClientTest clientTest("test.org:8080/test");
if(!clientTest.constructor_parse_test1()) {
cerr << "FAIL SocketClient::SocketClient" << endl;
return 1;
}
SocketClientTest clientTest2("test.org/test");
if(!clientTest2.constructor_parse_test2()) {
cerr << "FAIL SocketClient::SocketClient" << endl;
return 1;
}
SocketClientTest clientTest3("test.org");
if(!clientTest3.constructor_parse_test3()) {
cerr << "FAIL SocketClient::SocketClient" << endl;
return 1;
}
SocketClientTest clientTest4("test.org:8080");
if(!clientTest4.constructor_parse_test4()) {
cerr << "FAIL SocketClient::SocketClient" << endl;
return 1;
}
if(!clientTest4.parse_response_header_test()) {
cerr << "FAIL SocketClient::parse_response_header" << endl;
return 1;
}
return 0;
}