Skip to content

Commit ca3e50c

Browse files
committed
Added test scripts
1 parent 43f5365 commit ca3e50c

File tree

5 files changed

+226
-2
lines changed

5 files changed

+226
-2
lines changed

EVAL.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
- [ ] Check if wordpress is working
2-
- [ ] Test with script [testResponses.py](websites/config_test/listing/testResponses.py)
2+
- [ ] Test with script [testResponses.py](tests/testResponses.py)
3+
- [ ] Test with script [createChunkedRequest.py](tests/createChunkedRequest.py)
4+
- [ ] Test with script [testHeader.cpp](tests/testHeader.cpp)
35
- [ ] Go through config_test website and test basic functionality of all links

tests/Makefile

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
NAME := webserv
2+
3+
OBJ_DIR := obj
4+
SRC_DIR := src
5+
INC_DIR := include
6+
7+
SRC += main.cpp
8+
SRC += global.cpp
9+
10+
SRC += poll/Address.cpp
11+
SRC += poll/AConnection.cpp
12+
SRC += poll/ListenSocket.cpp
13+
SRC += poll/Poll.cpp
14+
SRC += poll/CallbackPointer.cpp
15+
SRC += poll/timeval.cpp
16+
17+
SRC += config/argument.cpp
18+
SRC += config/Config.cpp
19+
SRC += config/Context.cpp
20+
SRC += config/Init.cpp
21+
22+
SRC += http/Http.cpp
23+
SRC += http/Request.cpp
24+
SRC += http/Response.cpp
25+
SRC += http/Uri.cpp
26+
SRC += http/VirtualHost.cpp
27+
28+
SRC += output/output.cpp
29+
SRC += output/Log.cpp
30+
31+
SRC += utils/utils.cpp
32+
SRC += utils/File.cpp
33+
34+
HEADERS += webserv.hpp
35+
HEADERS += global.hpp
36+
37+
HEADERS += config/argument.hpp
38+
HEADERS += config/Config.hpp
39+
HEADERS += config/Context.hpp
40+
HEADERS += config/Init.hpp
41+
42+
HEADERS += poll/Address.hpp
43+
HEADERS += poll/AConnection.hpp
44+
HEADERS += poll/ListenSocket.hpp
45+
HEADERS += poll/Poll.hpp
46+
HEADERS += poll/IFileDescriptor.hpp
47+
HEADERS += poll/CallbackPointer.hpp
48+
HEADERS += poll/timeval.hpp
49+
50+
HEADERS += http/Http.hpp
51+
HEADERS += http/Request.hpp
52+
HEADERS += http/Response.hpp
53+
HEADERS += http/VirtualHost.hpp
54+
55+
HEADERS += output/colors.hpp
56+
HEADERS += output/Log.hpp
57+
HEADERS += output/output.hpp
58+
59+
HEADERS += utils/File.hpp
60+
HEADERS += utils/utils.hpp
61+
62+
OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.cpp=%.o))
63+
DEPS := $(addprefix $(INC_DIR)/, $(HEADERS))
64+
65+
CXX := c++
66+
CXXFLAGS := -Wall -Wextra -Werror -std=c++98 -Iinclude -Iinclude/poll -Iinclude/config -Iinclude/http -Iinclude/utils -Iinclude/output
67+
68+
all: testHeader
69+
70+
testHeader:
71+
c++ testHeader.cpp -o testHeader.out -I../include/http -I../include/utils -I../include/output
72+
73+
fclean:
74+
rm -f *.out
75+
76+
re: fclean all
77+
78+
.PHONY: all fclean re testHeader

tests/createChunkedRequest.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import sys
2+
import argparse
3+
import requests
4+
5+
def split_string_into_chunks(input_string, chunk_size):
6+
return [input_string[i:i + chunk_size] for i in range(0, len(input_string), chunk_size)]
7+
8+
def send_chunked_request(url, data):
9+
# Create an empty list to store the data chunks
10+
data_chunks = split_string_into_chunks(data, 1000)
11+
12+
# Create a session to send the request
13+
with requests.Session() as session:
14+
def chunked_data():
15+
for chunk in data_chunks:
16+
yield chunk.encode() # Encode each chunk as bytes
17+
18+
# Send the request with the 'Transfer-Encoding: chunked' header using a PUT request
19+
response = session.put(url, headers={'Transfer-Encoding': 'chunked'}, data=chunked_data())
20+
21+
# Print the server's response for each chunk if needed
22+
print(response.status_code)
23+
print(response.text)
24+
25+
result = response.status_code == 201 or response.status_code == 204
26+
if result:
27+
sys.exit(0) # Exit with code 0 for success
28+
else:
29+
sys.exit(1) # Exit with a non-zero code for failure
30+
31+
if __name__ == '__main__':
32+
# Define the URL of the server you want to test
33+
host = 'http://localhost:8080'
34+
35+
# Create an argument parser to accept the request URI and data as arguments
36+
parser = argparse.ArgumentParser(description='Send data in chunks via PUT request')
37+
parser.add_argument('uri', help='Request URI')
38+
parser.add_argument('data', help='Data to send')
39+
args = parser.parse_args()
40+
41+
url = host + args.uri
42+
43+
# Call the function to send the chunked request
44+
send_chunked_request(url, args.data)

tests/testHeader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ int readStatusLine(int fd, int i, std::string requestLine) {
4949
split<std::vector<std::string> >(data, " ", true);
5050
if (statusLine[1] == tests_g[i].responseCode)
5151
std::cout << GREEN << "Success: " << requestLine
52-
<< " - > Got : " << tests_g[i].responseCode << RESET << std::endl;
52+
<< " -> Got : " << tests_g[i].responseCode << RESET << std::endl;
5353
else
5454
std::cout << RED << "Fail: " << requestLine
5555
<< " - > Expected: " << tests_g[i].responseCode

tests/testResponses.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import requests
2+
import sys
3+
import os
4+
5+
# Define the target URL
6+
target_url = "http://localhost:8080" # Replace with the actual URL you want to send requests to
7+
8+
# List of requests to send with associated data (URL, expected response code, and expected content file)
9+
requests_data = [
10+
{
11+
"url": "/",
12+
"expected_response": 200,
13+
"content_file": "",
14+
},
15+
{
16+
"url": "/notallowed/",
17+
"expected_response": 405,
18+
"content_file": "../websites/config_test/405.html",
19+
},
20+
{
21+
"url": "/notexistingpage/",
22+
"expected_response": 404,
23+
"content_file": "",
24+
},
25+
{
26+
"url": "/isthisworking%3FIhopeitis.html",
27+
"expected_response": 200,
28+
"content_file": "../websites/config_test/isthisworking?Ihopeitis.html",
29+
},
30+
{
31+
"url": "/lol😀.html",
32+
"expected_response": 200,
33+
"content_file": "",
34+
},
35+
{
36+
"url": "/redirect/",
37+
"expected_response": 200,
38+
"content_file": "",
39+
},
40+
{
41+
"url": "/redirect2/",
42+
"expected_response": 200,
43+
"content_file": "",
44+
},
45+
{
46+
"url": "/listing/",
47+
"expected_response": 200,
48+
"content_file": "",
49+
},
50+
{
51+
"url": "/index/",
52+
"expected_response": 200,
53+
"content_file": "../websites/config_test/multiindex/multiindex.html",
54+
},
55+
{
56+
"url": "/kapouet/pouic/toto/pouet",
57+
"expected_response": 200,
58+
"content_file": "../websites/config_test/pouic/toto/pouet",
59+
}
60+
]
61+
62+
# Function to send requests and check responses
63+
def send_and_check_requests():
64+
for request_data in requests_data:
65+
full_url = target_url + request_data["url"]
66+
try:
67+
response = requests.get(full_url)
68+
if response.status_code == request_data["expected_response"]:
69+
# Check if the content file exists
70+
content_file = request_data["content_file"]
71+
if content_file and not os.path.isfile(content_file):
72+
print(f"\033[31mExpected content file for {request_data['url']} is not a file.\033[0m")
73+
sys.exit(1) # Exit with a non-zero status code on error
74+
75+
# Read and compare response content as strings with the correct character encoding
76+
if content_file:
77+
with open(content_file, "r", encoding="utf-8") as expected_file:
78+
expected_content = expected_file.read()
79+
response_content = response.text
80+
81+
if response_content.strip() != expected_content.strip():
82+
print(f"\033[31mResponse content for {full_url} does not match expected content.\033[0m")
83+
print("##################################################################")
84+
print(response.text.strip())
85+
print("##################################################################")
86+
print(expected_content.strip())
87+
print("##################################################################")
88+
sys.exit(1) # Exit with a non-zero status code on error
89+
90+
print(f"\033[32mRequest to {full_url} succeeded with expected response {request_data['expected_response']}.\033[0m")
91+
else:
92+
print(f"\033[31mRequest to {full_url} failed. Expected {request_data['expected_response']}, got {response.status_code}.\033[0m")
93+
sys.exit(1) # Exit with a non-zero status code on error
94+
except requests.exceptions.RequestException as e:
95+
print(f"\033[31mRequest to {full_url} failed: {e}\033[0m")
96+
sys.exit(1) # Exit with a non-zero status code on error
97+
98+
if __name__ == "__main__":
99+
send_and_check_requests()
100+
sys.exit(0) # Exit with a status code of 0 on success

0 commit comments

Comments
 (0)