##// END OF EJS Templates
tests: fixed stream test chunking that returned wrongly str not bytes
super-admin -
r5168:7fe32e44 default
parent child Browse files
Show More
@@ -1,136 +1,139 b''
1
1
2
2
3 # Copyright (C) 2016-2023 RhodeCode GmbH
3 # Copyright (C) 2016-2023 RhodeCode GmbH
4 #
4 #
5 # This program is free software: you can redistribute it and/or modify
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
7 # (only), as published by the Free Software Foundation.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU Affero General Public License
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
16 #
17 # This program is dual-licensed. If you wish to learn more about the
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
20
21 """
21 """
22 Checking the chunked data transfer via HTTP
22 Checking the chunked data transfer via HTTP
23 """
23 """
24
24
25 import os
25 import os
26 import time
26 import time
27 import subprocess
27 import subprocess
28
28
29 import pytest
29 import pytest
30 import requests
30 import requests
31
31
32 from rhodecode.lib.middleware.utils import scm_app_http
32 from rhodecode.lib.middleware.utils import scm_app_http
33 from rhodecode.tests.utils import wait_for_url
33 from rhodecode.tests.utils import wait_for_url
34
34
35
35
36 def test_does_chunked_end_to_end_transfer(scm_app):
36 def test_does_chunked_end_to_end_transfer(scm_app):
37 response = requests.post(scm_app, data='', stream=True)
37 response = requests.post(scm_app, data='', stream=True)
38 assert response.headers['Transfer-Encoding'] == 'chunked'
38 assert response.headers['Transfer-Encoding'] == 'chunked'
39 times = [time.time() for chunk in response.raw.read_chunked()]
39 times = [time.time() for chunk in response.raw.read_chunked()]
40 assert times[1] - times[0] > 0.1, "Chunks arrived at the same time"
40 assert times[1] - times[0] > 0.1, "Chunks arrived at the same time"
41
41
42
42
43 SCM_APP_URL_TMPL = 'http://0.0.0.0:{port}'
44
45
43 @pytest.fixture()
46 @pytest.fixture()
44 def echo_app_chunking(request, available_port_factory):
47 def echo_app_chunking(request, available_port_factory):
45 """
48 """
46 Run the EchoApp via Waitress in a subprocess.
49 Run the EchoApp via Waitress in a subprocess.
47
50
48 Return the URL endpoint to reach the app.
51 Return the URL endpoint to reach the app.
49 """
52 """
50 port = available_port_factory()
53 port = available_port_factory()
51 command = (
54 command = (
52 'waitress-serve --send-bytes 1 --port {port} --call '
55 'waitress-serve --send-bytes 1 --port {port} --call '
53 'rhodecode.tests.lib.middleware.utils.test_scm_app_http_chunking'
56 'rhodecode.tests.lib.middleware.utils.test_scm_app_http_chunking'
54 ':create_echo_app')
57 ':create_echo_app')
55 command = command.format(port=port)
58 command = command.format(port=port)
56 proc = subprocess.Popen(command.split(' '), bufsize=0)
59 proc = subprocess.Popen(command.split(' '), bufsize=0)
57 echo_app_url = 'http://localhost:' + str(port)
60 echo_app_url = SCM_APP_URL_TMPL.format(port=port)
58
61
59 @request.addfinalizer
62 @request.addfinalizer
60 def stop_echo_app():
63 def stop_echo_app():
61 proc.kill()
64 proc.kill()
62
65
63 return echo_app_url
66 return echo_app_url
64
67
65
68
66 @pytest.fixture()
69 @pytest.fixture()
67 def scm_app(request, available_port_factory, echo_app_chunking):
70 def scm_app(request, available_port_factory, echo_app_chunking):
68 """
71 """
69 Run the scm_app in Waitress.
72 Run the scm_app in Waitress.
70
73
71 Returns the URL endpoint where this app can be reached.
74 Returns the URL endpoint where this app can be reached.
72 """
75 """
73 port = available_port_factory()
76 port = available_port_factory()
74 command = (
77 command = (
75 'waitress-serve --send-bytes 1 --port {port} --call '
78 'waitress-serve --send-bytes 1 --port {port} --call '
76 'rhodecode.tests.lib.middleware.utils.test_scm_app_http_chunking'
79 'rhodecode.tests.lib.middleware.utils.test_scm_app_http_chunking'
77 ':create_scm_app')
80 ':create_scm_app')
78 command = command.format(port=port)
81 command = command.format(port=port)
79 env = os.environ.copy()
82 env = os.environ.copy()
80 env["RC_ECHO_URL"] = echo_app_chunking
83 env["RC_ECHO_URL"] = echo_app_chunking
81 proc = subprocess.Popen(command.split(' '), bufsize=0, env=env)
84 proc = subprocess.Popen(command.split(' '), bufsize=0, env=env)
82 scm_app_url = 'http://localhost:' + str(port)
85 scm_app_url = SCM_APP_URL_TMPL.format(port=port)
83 wait_for_url(scm_app_url)
86 wait_for_url(scm_app_url)
84
87
85 @request.addfinalizer
88 @request.addfinalizer
86 def stop_echo_app():
89 def stop_echo_app():
87 proc.kill()
90 proc.kill()
88
91
89 return scm_app_url
92 return scm_app_url
90
93
91
94
92 class EchoApp(object):
95 class EchoApp(object):
93 """
96 """
94 Stub WSGI application which returns a chunked response to every request.
97 Stub WSGI application which returns a chunked response to every request.
95 """
98 """
96
99
97 def __init__(self, repo_path, repo_name, config):
100 def __init__(self, repo_path, repo_name, config):
98 self._repo_path = repo_path
101 self._repo_path = repo_path
99
102
100 def __call__(self, environ, start_response):
103 def __call__(self, environ, start_response):
101 environ['wsgi.input'].read()
104 environ['wsgi.input'].read()
102 status = '200 OK'
105 status = '200 OK'
103 headers = []
106 headers = []
104 start_response(status, headers)
107 start_response(status, headers)
105 return result_generator()
108 return result_generator()
106
109
107
110
108 def result_generator():
111 def result_generator():
109 """
112 """
110 Simulate chunked results.
113 Simulate chunked results.
111
114
112 The intended usage is to simulate a chunked response as we would get it
115 The intended usage is to simulate a chunked response as we would get it
113 out of a vcs operation during a call to "hg clone".
116 out of a vcs operation during a call to "hg clone".
114 """
117 """
115 yield 'waiting 2 seconds'
118 yield b'waiting 2 seconds'
116 # Wait long enough so that the first chunk can go out
119 # Wait long enough so that the first chunk can go out
117 time.sleep(2)
120 time.sleep(2)
118 yield 'final chunk'
121 yield b'final chunk'
119 # Another small wait, otherwise they go together
122 # Another small wait, otherwise they go together
120 time.sleep(0.1)
123 time.sleep(0.1)
121
124
122
125
123 def create_echo_app():
126 def create_echo_app():
124 """
127 """
125 Create EchoApp filled with stub data.
128 Create EchoApp filled with stub data.
126 """
129 """
127 return EchoApp('stub_path', 'repo_name', {})
130 return EchoApp('stub_path', 'repo_name', {})
128
131
129
132
130 def create_scm_app():
133 def create_scm_app():
131 """
134 """
132 Create a scm_app hooked up to speak to EchoApp.
135 Create a scm_app hooked up to speak to EchoApp.
133 """
136 """
134 echo_app_url = os.environ["RC_ECHO_URL"]
137 echo_app_url = os.environ["RC_ECHO_URL"]
135 return scm_app_http.VcsHttpProxy(
138 return scm_app_http.VcsHttpProxy(
136 echo_app_url, 'stub_path', 'stub_name', None)
139 echo_app_url, 'stub_path', 'stub_name', None)
General Comments 0
You need to be logged in to leave comments. Login now