Example #1
0
 def testCombinedTools(self):
     expectedResult = (ntou("Hello,world") + europoundUnicode).encode('utf-8')
     zbuf = BytesIO()
     zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=9)
     zfile.write(expectedResult)
     zfile.close()
     
     self.getPage("/euro", headers=[("Accept-Encoding", "gzip"),
                                     ("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7")])
     self.assertInBody(zbuf.getvalue()[:3])
     
     zbuf = BytesIO()
     zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=6)
     zfile.write(expectedResult)
     zfile.close()
     
     self.getPage("/decorated_euro", headers=[("Accept-Encoding", "gzip")])
     self.assertInBody(zbuf.getvalue()[:3])
     
     # This returns a different value because gzip's priority was
     # lowered in conf, allowing the rotator to run after gzip.
     # Of course, we don't want breakage in production apps,
     # but it proves the priority was changed.
     self.getPage("/decorated_euro/subpath",
                  headers=[("Accept-Encoding", "gzip")])
     if py3k:
         self.assertInBody(bytes([(x + 3) % 256 for x in zbuf.getvalue()]))
     else:
         self.assertInBody(''.join([chr((ord(x) + 3) % 256) for x in zbuf.getvalue()]))
Example #2
0
    def testCombinedTools(self):
        expectedResult = (ntou("Hello,world") +
                          europoundUnicode).encode('utf-8')
        zbuf = BytesIO()
        zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=9)
        zfile.write(expectedResult)
        zfile.close()

        self.getPage("/euro",
                     headers=[("Accept-Encoding", "gzip"),
                              ("Accept-Charset",
                               "ISO-8859-1,utf-8;q=0.7,*;q=0.7")])
        self.assertInBody(zbuf.getvalue()[:3])

        zbuf = BytesIO()
        zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=6)
        zfile.write(expectedResult)
        zfile.close()

        self.getPage("/decorated_euro", headers=[("Accept-Encoding", "gzip")])
        self.assertInBody(zbuf.getvalue()[:3])

        # This returns a different value because gzip's priority was
        # lowered in conf, allowing the rotator to run after gzip.
        # Of course, we don't want breakage in production apps,
        # but it proves the priority was changed.
        self.getPage("/decorated_euro/subpath",
                     headers=[("Accept-Encoding", "gzip")])
        self.assertInBody(''.join(
            [chr((ord(x) + 3) % 256) for x in zbuf.getvalue()]))
 def testCombinedTools(self):
     expectedResult = (ntou('Hello,world') + europoundUnicode).encode('utf-8')
     zbuf = BytesIO()
     zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=9)
     zfile.write(expectedResult)
     zfile.close()
     self.getPage('/euro', headers=[('Accept-Encoding', 'gzip'), ('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.7')])
     self.assertInBody(zbuf.getvalue()[:3])
     zbuf = BytesIO()
     zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=6)
     zfile.write(expectedResult)
     zfile.close()
     self.getPage('/decorated_euro', headers=[('Accept-Encoding', 'gzip')])
     self.assertInBody(zbuf.getvalue()[:3])
     self.getPage('/decorated_euro/subpath', headers=[('Accept-Encoding', 'gzip')])
     self.assertInBody(''.join([ chr((ord(x) + 3) % 256) for x in zbuf.getvalue() ]))
Example #4
0
 def testGzip(self):
     zbuf = BytesIO()
     zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=9)
     zfile.write(ntob('Hello, world'))
     zfile.close()
     self.getPage('/gzip/', headers=[('Accept-Encoding', 'gzip')])
     self.assertInBody(zbuf.getvalue()[:3])
     self.assertHeader('Vary', 'Accept-Encoding')
     self.assertHeader('Content-Encoding', 'gzip')
     self.getPage('/gzip/', headers=[('Accept-Encoding', 'identity')])
     self.assertHeader('Vary', 'Accept-Encoding')
     self.assertNoHeader('Content-Encoding')
     self.assertBody('Hello, world')
     self.getPage('/gzip/', headers=[('Accept-Encoding', 'gzip;q=0')])
     self.assertHeader('Vary', 'Accept-Encoding')
     self.assertNoHeader('Content-Encoding')
     self.assertBody('Hello, world')
     self.getPage('/gzip/', headers=[('Accept-Encoding', '*;q=0')])
     self.assertStatus(406)
     self.assertNoHeader('Content-Encoding')
     self.assertErrorPage(406, 'identity, gzip')
     self.getPage('/gzip/noshow', headers=[('Accept-Encoding', 'gzip')])
     self.assertNoHeader('Content-Encoding')
     self.assertStatus(500)
     self.assertErrorPage(500, pattern='IndexError\n')
     if cherrypy.server.protocol_version == 'HTTP/1.0' or getattr(cherrypy.server, 'using_apache', False):
         self.getPage('/gzip/noshow_stream', headers=[('Accept-Encoding', 'gzip')])
         self.assertHeader('Content-Encoding', 'gzip')
         self.assertInBody('\x1f\x8b\x08\x00')
     else:
         self.assertRaises((ValueError, IncompleteRead), self.getPage, '/gzip/noshow_stream', headers=[('Accept-Encoding', 'gzip')])
Example #5
0
 def testGzip(self):
     zbuf = BytesIO()
     zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=9)
     zfile.write(ntob('Hello, world'))
     zfile.close()
     self.getPage('/gzip/', headers=[('Accept-Encoding', 'gzip')])
     self.assertInBody(zbuf.getvalue()[:3])
     self.assertHeader('Vary', 'Accept-Encoding')
     self.assertHeader('Content-Encoding', 'gzip')
     self.getPage('/gzip/', headers=[('Accept-Encoding', 'identity')])
     self.assertHeader('Vary', 'Accept-Encoding')
     self.assertNoHeader('Content-Encoding')
     self.assertBody('Hello, world')
     self.getPage('/gzip/', headers=[('Accept-Encoding', 'gzip;q=0')])
     self.assertHeader('Vary', 'Accept-Encoding')
     self.assertNoHeader('Content-Encoding')
     self.assertBody('Hello, world')
     self.getPage('/gzip/', headers=[('Accept-Encoding', '*;q=0')])
     self.assertStatus(406)
     self.assertNoHeader('Content-Encoding')
     self.assertErrorPage(406, 'identity, gzip')
     self.getPage('/gzip/noshow', headers=[('Accept-Encoding', 'gzip')])
     self.assertNoHeader('Content-Encoding')
     self.assertStatus(500)
     self.assertErrorPage(500, pattern='IndexError\n')
     if cherrypy.server.protocol_version == 'HTTP/1.0' or getattr(cherrypy.server, 'using_apache', False):
         self.getPage('/gzip/noshow_stream', headers=[('Accept-Encoding', 'gzip')])
         self.assertHeader('Content-Encoding', 'gzip')
         self.assertInBody('\x1f\x8b\x08\x00')
     else:
         self.assertRaises((ValueError, IncompleteRead), self.getPage, '/gzip/noshow_stream', headers=[('Accept-Encoding', 'gzip')])
Example #6
0
 def stats(self, filename, sortby='cumulative'):
     """:rtype stats(index): output of print_stats() for the given profile.
     """
     sio = BytesIO()
     s = pstats.Stats(os.path.join(self.path, filename), stream=sio)
     s.strip_dirs()
     s.sort_stats(sortby)
     s.print_stats()
     response = sio.getvalue()
     sio.close()
     return response
Example #7
0
 def testCombinedTools(self):
     expectedResult = (ntou('Hello,world') +
                       europoundUnicode).encode('utf-8')
     zbuf = BytesIO()
     zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=9)
     zfile.write(expectedResult)
     zfile.close()
     self.getPage('/euro',
                  headers=[('Accept-Encoding', 'gzip'),
                           ('Accept-Charset',
                            'ISO-8859-1,utf-8;q=0.7,*;q=0.7')])
     self.assertInBody(zbuf.getvalue()[:3])
     zbuf = BytesIO()
     zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=6)
     zfile.write(expectedResult)
     zfile.close()
     self.getPage('/decorated_euro', headers=[('Accept-Encoding', 'gzip')])
     self.assertInBody(zbuf.getvalue()[:3])
     self.getPage('/decorated_euro/subpath',
                  headers=[('Accept-Encoding', 'gzip')])
     self.assertInBody(''.join(
         [chr((ord(x) + 3) % 256) for x in zbuf.getvalue()]))
Example #8
0
    def testGzip(self):
        zbuf = BytesIO()
        zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=9)
        zfile.write(ntob("Hello, world"))
        zfile.close()

        self.getPage('/gzip/', headers=[("Accept-Encoding", "gzip")])
        self.assertInBody(zbuf.getvalue()[:3])
        self.assertHeader("Vary", "Accept-Encoding")
        self.assertHeader("Content-Encoding", "gzip")

        # Test when gzip is denied.
        self.getPage('/gzip/', headers=[("Accept-Encoding", "identity")])
        self.assertHeader("Vary", "Accept-Encoding")
        self.assertNoHeader("Content-Encoding")
        self.assertBody("Hello, world")

        self.getPage('/gzip/', headers=[("Accept-Encoding", "gzip;q=0")])
        self.assertHeader("Vary", "Accept-Encoding")
        self.assertNoHeader("Content-Encoding")
        self.assertBody("Hello, world")

        self.getPage('/gzip/', headers=[("Accept-Encoding", "*;q=0")])
        self.assertStatus(406)
        self.assertNoHeader("Content-Encoding")
        self.assertErrorPage(406, "identity, gzip")

        # Test for ticket #147
        self.getPage('/gzip/noshow', headers=[("Accept-Encoding", "gzip")])
        self.assertNoHeader('Content-Encoding')
        self.assertStatus(500)
        self.assertErrorPage(500, pattern="IndexError\n")

        # In this case, there's nothing we can do to deliver a
        # readable page, since 1) the gzip header is already set,
        # and 2) we may have already written some of the body.
        # The fix is to never stream yields when using gzip.
        if (cherrypy.server.protocol_version == "HTTP/1.0"
                or getattr(cherrypy.server, "using_apache", False)):
            self.getPage('/gzip/noshow_stream',
                         headers=[("Accept-Encoding", "gzip")])
            self.assertHeader('Content-Encoding', 'gzip')
            self.assertInBody('\x1f\x8b\x08\x00')
        else:
            # The wsgiserver will simply stop sending data, and the HTTP client
            # will error due to an incomplete chunk-encoded stream.
            self.assertRaises((ValueError, IncompleteRead),
                              self.getPage,
                              '/gzip/noshow_stream',
                              headers=[("Accept-Encoding", "gzip")])
Example #9
0
 def testGzip(self):
     zbuf = BytesIO()
     zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=9)
     zfile.write(ntob("Hello, world"))
     zfile.close()
     
     self.getPage('/gzip/', headers=[("Accept-Encoding", "gzip")])
     self.assertInBody(zbuf.getvalue()[:3])
     self.assertHeader("Vary", "Accept-Encoding")
     self.assertHeader("Content-Encoding", "gzip")
     
     # Test when gzip is denied.
     self.getPage('/gzip/', headers=[("Accept-Encoding", "identity")])
     self.assertHeader("Vary", "Accept-Encoding")
     self.assertNoHeader("Content-Encoding")
     self.assertBody("Hello, world")
     
     self.getPage('/gzip/', headers=[("Accept-Encoding", "gzip;q=0")])
     self.assertHeader("Vary", "Accept-Encoding")
     self.assertNoHeader("Content-Encoding")
     self.assertBody("Hello, world")
     
     self.getPage('/gzip/', headers=[("Accept-Encoding", "*;q=0")])
     self.assertStatus(406)
     self.assertNoHeader("Content-Encoding")
     self.assertErrorPage(406, "identity, gzip")
     
     # Test for ticket #147
     self.getPage('/gzip/noshow', headers=[("Accept-Encoding", "gzip")])
     self.assertNoHeader('Content-Encoding')
     self.assertStatus(500)
     self.assertErrorPage(500, pattern="IndexError\n")
     
     # In this case, there's nothing we can do to deliver a
     # readable page, since 1) the gzip header is already set,
     # and 2) we may have already written some of the body.
     # The fix is to never stream yields when using gzip.
     if (cherrypy.server.protocol_version == "HTTP/1.0" or
         getattr(cherrypy.server, "using_apache", False)):
         self.getPage('/gzip/noshow_stream',
                      headers=[("Accept-Encoding", "gzip")])
         self.assertHeader('Content-Encoding', 'gzip')
         self.assertInBody('\x1f\x8b\x08\x00')
     else:
         # The wsgiserver will simply stop sending data, and the HTTP client
         # will error due to an incomplete chunk-encoded stream.
         self.assertRaises((ValueError, IncompleteRead), self.getPage,
                           '/gzip/noshow_stream',
                           headers=[("Accept-Encoding", "gzip")])
Example #10
0
    def stats(self, filename, sortby='cumulative'):
        sio = BytesIO()
        if sys.version_info >= (2, 5):
            s = pstats.Stats(os.path.join(self.path, filename), stream=sio)
            s.strip_dirs()
            s.sort_stats(sortby)
            s.print_stats()
        else:
            s = pstats.Stats(os.path.join(self.path, filename))
            s.strip_dirs()
            s.sort_stats(sortby)
            oldout = sys.stdout
            try:
                sys.stdout = sio
                s.print_stats()
            finally:
                sys.stdout = oldout

        response = sio.getvalue()
        sio.close()
        return response
Example #11
0
 def stats(self, filename, sortby='cumulative'):
     """:rtype stats(index): output of print_stats() for the given profile.
     """
     sio = BytesIO()
     if sys.version_info >= (2, 5):
         s = pstats.Stats(os.path.join(self.path, filename), stream=sio)
         s.strip_dirs()
         s.sort_stats(sortby)
         s.print_stats()
     else:
         # pstats.Stats before Python 2.5 didn't take a 'stream' arg,
         # but just printed to stdout. So re-route stdout.
         s = pstats.Stats(os.path.join(self.path, filename))
         s.strip_dirs()
         s.sort_stats(sortby)
         oldout = sys.stdout
         try:
             sys.stdout = sio
             s.print_stats()
         finally:
             sys.stdout = oldout
     response = sio.getvalue()
     sio.close()
     return response