def test_template_filters(self): with file(os.path.join(self.app_dir, 'startup.py'), 'w') as fp: fp.write('from keystone.render import template_filter\n') fp.write('@template_filter\n') fp.write('def silly(value):\n') fp.write(' return "silly"\n') with file(os.path.join(self.app_dir, 'index.ks'), 'w') as fp: fp.write('{{novar|silly}}') app = Keystone(self.app_dir) req = Request(wsgi_environ('GET', '/')) response = app.dispatch(req) output = ''.join(response.response) self.assertTrue(isinstance(response, BaseResponse)) self.assertEqual(output, 'silly')
def test_dispatch(self): index = os.path.join(self.app_dir, 'index.ks') static = os.path.join(self.app_dir, 'base.css') app = Keystone(self.app_dir) req = Request(wsgi_environ('GET', '/')) response = app.dispatch(req) self.assertTrue(isinstance(response, BaseResponse), 'dispatch on missing did not return a Response') self.assertEqual(response.status_code, 404, 'missing did not get a 404 HTTPException (got %d)' % response.status_code) changer = util.MtimeChanger() with changer.change_times(file(index, 'w')) as fp: fp.write('raise Exception("blah")\n----\n<strong>this is HTML</strong>\n') response = app.dispatch(req) self.assertTrue(isinstance(response, BaseResponse), 'dispatch on exception did not return a Response') self.assertEqual(response.status_code, 500, 'error did not get a 500 HTTPException (got %d)' % response.status_code) with changer.change_times(file(index, 'w')) as fp: fp.write('raise http.SeeOther("/foo")\n----\n<strong>this is HTML</strong>\n') response = app.dispatch(req) self.assertTrue(isinstance(response, BaseResponse), 'dispatch on HTTPException did not return a Response') self.assertEqual(response.status_code, 303, 'redirect (see other) did not get a 303 HTTPException (got %d)' % response.status_code) with changer.change_times(file(index, 'w')) as fp: fp.write('<strong>this is HTML</strong>\n') response = app.dispatch(req) self.assertTrue(isinstance(response, BaseResponse), 'dispatch on html-only template did not return a Response') self.assertEqual(response.status_code, 200, 'dispatch to template did not get a 200 status (got %d)' % response.status_code) with changer.change_times(file(index, 'w')) as fp: fp.write('# this is python\n----\n<strong>this is HTML</strong>\n') response = app.dispatch(req) self.assertTrue(isinstance(response, BaseResponse), 'dispatch on mixed template did not return a Response') self.assertEqual(response.status_code, 200, 'dispatch to template did not get a 200 status (got %d)' % response.status_code) with changer.change_times(file(static, 'w')) as fp: fp.write('* { background-color: white }\n') req = Request(wsgi_environ('GET', '/base.css')) response = app.dispatch(req) self.assertTrue(isinstance(response, BaseResponse), 'dispatch on static did not return a Response') self.assertEqual(response.status_code, 200, 'dispatch to static did not get a 200 status (got %d)' % response.status_code)
def test_dispatch(self): index = os.path.join(self.app_dir, 'index.ks') static = os.path.join(self.app_dir, 'base.css') app = Keystone(self.app_dir) req = Request(wsgi_environ('GET', '/')) response = app.dispatch(req) self.assertTrue(isinstance(response, BaseResponse), 'dispatch on missing did not return a Response') self.assertEqual( response.status_code, 404, 'missing did not get a 404 HTTPException (got %d)' % response.status_code) changer = util.MtimeChanger() with changer.change_times(file(index, 'w')) as fp: fp.write( 'raise Exception("blah")\n----\n<strong>this is HTML</strong>\n' ) response = app.dispatch(req) self.assertTrue(isinstance(response, BaseResponse), 'dispatch on exception did not return a Response') self.assertEqual( response.status_code, 500, 'error did not get a 500 HTTPException (got %d)' % response.status_code) with changer.change_times(file(index, 'w')) as fp: fp.write( 'raise http.SeeOther("/foo")\n----\n<strong>this is HTML</strong>\n' ) response = app.dispatch(req) self.assertTrue(isinstance(response, BaseResponse), 'dispatch on HTTPException did not return a Response') self.assertEqual( response.status_code, 303, 'redirect (see other) did not get a 303 HTTPException (got %d)' % response.status_code) with changer.change_times(file(index, 'w')) as fp: fp.write('<strong>this is HTML</strong>\n') response = app.dispatch(req) self.assertTrue( isinstance(response, BaseResponse), 'dispatch on html-only template did not return a Response') self.assertEqual( response.status_code, 200, 'dispatch to template did not get a 200 status (got %d)' % response.status_code) with changer.change_times(file(index, 'w')) as fp: fp.write('# this is python\n----\n<strong>this is HTML</strong>\n') response = app.dispatch(req) self.assertTrue( isinstance(response, BaseResponse), 'dispatch on mixed template did not return a Response') self.assertEqual( response.status_code, 200, 'dispatch to template did not get a 200 status (got %d)' % response.status_code) with changer.change_times(file(static, 'w')) as fp: fp.write('* { background-color: white }\n') req = Request(wsgi_environ('GET', '/base.css')) response = app.dispatch(req) self.assertTrue(isinstance(response, BaseResponse), 'dispatch on static did not return a Response') self.assertEqual( response.status_code, 200, 'dispatch to static did not get a 200 status (got %d)' % response.status_code)