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