示例#1
0
    def test_registering_with_slimming_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()

        # it will only fix images that have otherwise been registered
        registerImage(MyProduct, right_here("image.jpg"), rel_path="tests")

        registerCSSFile(
            MyProduct,
            "containsimages.css",
            rel_path="tests",
            set_expiry_header=True,
            slim_if_possible=True,
            replace_images_with_aliases=True,
        )
        static = getattr(instance, "containsimages.css")

        # Not the same...
        self.assertNotEqual(str(static), css_slimmer(open(right_here("containsimages.css")).read()))
        # unless you remove all '.\d+.'
        self.assertEqual(
            re.sub("\.\d{10,11}\.", ".", str(static)), css_slimmer(open(right_here("containsimages.css")).read())
        )

        # because we haven't registered large.jpg it won't be aliased
        self.assertTrue("large.jpg" in str(static))
        self.assertTrue("image.jpg" not in str(static))

        self.assertEqual(
            sorted(["containsimages.css-slimmed.css-aliased.css", "containsimages.css-slimmed.css"]),
            sorted(os.listdir(_get_autogenerated_dir())),
        )

        # if you don it again it should just overwrite the old one
        registerCSSFile(
            MyProduct,
            "containsimages.css",
            rel_path="tests",
            set_expiry_header=True,
            slim_if_possible=True,
            replace_images_with_aliases=True,
        )

        self.assertEqual(
            sorted(["containsimages.css-slimmed.css-aliased.css", "containsimages.css-slimmed.css"]),
            sorted(os.listdir(_get_autogenerated_dir())),
        )
示例#2
0
    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)
示例#3
0
    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()))
示例#4
0
    def test_registerCSSFile(self):
        class MyProduct:
            pass

        registerCSSFile(MyProduct, "test.css", rel_path="tests", set_expiry_header=True, slim_if_possible=False)
        instance = MyProduct()
        static = getattr(instance, "test.css")

        test_css_stat = os.stat(right_here("test.css"))

        # run through the same tests as above but with this
        mtime = test_css_stat[stat.ST_MTIME]
        static_infinite = getattr(instance, "test.%s.css" % mtime, None)
        self.assertNotEqual(static_infinite, None)

        self.assertEqual(len(str(static)), test_css_stat[stat.ST_SIZE])
        self.assertEqual(len(str(static_infinite)), test_css_stat[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.css"))[stat.ST_MTIME])
        )
        self.assertEqual(RESPONSE.getHeader("cache-control"), "public,max-age=3600")  # default
        self.assertEqual(RESPONSE.getHeader("content-type"), "text/css")
        self.assertEqual(int(RESPONSE.getHeader("content-length")), os.stat(right_here("test.css"))[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.css"))[stat.ST_MTIME])
        )
        self.assertEqual(RESPONSE.getHeader("cache-control"), "public,max-age=%s" % EXPIRY_INFINITY)  #
        self.assertEqual(RESPONSE.getHeader("content-type"), "text/css")
        self.assertEqual(int(RESPONSE.getHeader("content-length")), os.stat(right_here("test.css"))[stat.ST_SIZE])
示例#5
0
    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()))