def test_registering_with_slimming_basic(self): try: from slimmer import js_slimmer, css_slimmer except ImportError: # not possible to test this return class MyProduct: pass instance = MyProduct() registerJSFile(MyProduct, "test.js", rel_path="tests", set_expiry_header=True, slim_if_possible=True) static = getattr(instance, "test.js") self.assertEqual(str(static), js_slimmer(open(right_here("test.js")).read())) # this will have created a file called 'test.js-slimmed.js' whose content # is the same as str(static) copy_test_js = os.path.join(_get_autogenerated_dir(), "test.js-slimmed.js") self.assertTrue(os.path.isfile(copy_test_js)) self.assertEqual(open(copy_test_js).read(), str(static)) # and it that directory there should not be any other files for f in os.listdir(os.path.dirname(copy_test_js)): self.assertEqual(f, os.path.basename(copy_test_js)) registerCSSFile(MyProduct, "test.css", rel_path="tests", set_expiry_header=True, slim_if_possible=True) static = getattr(instance, "test.css") self.assertEqual(str(static), css_slimmer(open(right_here("test.css")).read())) # this will have created a file called 'test.css-slimmed.css' whose content # is the same as str(static) copy_test_css = os.path.join(_get_autogenerated_dir(), "test.css-slimmed.css") self.assertTrue(os.path.isfile(copy_test_css)) self.assertEqual(open(copy_test_css).read(), str(static)) # and it that directory there should not be any other files other # than the one we made before called test.js-slimmed.js for f in os.listdir(os.path.dirname(copy_test_css)): self.assertTrue(f == os.path.basename(copy_test_js) or f == os.path.basename(copy_test_css)) # if you don it again it should just overwrite the old one registerCSSFile(MyProduct, "test.css", rel_path="tests", set_expiry_header=True, slim_if_possible=True) static = getattr(instance, "test.css") # there should still only be two files in the autogenerated directory self.assertEqual(len(os.listdir(_get_autogenerated_dir())), 2)
def test_large_GzippedImageFile(self): """ test registering a css file or a js file with gzip_if_possible=True """ class MyProduct: pass # first without also slimming registerJSFile( MyProduct, "large.js", rel_path="tests", set_expiry_header=True, slim_if_possible=False, gzip_if_possible=True, ) instance = MyProduct() static = getattr(instance, "large.js") REQUEST = self.app.REQUEST RESPONSE = REQUEST.RESPONSE bin_content = static.index_html(REQUEST, RESPONSE) self.assertEqual( RESPONSE.getHeader("last-modified"), rfc1123_date(os.stat(right_here("large.js"))[stat.ST_MTIME]) ) self.assertEqual(RESPONSE.getHeader("cache-control"), "public,max-age=3600") # default self.assertEqual(RESPONSE.getHeader("content-type"), "application/x-javascript") self.assertEqual(int(RESPONSE.getHeader("content-length")), os.stat(right_here("large.js"))[stat.ST_SIZE]) # since our REQUEST does't have gzip in HTTP_ACCEPT_ENCODING # we're not going to get the gzipped version self.assertEqual(len(bin_content), os.stat(right_here("large.js"))[stat.ST_SIZE]) # now change HTTP_ACCEPT_ENCODING REQUEST.set("HTTP_ACCEPT_ENCODING", "gzip") bin_content = static.index_html(REQUEST, RESPONSE) self.assertTrue(isinstance(bin_content, open)) _content = bin_content.read() self.assertNotEqual(len(_content), os.stat(right_here("large.js"))[stat.ST_SIZE]) data = _gzip2ascii(_content) self.assertEqual(data, open(right_here("large.js")).read())
def test_registering_with_slimming_and_gzip_and_images_in_css(self): try: from slimmer import js_slimmer, css_slimmer except ImportError: # not possible to test this return class MyProduct: pass instance = MyProduct() registerJSFile(MyProduct, "test.js", rel_path="tests", set_expiry_header=True, slim_if_possible=True) static = getattr(instance, "test.js") self.assertEqual(str(static), js_slimmer(open(right_here("test.js")).read())) registerCSSFile( MyProduct, "test.css", rel_path="tests", set_expiry_header=True, slim_if_possible=True, gzip_if_possible=True, ) static = getattr(instance, "test.css") self.assertEqual(str(static), css_slimmer(open(right_here("test.css")).read())) # if you don it again it should just overwrite the old one registerCSSFile( MyProduct, "test.css", rel_path="tests", set_expiry_header=True, slim_if_possible=True, gzip_if_possible=True, ) static = getattr(instance, "test.css") self.assertEqual(str(static), css_slimmer(open(right_here("test.css")).read()))
def test_registerJSFile(self): class MyProduct: pass registerJSFile(MyProduct, "test.js", rel_path="tests", set_expiry_header=True, slim_if_possible=False) instance = MyProduct() static = getattr(instance, "test.js") # run through the same tests as above but with this mtime = os.stat(right_here("test.js"))[stat.ST_MTIME] static_infinite = getattr(instance, "test.%s.js" % mtime, None) self.assertNotEqual(static_infinite, None) self.assertEqual(len(str(static)), os.stat(right_here("test.js"))[stat.ST_SIZE]) self.assertEqual(len(str(static_infinite)), os.stat(right_here("test.js"))[stat.ST_SIZE]) # if we render it with img.index_html() expect certain headers to be set REQUEST = self.app.REQUEST RESPONSE = REQUEST.RESPONSE bin_content = static.index_html(REQUEST, RESPONSE) self.assertEqual( RESPONSE.getHeader("last-modified"), rfc1123_date(os.stat(right_here("test.js"))[stat.ST_MTIME]) ) self.assertEqual(RESPONSE.getHeader("cache-control"), "public,max-age=3600") # default self.assertEqual(RESPONSE.getHeader("content-type"), "application/x-javascript") self.assertEqual(int(RESPONSE.getHeader("content-length")), os.stat(right_here("test.js"))[stat.ST_SIZE]) # if we render the infinitely cached one we can expect different headers bin_content = static_infinite.index_html(REQUEST, RESPONSE) self.assertEqual( RESPONSE.getHeader("last-modified"), rfc1123_date(os.stat(right_here("test.js"))[stat.ST_MTIME]) ) self.assertEqual(RESPONSE.getHeader("cache-control"), "public,max-age=%s" % EXPIRY_INFINITY) # self.assertEqual(RESPONSE.getHeader("content-type"), "application/x-javascript") self.assertEqual(int(RESPONSE.getHeader("content-length")), os.stat(right_here("test.js"))[stat.ST_SIZE])
def test_registering_with_slimming_and_gzip(self): try: from slimmer import js_slimmer, css_slimmer except ImportError: # not possible to test this return class MyProduct: pass instance = MyProduct() registerJSFile( MyProduct, "test.js", rel_path="tests", set_expiry_header=True, slim_if_possible=True, gzip_if_possible=True ) static = getattr(instance, "test.js") # Note, using str(static) means it does not send the Accept-Encoding: gzip # header. self.assertEqual(str(static), js_slimmer(open(right_here("test.js")).read())) # this will have created a file called 'test.js-slimmed.js' whose content # is the same as str(static) copy_test_js = os.path.join(_get_autogenerated_dir(), "test.js-slimmed.js") self.assertTrue(os.path.isfile(copy_test_js)) self.assertEqual(open(copy_test_js).read(), str(static)) # it would also have generated another copy called # test.js-slimmed.js.gz copy_test_js_gz = os.path.join(_get_autogenerated_dir(), "test.js-slimmed.js.gz") self.assertTrue(os.path.isfile(copy_test_js_gz)) self.assertEqual(gzip.open(copy_test_js_gz).read(), str(static)) registerCSSFile( MyProduct, "test.css", rel_path="tests", set_expiry_header=True, slim_if_possible=True, gzip_if_possible=True, ) static = getattr(instance, "test.css") self.assertEqual(str(static), css_slimmer(open(right_here("test.css")).read())) # this will have created a file called 'test.css-slimmed.css' whose content # is the same as str(static) copy_test_css = os.path.join(_get_autogenerated_dir(), "test.css-slimmed.css") self.assertTrue(os.path.isfile(copy_test_css)) self.assertEqual(open(copy_test_css).read(), str(static)) # it would also have generated another copy called # test.css-slimmed.css.gz copy_test_css_gz = os.path.join(_get_autogenerated_dir(), "test.css-slimmed.css.gz") self.assertTrue(os.path.isfile(copy_test_css_gz)) self.assertEqual(gzip.open(copy_test_css_gz).read(), str(static)) # if you don it AGAIN it should just overwrite the old one registerCSSFile( MyProduct, "test.css", rel_path="tests", set_expiry_header=True, slim_if_possible=True, gzip_if_possible=True, ) static = getattr(instance, "test.css") self.assertEqual(str(static), css_slimmer(open(right_here("test.css")).read()))