-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrequest_handler_test.cpp
88 lines (65 loc) · 3.09 KB
/
request_handler_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
#include "gtest/gtest.h"
#include "request_handler.h"
#include "gmock/gmock.h"
#include <boost/asio.hpp>
#include <string>
#include "reply.h"
#include "request.hpp"
#include <iostream>
TEST(RequestHandlerTest, HandleRequestNotFound) {
std::string doc_root = ".";
http::server::request_handler request_handler_tester(doc_root);
http::server::request my_request;
//form out my test request
my_request.method = "GET";
my_request.uri = "/my_test_dir/my_test.html"; //Expecting 404 Not Found since it is not a real file
my_request.http_version_major = 0;
my_request.http_version_minor = 0;
http::server::reply my_reply;
request_handler_tester.handle_request(my_request, my_reply);
std::string expected_response = "<html><head><title>Not Found</title></head><body><h1>404 Not Found</h1></body></html>"; //404 Not Found in HTML format
std::string response_length = std::to_string(expected_response.length());
EXPECT_EQ(my_reply.content, expected_response);
EXPECT_EQ(my_reply.headers[0].name, "Content-Length");
EXPECT_EQ(my_reply.headers[0].value, response_length.c_str());
EXPECT_EQ(my_reply.headers[1].name, "Content-Type");
EXPECT_EQ(my_reply.headers[1].value, "text/html");
}
TEST(RequestHandlerTest, HandleGoodRequest) {
std::string doc_root = ".";
http::server::request_handler request_handler_tester(doc_root);
http::server::request my_request;
//form out my test request
my_request.method = "GET";
my_request.uri = "/index.html"; //should return our default: index.html
my_request.http_version_major = 0;
my_request.http_version_minor = 0;
http::server::reply my_reply;
request_handler_tester.handle_request(my_request, my_reply);
std::string expected_response = "<html><h1>Usage: /echo for echo server</h1><h1>Usage: /filePath to serve static files</h1></html>"; //Our index.html content
std::string response_length = std::to_string(expected_response.length());
EXPECT_EQ(my_reply.content, expected_response);
EXPECT_EQ(my_reply.headers[0].name, "Content-Length");
EXPECT_EQ(my_reply.headers[0].value, response_length.c_str());
EXPECT_EQ(my_reply.headers[1].name, "Content-Type");
EXPECT_EQ(my_reply.headers[1].value, "text/html");
}
TEST(RequestHandlerTest, HandleBadRequest) {
std::string doc_root = ".";
http::server::request_handler request_handler_tester(doc_root);
http::server::request my_request;
//form my test request
my_request.method = "GET";
my_request.uri = "index[html"; //should return a bad request
my_request.http_version_major = 0;
my_request.http_version_minor = 0;
http::server::reply my_reply;
request_handler_tester.handle_request(my_request, my_reply);
std::string expected_response = "<html><head><title>Bad Request</title></head><body><h1>400 Bad Request</h1></body></html>"; //Bad request
std::string response_length = std::to_string(expected_response.length());
EXPECT_EQ(my_reply.content, expected_response);
EXPECT_EQ(my_reply.headers[0].name, "Content-Length");
EXPECT_EQ(my_reply.headers[0].value, response_length.c_str());
EXPECT_EQ(my_reply.headers[1].name, "Content-Type");
EXPECT_EQ(my_reply.headers[1].value, "text/html");
}