Skip to content

Commit cb65b78

Browse files
committed
Fix get body issue and move set response out of common handler
1 parent ead8afd commit cb65b78

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

mock_servers/simple_endpoint.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,25 @@ def do_OPTIONS(self):
4646
request_headers[header[0]] = header[1]
4747
self.wfile.write("")
4848

49-
def common_handler(self):
49+
"""
50+
- 1xx: Informational - Request received, continuing process
51+
- 2xx: Success - The action was successfully received,
52+
understood, and accepted
53+
- 3xx: Redirection - Further action must be taken in order to
54+
complete the request
55+
- 4xx: Client Error - The request contains bad syntax or cannot
56+
be fulfilled
57+
- 5xx: Server Error - The server failed to fulfill an apparently
58+
valid request
59+
"""
60+
61+
def setStatusCode(self):
5062
# self.send_response(HTTPStatus.FORBIDDEN)
51-
# self.send_response(HTTPStatus.INTERNAL_SERVER_ERROR)
52-
self.send_response(HTTPStatus.OK)
63+
self.send_response(HTTPStatus.INTERNAL_SERVER_ERROR, "Wolaaa")
64+
# self.send_response(HTTPStatus.OK)
65+
66+
def common_handler(self):
67+
self.setStatusCode()
5368
# print(self.path)
5469
auth_params = self.decodeAuthHeader()
5570
"""
@@ -112,10 +127,11 @@ def run():
112127
"""
113128

114129
def getBody(self):
130+
blocking = False
115131
content_length = int(self.headers.get("content-length", -1))
116132
content_type = self.headers.get("content-type", -1)
117133
method = self.command
118-
if content_length == -1 and method in ["POST", "PUT", "PATCH"] and content_type != -1:
134+
if blocking and content_length == -1 and method in ["POST", "PUT", "PATCH"] and content_type != -1:
119135
# Just giving a try here, The `content-length` header could be missing in case of HTTP1.1 chunked transfer encoding state
120136
# So giving it a try, If there is no data while satisfying the above condition , the rfile.readline() will hang!!!
121137
request_body = ""
@@ -125,7 +141,7 @@ def getBody(self):
125141
break
126142
request_body += line
127143
return request_body
128-
return None if content_length == 0 else self.rfile.read(content_length).decode("UTF-8")
144+
return None if content_length in [0, -1] else self.rfile.read(content_length).decode("UTF-8")
129145

130146
def getDigestAuth(self):
131147
www_authenticate_header = python_digest.build_digest_challenge(

0 commit comments

Comments
 (0)