From 3cf8cf07e7d1b94c74facd1d16cc647bde224459 Mon Sep 17 00:00:00 2001 From: dantol29 Date: Tue, 21 May 2024 20:31:00 +0200 Subject: [PATCH 1/2] feat(tests): add tests --- .github/workflows/github-actions-demo.yml | 10 +++ conf/webserv_default.conf | 11 +++- config/development.conf | 9 --- config/webserv.conf | 35 ----------- config/webserv_default.conf | 47 -------------- src/Config.cpp | 52 ++++++++-------- src/Router.cpp | 9 +-- src/main.cpp | 6 +- tests/error_codes.sh | 74 +++++++++++++++++++++++ 9 files changed, 127 insertions(+), 126 deletions(-) delete mode 100644 config/development.conf delete mode 100644 config/webserv.conf delete mode 100644 config/webserv_default.conf create mode 100755 tests/error_codes.sh diff --git a/.github/workflows/github-actions-demo.yml b/.github/workflows/github-actions-demo.yml index 5f9d890e..965c610d 100644 --- a/.github/workflows/github-actions-demo.yml +++ b/.github/workflows/github-actions-demo.yml @@ -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_test.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' diff --git a/conf/webserv_default.conf b/conf/webserv_default.conf index babdbd27..d6a20f18 100644 --- a/conf/webserv_default.conf +++ b/conf/webserv_default.conf @@ -32,7 +32,7 @@ server { } server { - listen 8080; + listen 8081; server_name www.php_site.com; allow_methods GET POST DELETE; autoindex off; @@ -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/; } \ No newline at end of file diff --git a/config/development.conf b/config/development.conf deleted file mode 100644 index cbe082f4..00000000 --- a/config/development.conf +++ /dev/null @@ -1,9 +0,0 @@ -server { - listen 8080; - server_name www.development_site; - allow_methods GET POST DELETE; - autoindex off; - root /var/; - error_page 404 404.html; - cgi_ext .cgi; -} \ No newline at end of file diff --git a/config/webserv.conf b/config/webserv.conf deleted file mode 100644 index cdd93046..00000000 --- a/config/webserv.conf +++ /dev/null @@ -1,35 +0,0 @@ - -server { - listen 8080; - server_name www.saladbook.xyz; - allow_methods GET POST DELETE; - root var/; - index html/index_salad.html; - autoindex on; - error_page 404 html/404_salad.html; - error_page 400 html/400_salad.html; - client_max_body_size 228; - - location /admin { - allow_methods POST; - } - - location /database { - allow_methods POST; - } - - location /special_effects { - allow_methods GET SALAD; - } -} - -server { - listen 8080; - server_name localhost:8080; - allow_methods GET POST DELETE; - root html/; - index html/; - autoindex on; - error_page 404 404/404.jpg; - -} diff --git a/config/webserv_default.conf b/config/webserv_default.conf deleted file mode 100644 index 326f5278..00000000 --- a/config/webserv_default.conf +++ /dev/null @@ -1,47 +0,0 @@ -server { - listen 8080; - server_name localho:8080 localhost:8080 www.localhost:8080; - allow_methods GET POST DELETE; - autoindex off; - root /var/; - error_page 404 404.html; - cgi_ext .cgi; - - location /admin { - return http://google.com; - index admin.html; - allow_methods GET POST; - } - location /cgi-bin/ { - cgi_ext .cgi .py; - autoindex off; - } -} - -server { - listen 8080; - server_name www.example.com; - allow_methods GET POST; - autoindex on; - root var/; - location / { - index index.html; - allow_methods GET POST; - upload_path upload/; - } -} - -server { - listen 8080; - server_name www.php_site.com; - allow_methods GET POST DELETE; - autoindex off; - root var/; -} - -server { - listen 8080; - server_name www.php_site; - allow_methods GET POST DELETE; - root var/; -} \ No newline at end of file diff --git a/src/Config.cpp b/src/Config.cpp index c4accced..e3048493 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -282,32 +282,32 @@ std::ostream &operator<<(std::ostream &out, const Config &file) std::cout << std::endl; std::cout << "alias: " << var._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; - } + // 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; } diff --git a/src/Router.cpp b/src/Router.cpp index dec60d43..bead8656 100644 --- a/src/Router.cpp +++ b/src/Router.cpp @@ -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; } } diff --git a/src/main.cpp b/src/main.cpp index b5a46e77..a0a18ab8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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(); diff --git a/tests/error_codes.sh b/tests/error_codes.sh new file mode 100755 index 00000000..c6aaf569 --- /dev/null +++ b/tests/error_codes.sh @@ -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 \ No newline at end of file From 2270711a6fbfe36a543f3d22e17f4f5d3e4bca16 Mon Sep 17 00:00:00 2001 From: dantol29 Date: Tue, 21 May 2024 20:33:18 +0200 Subject: [PATCH 2/2] fix(yaml): fix git ations --- .github/workflows/github-actions-demo.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/github-actions-demo.yml b/.github/workflows/github-actions-demo.yml index 965c610d..30d87b22 100644 --- a/.github/workflows/github-actions-demo.yml +++ b/.github/workflows/github-actions-demo.yml @@ -76,7 +76,7 @@ jobs: if: always() working-directory: tests # Set working directory to 'tests' run: | - ./error_codes_test.sh + ./error_codes.sh exit_code=$? if [ $exit_code -ne 0 ]; then echo "❌ Tests failed. Exiting with status code $exit_code."