Skip to content

Commit e011f27

Browse files
Copilotmindflayer
andauthored
Restore doctests in mockhttp.py (#318)
* Initial plan * Restore doctests in mockhttp.py for set_extra_headers, can_handle, and _parse_requestline Co-authored-by: mindflayer <527325+mindflayer@users.noreply.github.com> * Add coverage.xml to .gitignore and remove from tracking Co-authored-by: mindflayer <527325+mindflayer@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: mindflayer <527325+mindflayer@users.noreply.github.com>
1 parent a9ad9f5 commit e011f27

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ shippable
2828
.vscode/
2929
Pipfile.lock
3030
requirements.txt
31+
coverage.xml

mocket/mocks/mockhttp.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,19 @@ def set_base_headers(self) -> None:
182182
self.headers["Content-Type"] = do_the_magic(self.body)
183183

184184
def set_extra_headers(self, headers: dict) -> None:
185-
"""Add extra headers to the response.
185+
r"""Add extra headers to the response.
186186
187187
Args:
188188
headers: Dictionary of additional headers
189+
190+
>>> r = Response(body="<html />")
191+
>>> len(r.headers.keys())
192+
6
193+
>>> r.set_extra_headers({"foo-bar": "Foobar"})
194+
>>> len(r.headers.keys())
195+
7
196+
>>> encode_to_bytes(r.headers.get("Foo-Bar")) == encode_to_bytes("Foobar")
197+
True
189198
"""
190199
for k, v in headers.items():
191200
self.headers["-".join(token.capitalize() for token in k.split("-"))] = v
@@ -294,13 +303,20 @@ def _can_handle(self, path: str, qs_dict: dict) -> bool:
294303
return can_handle
295304

296305
def can_handle(self, data: bytes) -> bool:
297-
"""Check if this entry can handle the given request data.
306+
r"""Check if this entry can handle the given request data.
298307
299308
Args:
300309
data: Request data
301310
302311
Returns:
303312
True if this entry can handle the request
313+
314+
>>> e = Entry('http://www.github.com/?bar=foo&foobar', Entry.GET, (Response(b'<html/>'),))
315+
>>> e.can_handle(b'GET /?bar=foo HTTP/1.1\r\nHost: github.com\r\nAccept-Encoding: gzip, deflate\r\nConnection: keep-alive\r\nUser-Agent: python-requests/2.7.0 CPython/3.4.3 Linux/3.19.0-16-generic\r\nAccept: */*\r\n\r\n')
316+
False
317+
>>> e = Entry('http://www.github.com/?bar=foo&foobar', Entry.GET, (Response(b'<html/>'),))
318+
>>> e.can_handle(b'GET /?bar=foo&foobar HTTP/1.1\r\nHost: github.com\r\nAccept-Encoding: gzip, deflate\r\nConnection: keep-alive\r\nUser-Agent: python-requests/2.7.0 CPython/3.4.3 Linux/3.19.0-16-generic\r\nAccept: */*\r\n\r\n')
319+
True
304320
"""
305321
try:
306322
requestline, _ = decode_from_bytes(data).split(CRLF, 1)
@@ -322,6 +338,8 @@ def can_handle(self, data: bytes) -> bool:
322338
def _parse_requestline(line: str) -> tuple:
323339
"""Parse an HTTP request line.
324340
341+
http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5
342+
325343
Args:
326344
line: HTTP request line string
327345
@@ -330,6 +348,15 @@ def _parse_requestline(line: str) -> tuple:
330348
331349
Raises:
332350
ValueError: If line is not a valid request line
351+
352+
>>> Entry._parse_requestline('GET / HTTP/1.0') == ('GET', '/', '1.0')
353+
True
354+
>>> Entry._parse_requestline('post /testurl htTP/1.1') == ('POST', '/testurl', '1.1')
355+
True
356+
>>> Entry._parse_requestline('Im not a RequestLine')
357+
Traceback (most recent call last):
358+
...
359+
ValueError: Not a Request-Line
333360
"""
334361
m = re.match(
335362
r"({})\s+(.*)\s+HTTP/(1.[0|1])".format("|".join(Entry.METHODS)), line, re.I

0 commit comments

Comments
 (0)