-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpClientTest.py
More file actions
96 lines (76 loc) · 2.81 KB
/
httpClientTest.py
File metadata and controls
96 lines (76 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
'''
test cases for a simple http client.
'''
import logging
import sys
from urllib.parse import urlparse
# from hw2 import retrieve_url
from hw2 import retrieve_url
#import requests
TEST_CASES = ['http://www.example.com', # most basic example (with no slash)
'http://wonderousshinyinnerspell.neverssl.com/online', # another basic example
'http://info.cern.ch/images/NextEditorBW.gif', # is an image
'http://go.com/doesnotexist', # causes 404
'http://www.ifyouregisterthisforclassyouareoutofcontrol.com/', # NXDOMAIN
'http://www.asnt.org:8080/Test.html', # nonstandard port number
'http://www.httpwatch.com/httpgallery/chunked/chunkedimage.aspx' # chunked encoding
]
TEST_CASES_BONUS = ['https://www.google.com/', # https
'https://😻🍕.ws', # utf-8 in the url
'http://www.fieggen.com/shoelace' # redirects to trailing slash
]
def retrieve_url_bonus(url):
'''
https, redirects, utf-8 in urls!
'''
response = requests.get(url)
if response.status_code == 200:
return response.content
else:
return None
def retrieve_url_oracle(url):
'''
the easiest way to complete this assignment. too bad you can't use it.
'''
scheme = urlparse(url).scheme
if scheme != 'http':
return None
response = requests.get(url, allow_redirects=False)
if response.status_code == 200:
return response.content
else:
return None
def compare_output(url, oracle=retrieve_url_oracle):
'''
compare hw1.py output with requests output for ::url::
'''
try:
# download the same page twice to check for dynamic content
correct_output = oracle(url)
dynamic_output = oracle(url)
if correct_output != dynamic_output:
print("this is a dynamic page, skipping: {}".format(url))
return
except requests.RequestException as exc:
logging.debug(
"something went wrong downloading the page: %s", type(exc).__name__)
correct_output = None
try:
student_output = retrieve_url(url)
except Exception as exc: # pylint: disable=broad-except
print("uncaught exception ({}) for {}".format(type(exc).__name__, url))
return
if correct_output == student_output:
print("correct output for {}".format(url))
else:
print("incorrect output for {}".format(url))
def main(args):
if "--debug" in args:
logging.basicConfig(level=logging.DEBUG)
for testcase in TEST_CASES:
compare_output(testcase)
print("and for style points...")
for testcase in TEST_CASES_BONUS:
compare_output(testcase, oracle=retrieve_url_bonus)
if __name__ == "__main__":
main(sys.argv[1:])