Skip to content

Commit

Permalink
fix(tests): add more tests adn fix c++ parser test
Browse files Browse the repository at this point in the history
  • Loading branch information
dantol29 committed May 23, 2024
1 parent 55e807d commit 7567f51
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 11 deletions.
6 changes: 3 additions & 3 deletions include/webserv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

#define SEND_BUFFER_SIZE 1024 * 100 // 100 KB
#define BUFFER_SIZE 1025
#define CGI_TIMEOUT_MS 10000 // 10 seconds
#define CGI_TIMEOUT_S 10 // 10 seconds
#define CGI_POLL_TIMEOUT_MS 500 // 0.5 seconds
#define CLIENT_POLL_TIMEOUT_MS 15000 // 15 seconds
#define CLIENT_TIMEOUT_MS 10000 // 10 seconds
#define CLIENT_POLL_TIMEOUT_MS 10000 // 10 seconds
#define CLIENT_TIMEOUT_S 7 // 7 seconds
#define CONFIG_FILE_DEFAULT_PATH "./conf/webserv_default.conf"

#define RED "\033[1;31m"
Expand Down
5 changes: 3 additions & 2 deletions src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ void Server::waitCGI()

double elapsed = difftime(time(NULL), _connections[i].getCGIStartTime());
Debug::log("Elapsed time: " + toString(elapsed) + " seconds", Debug::CGI);
if (_connections[i].getHasCGI() && elapsed > CGI_TIMEOUT_MS) // 10 seconds
if (_connections[i].getHasCGI() && elapsed > CGI_TIMEOUT_S) // 10 seconds
{
Debug::log("CGI timed out", Debug::NORMAL);

Expand Down Expand Up @@ -818,7 +818,8 @@ void Server::handleSocketTimeoutIfAny()
continue;

double elapsed = difftime(time(NULL), _connections[i].getStartTime());
if (elapsed > CLIENT_TIMEOUT_MS) // 10 seconds
Debug::log("Elapsed time: " + toString(elapsed) + " seconds", Debug::SERVER);
if (elapsed > CLIENT_TIMEOUT_S) // 10 seconds
{
Debug::log("Elapsed time: " + toString(elapsed) + " seconds", Debug::SERVER);
// We have to send a 408 Request Timeout
Expand Down
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

int main(int argc, char **argv)
{
Debug::enable(false);
Debug::setLevel(Debug::NORMAL);
Debug::enable(true);
Debug::setLevel(Debug::SERVER);

if (argc > 2)
{
Expand Down
36 changes: 36 additions & 0 deletions tests/error_codes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,42 @@ else
echo -e "$RED www.python_site.com:8080: $response $RESET"
fi

response=$(curl -s -o /dev/null -w "%{http_code}" -H "Host: www.development_site" http://127.0.0.1:8080/cgi-bin/error.cgi)

if [ "$response" -eq 500 ]; then
echo -e "$GREEN www.development_site.com:8080: 500(Internal Server Error) $RESET"
else
echo -e "$RED www.development_site.com:8080: $response $RESET"
is_error=true
fi

response=$(curl -s -o /dev/null -w "%{http_code}" -H "Host: www.development_site" http://127.0.0.1:8080/cgi-bin/permission.cgi)

if [ "$response" -eq 500 ]; then
echo -e "$GREEN www.development_site.com:8080: 500(Internal Server Error) $RESET"
else
echo -e "$RED www.development_site.com:8080: $response $RESET"
is_error=true
fi

response=$(curl -s -o /dev/null -w "%{http_code}" -H "Host: www.development_site" http://127.0.0.1:8080/cgi-bin/hello_var.cgi)

if [ "$response" -eq 200 ]; then
echo -e "$GREEN www.development_site.com:8080: 200 $RESET"
else
echo -e "$RED www.development_site.com:8080: $response $RESET"
is_error=true
fi

response=$(curl -s -o /dev/null -w "%{http_code}" -H "Host: www.development_site" http://127.0.0.1:8080/cgi-bin/duration_ts.cgi)

if [ "$response" -eq 200 ]; then
echo -e "$GREEN www.development_site.com:8080: 200 $RESET"
else
echo -e "$RED www.development_site.com:8080: $response $RESET"
is_error=true
fi

if [ "$is_error" = true ]; then
exit 1
fi
Expand Down
8 changes: 4 additions & 4 deletions tests/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ void sendData(const std::vector<HTTPTest> &tests, sockaddr_in serverAddress)
ssize_t bytesSent = send(clientSocket, test.request.c_str(), test.request.size(), 0);

char buffer[BUFFER_SIZE];
if (waitForResponseWitPoll(clientSocket, POLL_TIMOUT * 100))
if (waitForResponseWitPoll(clientSocket, POLL_TIMOUT * 15))
{
ssize_t bytesRead = read(clientSocket, buffer, BUFFER_SIZE);
if (bytesRead < 0)
Expand Down Expand Up @@ -281,18 +281,18 @@ int main(int argc, char **argv)

// if (std::strcmp(argv[1], "query") == 0)
std::cout << "\033[38;5;214mRunning query tests\033[0m" << std::endl;
// query(serverAddress);
query(serverAddress);
// else if (std::strcmp(argv[1], "simple") == 0)
std::cout << "\033[38;5;214mQuery tests done\033[0m" << std::endl;
std::cout << "\033[38;5;214mRunning simple tests\033[0m" << std::endl;
// simple(serverAddress);
simple(serverAddress);
std::cout << "\033[38;5;214mSimple tests done\033[0m" << std::endl;
std::cout << "\033[38;5;214mRunning headers tests\033[0m" << std::endl;
// else if (std::strcmp(argv[1], "headers") == 0)
headers(serverAddress);
std::cout << "\033[38;5;214mHeaders tests done\033[0m" << std::endl;
// else if (std::strcmp(argv[1], "body") == 0)
// body(serverAddress);
body(serverAddress);
// else
// std::cout << "Invalid test name" << std::endl;
if (is_error)
Expand Down

0 comments on commit 7567f51

Please sign in to comment.