1+ #include < arpa/inet.h>
2+ #include < netdb.h>
3+ #include < netinet/in.h>
4+ #include < sys/socket.h>
5+ #include < sys/types.h>
6+ #include < unistd.h>
7+
8+ #include < iostream>
9+
10+ #include " Request.hpp"
11+ #include " colors.hpp"
12+
13+ typedef struct request_s {
14+ std::string method;
15+ std::string uri;
16+ std::string version;
17+ std::string responseCode;
18+ } request_t ;
19+
20+ const request_t tests_g[] = {
21+ {" GET" , " /" , " HTTP/2" , " 505" },
22+ {" GET" , " /" , " HTTP/1.1" , " 200" },
23+ {" GET" , " /*" , " HTTP/1.1" , " 404" },
24+ {" GET" , " */" , " HTTP/1.1" , " 400" },
25+ {" OPTIONS" , " *" , " HTTP/1.1" , " 200" },
26+ {" OPTIONS" , " /*" , " HTTP/1.1" , " 200" },
27+ {" OPTIONS" , " */" , " HTTP/1.1" , " 400" },
28+ {" OPTIONS" , " ../" , " HTTP/1.1" , " 400" },
29+ {" GET" , " /../" , " HTTP/1.1" , " 400" },
30+ {" GET" , " /te4st/../" , " HTTP/1.1" , " 200" },
31+ {" GET" , " /test/../../" , " HTTP/1.1" , " 400" },
32+ {" LOL" , " /" , " HTTP/1.1" , " 501" },
33+ {" GET" , " http://localhost/" , " HTTP/1.1" , " 200" },
34+ {" PUT" , " /" , " HTTP/1.1" , " 405" },
35+ {" POST" , " /" , " HTTP/1.1" , " 405" },
36+ {" PUT" , " /upload/test.txt" , " HTTP/1.1" , " 411" },
37+ {" POST" , " /listing/cgi/index.php" , " HTTP/1.1" , " 411" },
38+ {" GET" , " /redirect/" , " HTTP/1.1" , " 301" },
39+ {" GET" , " /redirect2/" , " HTTP/1.1" , " 301" }};
40+
41+ int readStatusLine (int fd, int i, std::string requestLine) {
42+ std::string data;
43+ char buffer[1000 ];
44+ int ret = read (fd, buffer, 1000 );
45+ if (ret == 0 ) return 1 ;
46+ data.append (buffer, ret);
47+ data = data.erase (data.find (" \r\n " ));
48+ std::vector<std::string> statusLine =
49+ split<std::vector<std::string> >(data, " " , true );
50+ if (statusLine[1 ] == tests_g[i].responseCode )
51+ std::cout << GREEN << " Success: " << requestLine
52+ << " - > Got : " << tests_g[i].responseCode << RESET << std::endl;
53+ else
54+ std::cout << RED << " Fail: " << requestLine
55+ << " - > Expected: " << tests_g[i].responseCode
56+ << " Got: " << statusLine[1 ] << RESET << std::endl;
57+ return 0 ;
58+ }
59+
60+ int main (int argc, char **argv) {
61+ for (size_t i = 0 ; i < sizeof (tests_g) / sizeof (request_t ); i++) {
62+ int fd;
63+ struct sockaddr_in addr;
64+ struct sockaddr_in cmp;
65+ addr.sin_addr .s_addr = htonl (INADDR_LOOPBACK);
66+ addr.sin_port = htons (8080 );
67+ addr.sin_family = AF_INET;
68+ fd = socket (AF_INET, SOCK_STREAM, 0 );
69+ if (fd == -1 ) {
70+ std::cerr << " error: socket\n " ;
71+ return 1 ;
72+ }
73+ if (connect (fd, (sockaddr *)(&addr), sizeof (addr)) == -1 ) {
74+ std::cerr << " error: connect\n " ;
75+ return 1 ;
76+ }
77+ std::string requestLine =
78+ tests_g[i].method + " " + tests_g[i].uri + " " + tests_g[i].version ;
79+ std::string msg =
80+ requestLine + " \r\n " + " Host: localhost:8080\r\n " + " \r\n " ;
81+ write (fd, msg.c_str (), msg.size ());
82+ readStatusLine (fd, i, requestLine);
83+ close (fd);
84+ }
85+ }
0 commit comments