1# -*- coding: utf-8 -*- 2from tests.test_stack import TestConfig, app_from_config 3from tg.util import no_warn 4from tg._compat import u_ 5 6def setup_noDB(html_flash=False): 7 config = {'use_sqlalchemy': False, 8 'use_toscawidgets': False, 9 'use_toscawidgets2': False, 10 'ignore_parameters': ["ignore", "ignore_me"]} 11 12 if html_flash: 13 config['flash.allow_html'] = True 14 15 base_config = TestConfig(folder='dispatch', 16 values=config) 17 return app_from_config(base_config) 18 19 20app = None 21def setup(): 22 global app 23 app = setup_noDB() 24 25@no_warn #should be _default now 26def test_tg_style_default(): 27 resp = app.get('/sdfaswdfsdfa') #random string should be caught by the default route 28 assert 'Default' in resp.body.decode('utf-8') 29 30def test_url_encoded_param_passing(): 31 resp = app.get('/feed?feed=http%3A%2F%2Fdeanlandolt.com%2Ffeed%2Fatom%2F') 32 assert "http://deanlandolt.com/feed/atom/" in resp.body.decode('utf-8') 33 34def test_tg_style_index(): 35 resp = app.get('/index/') 36 assert 'hello' in resp.body.decode('utf-8'), resp 37 38def test_tg_style_subcontroller_index(): 39 resp = app.get('/sub/index') 40 assert "sub index" in resp.body.decode('utf-8') 41 42def test_tg_style_subcontroller_default(): 43 resp=app.get('/sub/bob/tim/joe') 44 assert 'bob' in resp.body.decode('utf-8'), resp 45 assert 'tim' in resp.body.decode('utf-8'), resp 46 assert 'joe' in resp.body.decode('utf-8'), resp 47 48def test_redirect_absolute(): 49 resp = app.get('/redirect_me?target=/') 50 assert resp.status == "302 Found", resp.status 51 assert 'http://localhost/' in resp.headers['location'] 52 resp = resp.follow() 53 assert 'hello world' in resp, resp 54 55@no_warn 56def test_redirect_relative(): 57 resp = app.get('/redirect_me?target=hello&name=abc') 58 resp = resp.follow() 59 assert 'Hello abc' in resp, resp 60 resp = app.get('/sub/redirect_me?target=hello&name=def') 61 resp = resp.follow() 62 assert 'Why HELLO! def' in resp, resp 63 resp = app.get('/sub/redirect_me?target=../hello&name=ghi') 64 resp = resp.follow() 65 assert 'Hello ghi' in resp, resp 66 67def test_redirect_external(): 68 resp = app.get('/redirect_me?target=http://example.com') 69 assert resp.status == "302 Found" and resp.headers['location'] == 'http://example.com', resp 70 71def test_redirect_param(): 72 resp = app.get('/redirect_me?target=/hello&name=paj') 73 resp = resp.follow() 74 assert 'Hello paj' in resp, resp 75 resp = app.get('/redirect_me?target=/hello&name=pbj') 76 resp = resp.follow() 77 assert 'Hello pbj' in resp, resp 78 resp = app.get('/redirect_me?target=/hello&silly=billy&name=pcj') 79 resp = resp.follow() 80 assert 'Hello pcj' in resp, resp 81 82def test_redirect_cookie(): 83 resp = app.get('/redirect_cookie?name=stefanha').follow() 84 assert 'Hello stefanha' in resp 85 86def test_subcontroller_redirect_subindex(): 87 resp=app.get('/sub/redirect_sub').follow() 88 assert 'sub index' in resp 89 90def test_subcontroller_redirect_sub2index(): 91 resp=app.get('/sub2/').follow() 92 assert 'hello list' in resp 93 94#this test does not run because of some bug in nose 95def _test_subcontroller_lookup(): 96 resp=app.get('/sub2/findme').follow() 97 assert 'lookup' in resp, resp 98 99def test_subcontroller_redirect_no_slash_sub2index(): 100 resp=app.get('/sub2/').follow() 101 assert 'hello list' in resp, resp 102 103def test_redirect_to_list_of_strings(): 104 resp = app.get('/sub/redirect_list').follow() 105 assert 'hello list' in resp, resp 106 107def test_flash_redirect(): 108 resp = app.get('/flash_redirect').follow() 109 assert 'Wow, <br/>flash!' in resp, resp 110 111def test_bigflash_redirect(): 112 try: 113 resp = app.get('/bigflash_redirect') 114 assert False 115 except Exception as e: 116 assert 'Flash value is too long (cookie would be >4k)' in str(e) 117 118def test_flash_no_redirect(): 119 resp = app.get('/flash_no_redirect') 120 assert 'Wow, flash!' in resp, resp 121 122def test_flash_unicode(): 123 resp = app.get('/flash_unicode').follow() 124 content = resp.body.decode('utf8') 125 assert u_('Привет, мир!') in content, content 126 127def test_flash_status(): 128 resp = app.get('/flash_status') 129 assert 'ok' in resp, resp 130 131def test_flash_javascript(): 132 resp = app.get('/flash_render?using_js=True') 133 expected = 'webflash({"id": "flash", "name": "webflash"})' 134 epxected_reverse = 'webflash({"name": "webflash", "id": "flash"})' 135 assert expected in resp or epxected_reverse in resp, resp 136 assert 'webflash.render()' in resp, resp 137 138def test_flash_render_plain(): 139 resp = app.get('/flash_render') 140 assert 'JS <br/>Flash' in resp, resp 141 142def test_flash_render_plain_with_html(): 143 app = setup_noDB(html_flash=True) 144 resp = app.get('/flash_render') 145 assert 'JS <br/>Flash' in resp, resp 146 147def test_flash_render_no_message(): 148 resp = app.get('/flash_render?with_message=False') 149 assert 'flash' not in resp 150 151def test_custom_content_type(): 152 resp = app.get('/custom_content_type') 153 assert 'image/png' == dict(resp.headers)['Content-Type'], resp 154 assert resp.body.decode('utf-8') == 'PNG', resp 155 156def test_custom_text_plain_content_type(): 157 resp = app.get('/custom_content_text_plain_type') 158 assert 'text/plain; charset=utf-8' == dict(resp.headers)['Content-Type'], resp 159 assert resp.body.decode('utf-8') == """a<br/>bx""", resp 160 161@no_warn 162def test_custom_content_type2(): 163 resp = app.get('/custom_content_type2') 164 assert 'image/png' == dict(resp.headers)['Content-Type'], resp 165 assert resp.body.decode('utf-8') == 'PNG2', resp 166 167@no_warn 168def test_basicurls(): 169 resp = app.get("/test_url_sop") 170 171def test_ignore_parameters(): 172 resp = app.get("/check_params?ignore='bar'&ignore_me='foo'") 173 assert "None recieved" in resp.text, resp.text 174 175def test_https_redirect(): 176 resp = app.get("/test_https?foo=bar&baz=bat") 177 assert 'https://' in resp, resp 178 assert resp.location.endswith("/test_https?foo=bar&baz=bat") 179 resp = app.post("/test_https?foo=bar&baz=bat", status=405) 180 181def test_return_non_string(): 182 try: 183 resp = app.get("/return_something") 184 assert False 185 except: 186 # Do not try to be too smart catching all cases 187 # if returned value is not a valid wsgi app_iter 188 # let it crash 189 pass 190 191def test_return_none(): 192 resp = app.get('/return_none', status=204) 193 assert 'Content-Type' not in str(resp), resp 194 195def test_return_modified_response(): 196 resp = app.get('/return_modified_response', status=201) 197 assert 'Hello World' in resp.text 198 199class TestVisits(object): 200 def test_visit_path_sub1(self): 201 resp = app.get("/sub/hitme") 202 assert str(resp).endswith('/sub@/sub') 203 204 def test_visit_path_nested(self): 205 resp = app.get("/sub/nested/hitme") 206 assert str(resp).endswith('/sub/nested*/sub/nested') 207 208 def test_visit_path_nested_index(self): 209 resp = app.get("/sub/nested") 210 assert str(resp).endswith('/sub/nested-/sub/nested') 211 212 def test_runtime_visit_path_subcontroller(self): 213 resp = app.get("/sub/nested/nested/hitme") 214 assert str(resp).endswith('*/sub/nested') 215 216 def test_runtime_visit_path(self): 217 resp = app.get("/sub/nested/hiddenhitme") 218 assert str(resp).endswith(' /sub/nested') 219