Show More
@@ -1,130 +1,131 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2016-2016 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | import mock |
|
22 | 22 | import Pyro4 |
|
23 | 23 | import pytest |
|
24 | 24 | import webtest |
|
25 | 25 | |
|
26 | 26 | from rhodecode.lib.middleware.utils import scm_app_http, scm_app |
|
27 | from rhodecode.lib.middleware.simplegit import SimpleGit | |
|
27 | 28 | from rhodecode.lib.vcs.conf import settings |
|
28 | 29 | |
|
29 | 30 | |
|
30 | 31 | def vcs_http_app(vcsserver_http_echo_app): |
|
31 | 32 | """ |
|
32 | 33 | VcsHttpProxy wrapped in WebTest. |
|
33 | 34 | """ |
|
34 | 35 | git_url = vcsserver_http_echo_app.http_url + 'stream/git/' |
|
35 | 36 | vcs_http_proxy = scm_app_http.VcsHttpProxy( |
|
36 | 37 | git_url, 'stub_path', 'stub_name', None, 'stub_backend') |
|
37 | 38 | app = webtest.TestApp(vcs_http_proxy) |
|
38 | 39 | return app |
|
39 | 40 | |
|
40 | 41 | |
|
41 | 42 | @pytest.fixture(scope='module') |
|
42 | 43 | def vcsserver_http_echo_app(request, vcsserver_factory): |
|
43 | 44 | """ |
|
44 | 45 | A running VCSServer with the EchoApp activated via HTTP. |
|
45 | 46 | """ |
|
46 | 47 | vcsserver = vcsserver_factory( |
|
47 | 48 | request=request, |
|
48 | 49 | use_http=True, |
|
49 | 50 | overrides=[{'app:main': {'dev.use_echo_app': 'true'}}]) |
|
50 | 51 | return vcsserver |
|
51 | 52 | |
|
52 | 53 | |
|
53 | 54 | @pytest.fixture(scope='session') |
|
54 | 55 | def data(): |
|
55 | 56 | one_kb = "x" * 1024 |
|
56 | 57 | return one_kb * 1024 * 10 |
|
57 | 58 | |
|
58 | 59 | |
|
59 | 60 | def test_reuse_app_no_data(repeat, vcsserver_http_echo_app): |
|
60 | 61 | app = vcs_http_app(vcsserver_http_echo_app) |
|
61 | 62 | for x in xrange(repeat / 10): |
|
62 | 63 | response = app.post('/') |
|
63 | 64 | assert response.status_code == 200 |
|
64 | 65 | |
|
65 | 66 | |
|
66 | 67 | def test_reuse_app_with_data(data, repeat, vcsserver_http_echo_app): |
|
67 | 68 | app = vcs_http_app(vcsserver_http_echo_app) |
|
68 | 69 | for x in xrange(repeat / 10): |
|
69 | 70 | response = app.post('/', params=data) |
|
70 | 71 | assert response.status_code == 200 |
|
71 | 72 | |
|
72 | 73 | |
|
73 | 74 | def test_create_app_per_request_no_data(repeat, vcsserver_http_echo_app): |
|
74 | 75 | for x in xrange(repeat / 10): |
|
75 | 76 | app = vcs_http_app(vcsserver_http_echo_app) |
|
76 | 77 | response = app.post('/') |
|
77 | 78 | assert response.status_code == 200 |
|
78 | 79 | |
|
79 | 80 | |
|
80 | 81 | def test_create_app_per_request_with_data( |
|
81 | 82 | data, repeat, vcsserver_http_echo_app): |
|
82 | 83 | for x in xrange(repeat / 10): |
|
83 | 84 | app = vcs_http_app(vcsserver_http_echo_app) |
|
84 | 85 | response = app.post('/', params=data) |
|
85 | 86 | assert response.status_code == 200 |
|
86 | 87 | |
|
87 | 88 | |
|
88 | 89 | @pytest.fixture(scope='module') |
|
89 | 90 | def vcsserver_pyro_echo_app(request, vcsserver_factory): |
|
90 | 91 | """ |
|
91 | 92 | A running VCSServer with the EchoApp activated via Pyro4. |
|
92 | 93 | """ |
|
93 | 94 | vcsserver = vcsserver_factory( |
|
94 | 95 | request=request, |
|
95 | 96 | use_http=False, |
|
96 | 97 | overrides=[{'DEFAULT': {'dev.use_echo_app': 'true'}}]) |
|
97 | 98 | return vcsserver |
|
98 | 99 | |
|
99 | 100 | |
|
100 | 101 | def vcs_pyro4_app(vcsserver_pyro_echo_app): |
|
101 | 102 | """ |
|
102 | 103 | Pyro4 based Vcs proxy wrapped in WebTest |
|
103 | 104 | """ |
|
104 | 105 | stub_config = { |
|
105 | 106 | 'git_update_server_info': 'stub', |
|
106 | 107 | } |
|
107 | 108 | server_and_port = vcsserver_pyro_echo_app.server_and_port |
|
108 | 109 | GIT_REMOTE_WSGI = Pyro4.Proxy( |
|
109 | 110 | settings.pyro_remote( |
|
110 | 111 | settings.PYRO_GIT_REMOTE_WSGI, server_and_port)) |
|
111 | 112 | with mock.patch('rhodecode.lib.middleware.utils.scm_app.GIT_REMOTE_WSGI', |
|
112 | 113 | GIT_REMOTE_WSGI): |
|
113 | 114 | pyro4_app = scm_app.create_git_wsgi_app( |
|
114 | 'stub_path', 'stub_name', stub_config) | |
|
115 | 'stub_path', 'stub_name', stub_config, SimpleGit.SCM) | |
|
115 | 116 | app = webtest.TestApp(pyro4_app) |
|
116 | 117 | return app |
|
117 | 118 | |
|
118 | 119 | |
|
119 | 120 | def test_pyro4_no_data(repeat, pylonsapp, vcsserver_pyro_echo_app): |
|
120 | 121 | for x in xrange(repeat / 10): |
|
121 | 122 | app = vcs_pyro4_app(vcsserver_pyro_echo_app) |
|
122 | 123 | response = app.post('/') |
|
123 | 124 | assert response.status_code == 200 |
|
124 | 125 | |
|
125 | 126 | |
|
126 | 127 | def test_pyro4_with_data(repeat, pylonsapp, vcsserver_pyro_echo_app, data): |
|
127 | 128 | for x in xrange(repeat / 10): |
|
128 | 129 | app = vcs_pyro4_app(vcsserver_pyro_echo_app) |
|
129 | 130 | response = app.post('/', params=data) |
|
130 | 131 | assert response.status_code == 200 |
@@ -1,98 +1,102 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2010-2016 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | import webtest |
|
22 | 22 | |
|
23 | 23 | from rhodecode.lib.middleware.utils import wsgi_app_caller_client |
|
24 | 24 | |
|
25 | 25 | # pylint: disable=protected-access,too-many-public-methods |
|
26 | 26 | |
|
27 | 27 | |
|
28 | 28 | BASE_ENVIRON = { |
|
29 | 29 | 'REQUEST_METHOD': 'GET', |
|
30 | 30 | 'SERVER_NAME': 'localhost', |
|
31 | 31 | 'SERVER_PORT': '80', |
|
32 | 32 | 'SCRIPT_NAME': '', |
|
33 | 33 | 'PATH_INFO': '/', |
|
34 | 34 | 'QUERY_STRING': '', |
|
35 | 35 | 'foo.bool_var': True, |
|
36 | 36 | 'foo.str_var': 'True', |
|
37 | 37 | 'wsgi.foo': True, |
|
38 | 38 | # Some non string values. The validator expects to get an iterable as |
|
39 | 39 | # value. |
|
40 | 40 | (42,): '42', |
|
41 | 41 | (True,): 'False', |
|
42 | 42 | } |
|
43 | 43 | |
|
44 | 44 | |
|
45 | 45 | def assert_all_values_are_str(environ): |
|
46 | 46 | """Checks that all values of a dict are str.""" |
|
47 | 47 | for key, value in environ.iteritems(): |
|
48 | 48 | assert isinstance(value, str), ( |
|
49 | 49 | "Value for key %s: has type %s but 'str' was expected. Value: %s" % |
|
50 | 50 | (key, type(value), repr(value))) |
|
51 | 51 | |
|
52 | 52 | |
|
53 | 53 | def assert_all_keys_are_str(environ): |
|
54 | 54 | """Checks that all keys of a dict are str.""" |
|
55 | 55 | for key, value in environ.iteritems(): |
|
56 | 56 | assert isinstance(value, str), ( |
|
57 | 57 | "Key %s: has type %s but 'str' was expected. " % |
|
58 | 58 | (repr(key), type(key))) |
|
59 | 59 | |
|
60 | 60 | |
|
61 | 61 | def assert_no_prefix_in_keys(environ, prefix): |
|
62 | 62 | """Checks that no key of the dict starts with the prefix.""" |
|
63 | 63 | for key in environ: |
|
64 | 64 | assert not key.startswith(prefix), 'Key %s should not be present' % key |
|
65 | 65 | |
|
66 | 66 | |
|
67 | 67 | def test_get_environ(): |
|
68 | 68 | clean_environ = wsgi_app_caller_client._get_clean_environ(BASE_ENVIRON) |
|
69 | 69 | |
|
70 | 70 | assert len(clean_environ) == 7 |
|
71 | 71 | assert_no_prefix_in_keys(clean_environ, 'wsgi.') |
|
72 | 72 | assert_all_keys_are_str(clean_environ) |
|
73 | 73 | assert_all_values_are_str(clean_environ) |
|
74 | 74 | |
|
75 | 75 | |
|
76 | 76 | def test_remote_app_caller(): |
|
77 | 77 | |
|
78 | 78 | class RemoteAppCallerMock(object): |
|
79 | 79 | |
|
80 | 80 | def handle(self, environ, input_data, arg1, arg2, |
|
81 | 81 | arg3=None, arg4=None, arg5=None): |
|
82 | 82 | assert ((arg1, arg2, arg3, arg4, arg5) == |
|
83 | 83 | ('a1', 'a2', 'a3', 'a4', None)) |
|
84 | 84 | # Note: RemoteAppCaller is expected to return a tuple like the |
|
85 | 85 | # following one |
|
86 | 86 | return (['content'], '200 OK', [('Content-Type', 'text/plain')]) |
|
87 | 87 | |
|
88 | 88 | wrapper_app = wsgi_app_caller_client.RemoteAppCaller( |
|
89 |
RemoteAppCallerMock(), ' |
|
|
89 | RemoteAppCallerMock(), 'dummy-backend', | |
|
90 | 'a1', 'a2', arg3='a3', arg4='a4') | |
|
90 | 91 | |
|
91 | 92 | test_app = webtest.TestApp(wrapper_app) |
|
92 | 93 | |
|
93 | 94 | response = test_app.get('/path') |
|
94 | 95 | |
|
95 | 96 | assert response.status == '200 OK' |
|
96 | assert response.headers.items() == [ | |
|
97 | ('Content-Type', 'text/plain'), ('Content-Length', '7')] | |
|
97 | assert sorted(response.headers.items()) == sorted([ | |
|
98 | ('X-RhodeCode-Backend', 'dummy-backend'), | |
|
99 | ('Content-Type', 'text/plain'), | |
|
100 | ('Content-Length', '7'), | |
|
101 | ]) | |
|
98 | 102 | assert response.body == 'content' |
General Comments 0
You need to be logged in to leave comments.
Login now