|
| 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