Skip to content

Commit 1fee60e

Browse files
committed
add unit test for chunked redirect testing
1 parent c2b41af commit 1fee60e

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

tests/chunked_redirect_test.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
5+
""" Redirection Tests """
6+
7+
from unittest import mock
8+
import mocket
9+
import adafruit_requests
10+
from chunk_test import _chunk
11+
12+
IP = "1.2.3.4"
13+
HOST = "docs.google.com"
14+
PATH = "/spreadsheets/d/e/2PACX-1vR1WjUKz35-ek6SiR5droDfvPp51MTds4wUs57vEZNh2uDfihSTPhTaiiRovLbNe1mkeRgurppRJ_Zy/pub?output=tsv"
15+
16+
# response headers returned from the initial request
17+
HEADERS_REDIRECT = (
18+
b"HTTP/1.1 307 Temporary Redirect\r\n"
19+
b"Content-Type: text/html; charset=UTF-8\r\n"
20+
b"Cache-Control: no-cache, no-store, max-age=0, must-revalidate\r\n"
21+
b"Pragma: no-cache\r\n"
22+
b"Expires: Mon, 01 Jan 1990 00:00:00 GMT\r\n"
23+
b"Date: Sat, 25 Jun 2022 21:08:48 GMT\r\n"
24+
b"Location: https://doc-14-2g-sheets.googleusercontent.com/pub/70cmver1f290kjsnpar5ku2h9g/3llvt5u8njbvat22m9l19db1h4/1656191325000"
25+
b"/109226138307867586192/*/e@2PACX-1vR1WjUKz35-ek6SiR5droDfvPp51MTds4wUs57vEZNh2uDfihSTPhTaiiRovLbNe1mkeRgurppRJ_Zy?output=tsv\r\n"
26+
b'P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info."\r\n'
27+
b"X-Content-Type-Options: nosniff\r\n"
28+
b"X-XSS-Protection: 1; mode=block\r\n"
29+
b"Server: GSE\r\n"
30+
b"Set-Cookie: NID=511=EcnO010Porg0NIrxM8tSG6MhfQiVtWrQS42CjhKEpzwIvzBj2PFYH0-H_N--EAXaPBkR2jFjAWEAxIJNqhvKb0vswOWp9hqcCrO51S8kO5I4C3"
31+
b"Is2ctWe1b_ysRU-6hjnJyLHzqjXotAWzEmr_qA3bpqWDwlRaQIiC6SvxM8L0M; expires=Sun, 25-Dec-2022 21:08:48 GMT; path=/; "
32+
b"domain=.google.com; HttpOnly\r\n"
33+
b'Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443";'
34+
b' ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"\r\n'
35+
b"Accept-Ranges: none\r\n"
36+
b"Vary: Accept-Encoding\r\n"
37+
b"Transfer-Encoding: chunked\r\n\r\n"
38+
)
39+
40+
# response body returned from the initial request (needs to be chunked.)
41+
BODY_REDIRECT = (
42+
b'<HTML>\n<HEAD>\n<TITLE>Temporary Redirect</TITLE>\n</HEAD>\n<BODY BGCOLOR="#FFFFFF" TEXT="#000000">\n'
43+
b'<H1>Temporary Redirect</H1>\nThe document has moved <A HREF="https://doc-14-2g-sheets.googleusercontent.com/pub'
44+
b"/70cmver1f290kjsnpar5ku2h9g/3llvt5u8njbvat22m9l19db1h4/1656191325000/109226138307867586192/*/e@2PACX-1vR1WjUKz35-"
45+
b'ek6SiR5droDfvPp51MTds4wUs57vEZNh2uDfihSTPhTaiiRovLbNe1mkeRgurppRJ_Zy?output=tsv">here</A>.\n</BODY>\n</HTML>\n'
46+
)
47+
48+
# response headers from the request to the redirected location
49+
HEADERS = (
50+
b"HTTP/1.1 200 OK\r\n"
51+
b"Content-Type: text/tab-separated-values\r\n"
52+
b"X-Frame-Options: ALLOW-FROM https://docs.google.com\r\n"
53+
b"X-Robots-Tag: noindex, nofollow, nosnippet\r\n"
54+
b"Expires: Sat, 25 Jun 2022 21:08:49 GMT\r\n"
55+
b"Date: Sat, 25 Jun 2022 21:08:49 GMT\r\n"
56+
b"Cache-Control: private, max-age=300\r\n"
57+
b"Content-Disposition: attachment; filename=\"WeeklyPlanner-Sheet1.tsv\"; filename*=UTF-8''Weekly%20Planner%20-%20Sheet1.tsv\r\n"
58+
b"Access-Control-Allow-Origin: *\r\n"
59+
b"Access-Control-Expose-Headers: Cache-Control,Content-Disposition,Content-Encoding,Content-Length,Content-Type,Date,Expires,Server,Transfer-Encoding\r\n"
60+
b"Content-Security-Policy: frame-ancestors 'self' https://docs.google.com\r\n"
61+
b"Content-Security-Policy: base-uri 'self';object-src 'self';report-uri https://doc-14-2g-sheets.googleusercontent.com/spreadsheets/cspreport;"
62+
b"script-src 'report-sample' 'nonce-6V57medLoq3hw2BWeyGu_A' 'unsafe-inline' 'strict-dynamic' https: http: 'unsafe-eval';worker-src 'self' blob:\r\n"
63+
b"X-Content-Type-Options: nosniff\r\n"
64+
b"X-XSS-Protection: 1; mode=block\r\n"
65+
b"Server: GSE\r\n"
66+
b'Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443";'
67+
b' ma=2592000,quic=":443"; ma=2592000; v="46,43"\r\n'
68+
b"Accept-Ranges: none\r\n"
69+
b"Vary: Accept-Encoding\r\n"
70+
b"Transfer-Encoding: chunked\r\n\r\n"
71+
)
72+
73+
# response body from the request to the redirected location (needs to be chunked.)
74+
BODY = (
75+
b"Sunday\tMonday\tTuesday\tWednesday\tThursday\tFriday\tSaturday\r\n"
76+
b"Rest\tSpin class\tRowing\tWerewolf Twitter\tWeights\tLaundry\tPoke bowl\r\n"
77+
b"\t\tZoom call\tShow & Tell\t\tMow lawn\tSynth Riders\r\n"
78+
b"\t\tTacos\tAsk an Engineer\t\tTrash pickup\t\r\n"
79+
b"\t\t\t\t\tLeg day\t\r\n"
80+
b"\t\t\t\t\tPizza night\t"
81+
)
82+
83+
84+
class MocketRecvInto(mocket.Mocket):
85+
"""A special Mocket to cap the number of bytes returned from recv_into()"""
86+
87+
def __init__(self, response):
88+
super().__init__(response)
89+
self.recv_into = mock.Mock(side_effect=self._recv_into)
90+
91+
def _recv_into(self, buf, nbytes=0):
92+
assert isinstance(nbytes, int) and nbytes >= 0
93+
read = nbytes if nbytes > 0 else len(buf)
94+
remaining = len(self._response) - self._position
95+
read = min(read, remaining, 205)
96+
end = self._position + read
97+
buf[:read] = self._response[self._position : end]
98+
self._position = end
99+
return read
100+
101+
102+
def do_test_chunked_redirect():
103+
pool = mocket.MocketPool()
104+
pool.getaddrinfo.return_value = ((None, None, None, None, (IP, 443)),)
105+
chunk = _chunk(BODY_REDIRECT, len(BODY_REDIRECT))
106+
chunk2 = _chunk(BODY, len(BODY))
107+
108+
sock1 = MocketRecvInto(HEADERS_REDIRECT + chunk)
109+
sock2 = mocket.Mocket(HEADERS + chunk2)
110+
pool.socket.side_effect = (sock1, sock2)
111+
112+
requests_session = adafruit_requests.Session(pool, mocket.SSLContext())
113+
response = requests_session.get("https://" + HOST + PATH)
114+
115+
sock1.connect.assert_called_once_with((HOST, 443))
116+
sock2.connect.assert_called_once_with(
117+
("doc-14-2g-sheets.googleusercontent.com", 443)
118+
)
119+
120+
assert response.text == str(BODY, "utf-8")
121+
122+
123+
def test_chunked_redirect():
124+
do_test_chunked_redirect()

0 commit comments

Comments
 (0)