[test] Add missing u''
Test actually works in py3k, but not in python 2... That has to be a
first!
# -*- coding: utf-8 -*-# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr## This file is part of CubicWeb.## CubicWeb is free software: you can redistribute it and/or modify it under the# terms of the GNU Lesser General Public License as published by the Free# Software Foundation, either version 2.1 of the License, or (at your option)# any later version.## CubicWeb is distributed in the hope that it will be useful, but WITHOUT# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more# details.## You should have received a copy of the GNU Lesser General Public License along# with CubicWeb. If not, see <http://www.gnu.org/licenses/>.fromcontextlibimportcontextmanagerfromlogilab.commonimporttempattrfromlogilab.common.testlibimportTagsfromcubicweb.devtools.testlibimportCubicWebTCimportosimportos.pathasospimportglobfromcubicweb.utilsimportHTMLHeadfromcubicweb.web.views.staticcontrollersimportConcatFilesHandlerclassstaticfilespublishermixin(object):@contextmanagerdef_publish_static_files(self,url,header={}):withself.admin_access.web_request(headers=header)asreq:req._url=urlself.app_handle_request(req,url)yieldreqclassStaticControllerCacheTC(staticfilespublishermixin,CubicWebTC):tags=CubicWebTC.tags|Tags('static_controller','cache','http')deftest_static_file_are_cached(self):withself._publish_static_files('data/cubicweb.css')asreq:self.assertEqual(200,req.status_out)self.assertIn('last-modified',req.headers_out)next_headers={'if-modified-since':req.get_response_header('last-modified',raw=True),}withself._publish_static_files('data/cubicweb.css',next_headers)asreq:self.assertEqual(304,req.status_out)classStaticDirectoryControllerTC(staticfilespublishermixin,CubicWebTC):deftest_check_static_dir_access(self):"""write a file in the static directory and test the access"""staticdir=osp.join(self.session.vreg.config.static_directory)ifnotos.path.exists(staticdir):os.makedirs(staticdir)filename=osp.join(staticdir,'test')withopen(filename,'a')asf:withself._publish_static_files('static/test')asreq:self.assertEqual(200,req.status_out)classDataControllerTC(staticfilespublishermixin,CubicWebTC):tags=CubicWebTC.tags|Tags('static_controller','data','http')def_check_datafile_ok(self,fname):withself._publish_static_files(fname)asreq:self.assertEqual(200,req.status_out)self.assertIn('last-modified',req.headers_out)self.assertIn('expires',req.headers_out)self.assertEqual(req.get_response_header('cache-control'),{'max-age':604800})next_headers={'if-modified-since':req.get_response_header('last-modified',raw=True),}withself._publish_static_files(fname,next_headers)asreq:self.assertEqual(304,req.status_out)def_check_datafile_redirect(self,fname,expected):withself._publish_static_files(fname)asreq:self.assertEqual(302,req.status_out)self.assertEqual(req.get_response_header('location'),req.base_url()+expected)def_check_no_datafile(self,fname):withself._publish_static_files(fname)asreq:self.assertEqual(404,req.status_out)deftest_static_data_mode(self):hash=self.vreg.config.instance_md5_version()self.assertEqual(32,len(hash))withtempattr(self.vreg.config,'mode','test'):self._check_datafile_ok('data/cubicweb.css')self._check_no_datafile('data/does/not/exist')self._check_no_datafile('data/%s/cubicweb.css'%('0'*len(hash)))withtempattr(self.vreg.config,'mode','notest'):self.config._init_base_url()# reset config.datadir_urlself._check_datafile_redirect('data/cubicweb.css','data/%s/cubicweb.css'%hash)self._check_datafile_ok('data/%s/cubicweb.css'%hash)self._check_no_datafile('data/%s/does/not/exist'%hash)self._check_datafile_redirect('data/%s/does/not/exist'%('0'*len(hash)),'data/%s/%s/does/not/exist'%(hash,'0'*len(hash)))classConcatFilesTC(CubicWebTC):tags=CubicWebTC.tags|Tags('static_controller','concat')deftearDown(self):super(ConcatFilesTC,self).tearDown()self._cleanup_concat_cache()def_cleanup_concat_cache(self):uicachedir=osp.join(self.config.apphome,'uicache')forfnameinglob.glob(osp.join(uicachedir,'cache_concat_*')):os.unlink(osp.join(uicachedir,fname))@contextmanagerdef_publish_js_files(self,js_files):withself.admin_access.web_request()asreq:head=HTMLHead(req)url=head.concat_urls([req.data_url(js_file)forjs_fileinjs_files])[len(req.base_url()):]req._url=urlres=self.app_handle_request(req,url)yieldres,reqdefexpected_content(self,js_files):content=b''forjs_fileinjs_files:dirpath,rid=self.config.locate_resource(js_file)ifdirpathisnotNone:# ignore resources not foundwithopen(osp.join(dirpath,rid),'rb')asf:content+=f.read()+b'\n'returncontentdeftest_cache(self):js_files=('cubicweb.ajax.js','jquery.js')withself._publish_js_files(js_files)as(result,req):self.assertNotEqual(404,req.status_out)# check result contentself.assertEqual(result,self.expected_content(js_files))# make sure we kept a cached version on filesystemconcat_hander=ConcatFilesHandler(self.config)filepath=concat_hander.build_filepath(js_files)self.assertTrue(osp.isfile(filepath))deftest_invalid_file_in_debug_mode(self):js_files=('cubicweb.ajax.js','dummy.js')# in debug mode, an error is raisedself.config.debugmode=Truetry:withself._publish_js_files(js_files)as(result,req):#print resultself.assertEqual(404,req.status_out)finally:self.config.debugmode=Falsedeftest_invalid_file_in_production_mode(self):js_files=('cubicweb.ajax.js','dummy.js')withself._publish_js_files(js_files)as(result,req):self.assertNotEqual(404,req.status_out)# check result contentself.assertEqual(result,self.expected_content(js_files))if__name__=='__main__':fromlogilab.common.testlibimportunittest_mainunittest_main()