Skip to content

Commit

Permalink
Merge pull request #387 from dantol29/dtolmaco/add-error-codes-tests
Browse files Browse the repository at this point in the history
feat(tests): add tests
  • Loading branch information
552020 authored May 21, 2024
2 parents 36a1684 + de7eafd commit 31a07a8
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 126 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/github-actions-demo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ jobs:
echo "❌ Tests failed. Exiting with status code $exit_code."
exit $exit_code
fi
- name: Run Error codes tests
if: always()
working-directory: tests # Set working directory to 'tests'
run: |
./error_codes.sh
exit_code=$?
if [ $exit_code -ne 0 ]; then
echo "❌ Tests failed. Exiting with status code $exit_code."
exit $exit_code
fi
- name: Run C++ parser tests
if: always()
working-directory: tests # Set working directory to 'tests'
Expand Down
11 changes: 10 additions & 1 deletion conf/webserv_default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ server {
}

server {
listen 8080;
listen 8081;
server_name www.php_site.com;
allow_methods GET POST DELETE;
autoindex off;
Expand All @@ -46,4 +46,13 @@ server {
server_name www.development_site;
allow_methods GET POST DELETE;
root var/;
}

server {
listen 8080;
server_name www.python_site.com;
allow_methods POST DELETE;
client_max_body_size 1000;
autoindex off;
root var/;
}
9 changes: 0 additions & 9 deletions config/development.conf

This file was deleted.

35 changes: 0 additions & 35 deletions config/webserv.conf

This file was deleted.

47 changes: 0 additions & 47 deletions config/webserv_default.conf

This file was deleted.

54 changes: 28 additions & 26 deletions src/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,32 +282,34 @@ std::ostream &operator<<(std::ostream &out, const Config &file)
std::cout << std::endl;
std::cout << "alias: " << var._alias << std::endl;

for (unsigned int j = 0; j < loc.size(); ++j)
{
std::cout << "------------------Location-Block------------------------" << std::endl;
std::cout << "path: " << loc[j]._path << std::endl;
for (unsigned int k = 0; k < loc[j]._listen.size(); ++k)
{
std::cout << "ip: " << loc[j]._listen[k].getIp() << std::endl;
std::cout << "port: " << loc[j]._listen[k].getPort() << std::endl;
std::cout << "isIpv6: " << loc[j]._listen[k].getIsIpv6() << std::endl;
}
std::cout << "server_name: ";
for (unsigned int l = 0; l < loc[j]._serverName.size(); ++l)
std::cout << loc[j]._serverName[l] << " ";
for (unsigned int m = 0; m < loc[j]._errorPage.size(); ++m)
std::cout << loc[j]._errorPage[m].first << " " << loc[j]._errorPage[m].second << std::endl;
for (unsigned int n = 0; n < loc[j]._index.size(); ++n)
std::cout << loc[j]._index[n] << " ";
std::cout << "root: " << loc[j]._root << std::endl;
std::cout << "client_max_body_size: " << loc[j]._clientMaxBodySize << std::endl;
std::cout << "autoindex: " << loc[j]._autoindex << std::endl;
std::cout << "allowed_methods: ";
for (unsigned int o = 0; o < loc[j]._allowedMethods.size(); ++o)
std::cout << loc[j]._allowedMethods[o] << " ";
std::cout << std::endl;
std::cout << "alias: " << loc[j]._alias << std::endl;
}

// for (unsigned int i = 0; i < loc.size(); ++i)
// {
// std::cout << "------------------Location-Block------------------------" << std::endl;
// std::cout << "path: " << loc[i]._path << std::endl;
// for (unsigned int i = 0; i < loc[i]._listen.size(); ++i)
// {
// std::cout << "ip: " << loc[i]._listen[i].getIp() << std::endl;
// std::cout << "port: " << loc[i]._listen[i].getPort() << std::endl;
// std::cout << "isIpv6: " << loc[i]._listen[i].getIsIpv6() << std::endl;
// }
// std::cout << "server_name: ";
// for (unsigned int i = 0; i < loc[i]._serverName.size(); ++i)
// std::cout << loc[i]._serverName[i] << " ";
// for (unsigned int i = 0; i < loc[i]._errorPage.size(); ++i)
// std::cout << loc[i]._errorPage[i].first << " " << loc[i]._errorPage[i].second << std::endl;
// for (unsigned int i = 0; i < loc[i]._index.size(); ++i)
// std::cout << loc[i]._index[i] << " ";
// std::cout << "root: " << loc[i]._root << std::endl;
// std::cout << "client_max_body_size: " << loc[i]._clientMaxBodySize << std::endl;
// std::cout << "autoindex: " << loc[i]._autoindex << std::endl;
// std::cout << "allowed_methods: ";
// for (unsigned int i = 0; i < loc[i]._allowedMethods.size(); ++i)
// std::cout << loc[i]._allowedMethods[i] << " ";
// std::cout << std::endl;
// std::cout << "alias: " << loc[i]._alias << std::endl;
// }

std::cout << "------------------END---------------------------------" << std::endl;
}

Expand Down
9 changes: 5 additions & 4 deletions src/Router.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,19 @@ void Router::routeRequest(HTTPRequest &request, HTTPResponse &response)
std::cout << BLUE << "path: " << request.getPath() << RESET << std::endl;
std::cout << BLUE << "PathValidation: " << pathResult << RESET << std::endl;
// check if method is allowed

if (!_directive._allowedMethods.empty())
{
for (size_t i = -1; i < _directive._allowedMethods.size(); i++)
for (size_t i = 0; i < _directive._allowedMethods.size(); i++)
{
if (_directive._allowedMethods[i] == request.getMethod())
{
break;
}
if (i == _directive._allowedMethods.size() - 0)
if (i == _directive._allowedMethods.size() - 1)
{
response.setStatusCode(404, "Method Not Allowed");
handleServerBlockError(request, response, 404);
response.setStatusCode(405, "Method Not Allowed");
handleServerBlockError(request, response, 405);
return;
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ int main(int argc, char **argv)
Server webserv(config, eventManager);

ServerEventListener serverEventListener(webserv);
std::cout << "Subscribing serverEventListener" << std::endl;
std::cout << "Pointer to serverEventListener: " << &serverEventListener << std::endl;

eventManager.subscribe(&serverEventListener);

std::cout << &webserv.getEventManager() << std::endl;
std::cout << &eventManager << std::endl;
std::cout << "SIZE: " << webserv.getEventManager().getObservers().size() << std::endl;

webserv.startListening();
webserv.startPollEventLoop();

Expand Down
74 changes: 74 additions & 0 deletions tests/error_codes.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/bin/bash

PURPLE='\033[0;35m'
BLUE='\033[0;34m'
GREEN='\033[0;32m'
RED='\033[0;31m'
RESET='\033[0m'

URL="http://127.0.0.1:8080/"

is_error=false

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

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

response=$(curl -s -o /dev/null -w "%{http_code}" -H "Host: www.example.com" http://127.0.0.1:8081/)

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

response=$(curl -s -o /dev/null -w "%{http_code}" -H "Host: www.php_site.com" http://127.0.0.1:8081/)

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

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

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

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

if [ "$response" -eq 405 ]; then
echo -e "$GREEN www.python_site.com:8080: 405(Method not allowed) $RESET"
else
echo -e "$RED www.python_site.com:8080: $response $RESET"
is_error=true
fi

# Generate a 2KB string of 'a' characters
data=$(head -c 2048 < /dev/zero | tr '\0' 'a')

# Send the POST request and capture the response code
response=$(curl -s -o /dev/null -w "%{http_code}" -H "Host: www.python_site.com" -H "Content-Type: text/plain" -d "$data" http://127.0.0.1:8080/)

if [ "$response" -eq 413 ]; then
echo -e "$GREEN www.python_site.com:8080: 413(Payload too large) $RESET"
else
echo -e "$RED www.python_site.com:8080: $response $RESET"
fi

if [ "$is_error" = true ]; then
exit 1
fi

exit 0

0 comments on commit 31a07a8

Please sign in to comment.