Skip to content

Commit 600b3e7

Browse files
[Jupyter] Add a response notebook (#1064)
* Add a response generation jupyter notebook * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Make codespell happy * precommit codespell exclude Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 32a0759 commit 600b3e7

File tree

3 files changed

+447
-2
lines changed

3 files changed

+447
-2
lines changed

.pre-commit-config.yaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,12 @@ repos:
125125
rev: v2.1.0
126126
hooks:
127127
- id: codespell
128-
exclude: >-
129-
^.+\.min\.js$
128+
exclude: >
129+
(?x)^(
130+
tutorial/responses.ipynb|
131+
tests/http/test_responses\.py|
132+
^.+\.min\.js$
133+
)$
130134
131135
- repo: https://github.com/adrienverge/yamllint.git
132136
rev: v1.26.2

tests/http/test_responses.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
proxy.py
4+
~~~~~~~~
5+
⚡⚡⚡ Fast, Lightweight, Pluggable, TLS interception capable proxy server focused on
6+
Network monitoring, controls & Application development, testing, debugging.
7+
8+
:copyright: (c) 2013-present by Abhinav Singh and contributors.
9+
:license: BSD, see LICENSE for more details.
10+
11+
.. spelling::
12+
13+
nd
14+
"""
15+
import gzip
16+
17+
import unittest
18+
19+
from proxy.http.parser import ChunkParser
20+
from proxy.http.responses import okResponse
21+
from proxy.common.constants import CRLF
22+
23+
24+
class TestResponses(unittest.TestCase):
25+
26+
def test_basic(self) -> None:
27+
self.assertEqual(
28+
okResponse(),
29+
b'HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n',
30+
)
31+
self.assertEqual(
32+
okResponse(
33+
headers={
34+
b'X-Custom-Header': b'my value',
35+
},
36+
),
37+
b'HTTP/1.1 200 OK\r\nX-Custom-Header: my value\r\nContent-Length: 0\r\n\r\n',
38+
)
39+
self.assertEqual(
40+
okResponse(
41+
content=b'Hello World',
42+
headers={
43+
b'X-Custom-Header': b'my value',
44+
},
45+
),
46+
b'HTTP/1.1 200 OK\r\nX-Custom-Header: my value\r\nContent-Length: 11\r\n\r\nHello World',
47+
)
48+
49+
def test_compression(self) -> None:
50+
content = b'H' * 21
51+
self.assertEqual(
52+
gzip.decompress(
53+
okResponse(
54+
content=content,
55+
headers={
56+
b'X-Custom-Header': b'my value',
57+
},
58+
).tobytes().split(CRLF + CRLF, maxsplit=1)[-1],
59+
),
60+
content,
61+
)
62+
self.assertEqual(
63+
okResponse(
64+
content=content,
65+
headers={
66+
b'Host': b'jaxl.com',
67+
},
68+
min_compression_length=len(content),
69+
),
70+
b'HTTP/1.1 200 OK\r\nHost: jaxl.com\r\nContent-Length: 21\r\n\r\nHHHHHHHHHHHHHHHHHHHHH',
71+
)
72+
73+
def test_close_header(self) -> None:
74+
self.assertEqual(
75+
okResponse(
76+
content=b'Hello World',
77+
headers={
78+
b'Host': b'jaxl.com',
79+
},
80+
conn_close=True,
81+
),
82+
b'HTTP/1.1 200 OK\r\nHost: jaxl.com\r\nContent-Length: 11\r\nConnection: close\r\n\r\nHello World',
83+
)
84+
85+
def test_chunked_without_compression(self) -> None:
86+
chunks = ChunkParser.to_chunks(b'Hello World', chunk_size=5)
87+
self.assertEqual(
88+
okResponse(
89+
content=chunks,
90+
headers={
91+
b'Transfer-Encoding': b'chunked',
92+
},
93+
# Avoid compressing chunks for demo purposes here
94+
# Ideally you should omit this flag and send
95+
# compressed chunks.
96+
min_compression_length=len(chunks),
97+
),
98+
b'HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nHello\r\n5\r\n Worl\r\n1\r\nd\r\n0\r\n\r\n',
99+
)
100+
101+
def test_chunked_with_compression(self) -> None:
102+
chunks = ChunkParser.to_chunks(b'Hello World', chunk_size=5)
103+
self.assertEqual(
104+
gzip.decompress(
105+
okResponse(
106+
content=chunks,
107+
headers={
108+
b'Transfer-Encoding': b'chunked',
109+
},
110+
).tobytes().split(CRLF + CRLF, maxsplit=1)[-1],
111+
),
112+
chunks,
113+
)

0 commit comments

Comments
 (0)