Python cherrypy.org() Examples

The following are 15 code examples of cherrypy.org(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module cherrypy , or try the search function .
Example #1
Source File: _cprequest.py    From cherrypy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def response_namespace(k, v):
    """Attach response attributes declared in config."""
    # Provides config entries to set default response headers
    # http://cherrypy.org/ticket/889
    if k[:8] == 'headers.':
        cherrypy.serving.response.headers[k.split('.', 1)[1]] = v
    else:
        setattr(cherrypy.serving.response, k, v) 
Example #2
Source File: test_request_obj.py    From cherrypy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def testAbsoluteURIPathInfo(self):
        # http://cherrypy.org/ticket/1061
        self.getPage('http://localhost/pathinfo/foo/bar')
        self.assertBody('/pathinfo/foo/bar') 
Example #3
Source File: _cprequest.py    From opsbro with MIT License 5 votes vote down vote up
def response_namespace(k, v):
    """Attach response attributes declared in config."""
    # Provides config entries to set default response headers
    # http://cherrypy.org/ticket/889
    if k[:8] == 'headers.':
        cherrypy.serving.response.headers[k.split('.', 1)[1]] = v
    else:
        setattr(cherrypy.serving.response, k, v) 
Example #4
Source File: _cprequest.py    From bazarr with GNU General Public License v3.0 5 votes vote down vote up
def response_namespace(k, v):
    """Attach response attributes declared in config."""
    # Provides config entries to set default response headers
    # http://cherrypy.org/ticket/889
    if k[:8] == 'headers.':
        cherrypy.serving.response.headers[k.split('.', 1)[1]] = v
    else:
        setattr(cherrypy.serving.response, k, v) 
Example #5
Source File: test_request_obj.py    From bazarr with GNU General Public License v3.0 5 votes vote down vote up
def testAbsoluteURIPathInfo(self):
        # http://cherrypy.org/ticket/1061
        self.getPage('http://localhost/pathinfo/foo/bar')
        self.assertBody('/pathinfo/foo/bar') 
Example #6
Source File: _cprequest.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def response_namespace(k, v):
    """Attach response attributes declared in config."""
    # Provides config entries to set default response headers
    # http://cherrypy.org/ticket/889
    if k[:8] == 'headers.':
        cherrypy.serving.response.headers[k.split('.', 1)[1]] = v
    else:
        setattr(cherrypy.serving.response, k, v) 
Example #7
Source File: test_request_obj.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def testAbsoluteURIPathInfo(self):
        # http://cherrypy.org/ticket/1061
        self.getPage('http://localhost/pathinfo/foo/bar')
        self.assertBody('/pathinfo/foo/bar') 
Example #8
Source File: _cprequest.py    From moviegrabber with GNU General Public License v3.0 5 votes vote down vote up
def response_namespace(k, v):
    """Attach response attributes declared in config."""
    # Provides config entries to set default response headers
    # http://cherrypy.org/ticket/889
    if k[:8] == 'headers.':
        cherrypy.serving.response.headers[k.split('.', 1)[1]] = v
    else:
        setattr(cherrypy.serving.response, k, v) 
Example #9
Source File: test_request_obj.py    From moviegrabber with GNU General Public License v3.0 5 votes vote down vote up
def testAbsoluteURIPathInfo(self):
        # http://cherrypy.org/ticket/1061
        self.getPage("http://localhost/pathinfo/foo/bar")
        self.assertBody("/pathinfo/foo/bar") 
Example #10
Source File: test_request_obj.py    From moviegrabber with GNU General Public License v3.0 5 votes vote down vote up
def test_repeated_headers(self):
        # Test that two request headers are collapsed into one.
        # See http://www.cherrypy.org/ticket/542.
        self.getPage("/headers/Accept-Charset",
                     headers=[("Accept-Charset", "iso-8859-5"),
                              ("Accept-Charset", "unicode-1-1;q=0.8")])
        self.assertBody("iso-8859-5, unicode-1-1;q=0.8")
        
        # Tests that each header only appears once, regardless of case.
        self.getPage("/headers/doubledheaders")
        self.assertBody("double header test")
        hnames = [name.title() for name, val in self.headers]
        for key in ['Content-Length', 'Content-Type', 'Date',
                    'Expires', 'Location', 'Server']:
            self.assertEqual(hnames.count(key), 1, self.headers) 
Example #11
Source File: test_request_obj.py    From cherrypy with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def testParams(self):
        self.getPage('/params/?thing=a')
        self.assertBody(repr(ntou('a')))

        self.getPage('/params/?thing=a&thing=b&thing=c')
        self.assertBody(repr([ntou('a'), ntou('b'), ntou('c')]))

        # Test friendly error message when given params are not accepted.
        cherrypy.config.update({'request.show_mismatched_params': True})
        self.getPage('/params/?notathing=meeting')
        self.assertInBody('Missing parameters: thing')
        self.getPage('/params/?thing=meeting&notathing=meeting')
        self.assertInBody('Unexpected query string parameters: notathing')

        # Test ability to turn off friendly error messages
        cherrypy.config.update({'request.show_mismatched_params': False})
        self.getPage('/params/?notathing=meeting')
        self.assertInBody('Not Found')
        self.getPage('/params/?thing=meeting&notathing=meeting')
        self.assertInBody('Not Found')

        # Test "% HEX HEX"-encoded URL, param keys, and values
        self.getPage('/params/%d4%20%e3/cheese?Gruy%E8re=Bulgn%e9ville')
        self.assertBody('args: %s kwargs: %s' %
                        (('\xd4 \xe3', 'cheese'),
                         [('Gruy\xe8re', ntou('Bulgn\xe9ville'))]))

        # Make sure that encoded = and & get parsed correctly
        self.getPage(
            '/params/code?url=http%3A//cherrypy.org/index%3Fa%3D1%26b%3D2')
        self.assertBody('args: %s kwargs: %s' %
                        (('code',),
                         [('url', ntou('http://cherrypy.org/index?a=1&b=2'))]))

        # Test coordinates sent by <img ismap>
        self.getPage('/params/ismap?223,114')
        self.assertBody('Coordinates: 223, 114')

        # Test "name[key]" dict-like params
        self.getPage('/params/dictlike?a[1]=1&a[2]=2&b=foo&b[bar]=baz')
        self.assertBody('args: %s kwargs: %s' %
                        (('dictlike',),
                         [('a[1]', ntou('1')), ('a[2]', ntou('2')),
                          ('b', ntou('foo')), ('b[bar]', ntou('baz'))])) 
Example #12
Source File: test_request_obj.py    From bazarr with GNU General Public License v3.0 4 votes vote down vote up
def testParams(self):
        self.getPage('/params/?thing=a')
        self.assertBody(repr(ntou('a')))

        self.getPage('/params/?thing=a&thing=b&thing=c')
        self.assertBody(repr([ntou('a'), ntou('b'), ntou('c')]))

        # Test friendly error message when given params are not accepted.
        cherrypy.config.update({'request.show_mismatched_params': True})
        self.getPage('/params/?notathing=meeting')
        self.assertInBody('Missing parameters: thing')
        self.getPage('/params/?thing=meeting&notathing=meeting')
        self.assertInBody('Unexpected query string parameters: notathing')

        # Test ability to turn off friendly error messages
        cherrypy.config.update({'request.show_mismatched_params': False})
        self.getPage('/params/?notathing=meeting')
        self.assertInBody('Not Found')
        self.getPage('/params/?thing=meeting&notathing=meeting')
        self.assertInBody('Not Found')

        # Test "% HEX HEX"-encoded URL, param keys, and values
        self.getPage('/params/%d4%20%e3/cheese?Gruy%E8re=Bulgn%e9ville')
        self.assertBody('args: %s kwargs: %s' %
                        (('\xd4 \xe3', 'cheese'),
                         [('Gruy\xe8re', ntou('Bulgn\xe9ville'))]))

        # Make sure that encoded = and & get parsed correctly
        self.getPage(
            '/params/code?url=http%3A//cherrypy.org/index%3Fa%3D1%26b%3D2')
        self.assertBody('args: %s kwargs: %s' %
                        (('code',),
                         [('url', ntou('http://cherrypy.org/index?a=1&b=2'))]))

        # Test coordinates sent by <img ismap>
        self.getPage('/params/ismap?223,114')
        self.assertBody('Coordinates: 223, 114')

        # Test "name[key]" dict-like params
        self.getPage('/params/dictlike?a[1]=1&a[2]=2&b=foo&b[bar]=baz')
        self.assertBody('args: %s kwargs: %s' %
                        (('dictlike',),
                         [('a[1]', ntou('1')), ('a[2]', ntou('2')),
                          ('b', ntou('foo')), ('b[bar]', ntou('baz'))])) 
Example #13
Source File: test_request_obj.py    From Tautulli with GNU General Public License v3.0 4 votes vote down vote up
def testParams(self):
        self.getPage('/params/?thing=a')
        self.assertBody(repr(ntou('a')))

        self.getPage('/params/?thing=a&thing=b&thing=c')
        self.assertBody(repr([ntou('a'), ntou('b'), ntou('c')]))

        # Test friendly error message when given params are not accepted.
        cherrypy.config.update({'request.show_mismatched_params': True})
        self.getPage('/params/?notathing=meeting')
        self.assertInBody('Missing parameters: thing')
        self.getPage('/params/?thing=meeting&notathing=meeting')
        self.assertInBody('Unexpected query string parameters: notathing')

        # Test ability to turn off friendly error messages
        cherrypy.config.update({'request.show_mismatched_params': False})
        self.getPage('/params/?notathing=meeting')
        self.assertInBody('Not Found')
        self.getPage('/params/?thing=meeting&notathing=meeting')
        self.assertInBody('Not Found')

        # Test "% HEX HEX"-encoded URL, param keys, and values
        self.getPage('/params/%d4%20%e3/cheese?Gruy%E8re=Bulgn%e9ville')
        self.assertBody('args: %s kwargs: %s' %
                        (('\xd4 \xe3', 'cheese'),
                         [('Gruy\xe8re', ntou('Bulgn\xe9ville'))]))

        # Make sure that encoded = and & get parsed correctly
        self.getPage(
            '/params/code?url=http%3A//cherrypy.org/index%3Fa%3D1%26b%3D2')
        self.assertBody('args: %s kwargs: %s' %
                        (('code',),
                         [('url', ntou('http://cherrypy.org/index?a=1&b=2'))]))

        # Test coordinates sent by <img ismap>
        self.getPage('/params/ismap?223,114')
        self.assertBody('Coordinates: 223, 114')

        # Test "name[key]" dict-like params
        self.getPage('/params/dictlike?a[1]=1&a[2]=2&b=foo&b[bar]=baz')
        self.assertBody('args: %s kwargs: %s' %
                        (('dictlike',),
                         [('a[1]', ntou('1')), ('a[2]', ntou('2')),
                          ('b', ntou('foo')), ('b[bar]', ntou('baz'))])) 
Example #14
Source File: test_request_obj.py    From moviegrabber with GNU General Public License v3.0 4 votes vote down vote up
def testParams(self):
        self.getPage("/params/?thing=a")
        self.assertBody(repr(ntou("a")))
        
        self.getPage("/params/?thing=a&thing=b&thing=c")
        self.assertBody(repr([ntou('a'), ntou('b'), ntou('c')]))

        # Test friendly error message when given params are not accepted.
        cherrypy.config.update({"request.show_mismatched_params": True})
        self.getPage("/params/?notathing=meeting")
        self.assertInBody("Missing parameters: thing")
        self.getPage("/params/?thing=meeting&notathing=meeting")
        self.assertInBody("Unexpected query string parameters: notathing")
        
        # Test ability to turn off friendly error messages
        cherrypy.config.update({"request.show_mismatched_params": False})
        self.getPage("/params/?notathing=meeting")
        self.assertInBody("Not Found")
        self.getPage("/params/?thing=meeting&notathing=meeting")
        self.assertInBody("Not Found")

        # Test "% HEX HEX"-encoded URL, param keys, and values
        self.getPage("/params/%d4%20%e3/cheese?Gruy%E8re=Bulgn%e9ville")
        self.assertBody("args: %s kwargs: %s" %
                        (('\xd4 \xe3', 'cheese'),
                         {'Gruy\xe8re': ntou('Bulgn\xe9ville')}))
        
        # Make sure that encoded = and & get parsed correctly
        self.getPage("/params/code?url=http%3A//cherrypy.org/index%3Fa%3D1%26b%3D2")
        self.assertBody("args: %s kwargs: %s" %
                        (('code',),
                         {'url': ntou('http://cherrypy.org/index?a=1&b=2')}))
        
        # Test coordinates sent by <img ismap>
        self.getPage("/params/ismap?223,114")
        self.assertBody("Coordinates: 223, 114")
        
        # Test "name[key]" dict-like params
        self.getPage("/params/dictlike?a[1]=1&a[2]=2&b=foo&b[bar]=baz")
        self.assertBody("args: %s kwargs: %s" %
                        (('dictlike',),
                         {'a[1]': ntou('1'), 'b[bar]': ntou('baz'),
                          'b': ntou('foo'), 'a[2]': ntou('2')})) 
Example #15
Source File: test_request_obj.py    From moviegrabber with GNU General Public License v3.0 4 votes vote down vote up
def testHeaderElements(self):
        # Accept-* header elements should be sorted, with most preferred first.
        h = [('Accept', 'audio/*; q=0.2, audio/basic')]
        self.getPage("/headerelements/get_elements?headername=Accept", h)
        self.assertStatus(200)
        self.assertBody("audio/basic\n"
                        "audio/*;q=0.2")
        
        h = [('Accept', 'text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c')]
        self.getPage("/headerelements/get_elements?headername=Accept", h)
        self.assertStatus(200)
        self.assertBody("text/x-c\n"
                        "text/html\n"
                        "text/x-dvi;q=0.8\n"
                        "text/plain;q=0.5")
        
        # Test that more specific media ranges get priority.
        h = [('Accept', 'text/*, text/html, text/html;level=1, */*')]
        self.getPage("/headerelements/get_elements?headername=Accept", h)
        self.assertStatus(200)
        self.assertBody("text/html;level=1\n"
                        "text/html\n"
                        "text/*\n"
                        "*/*")
        
        # Test Accept-Charset
        h = [('Accept-Charset', 'iso-8859-5, unicode-1-1;q=0.8')]
        self.getPage("/headerelements/get_elements?headername=Accept-Charset", h)
        self.assertStatus("200 OK")
        self.assertBody("iso-8859-5\n"
                        "unicode-1-1;q=0.8")
        
        # Test Accept-Encoding
        h = [('Accept-Encoding', 'gzip;q=1.0, identity; q=0.5, *;q=0')]
        self.getPage("/headerelements/get_elements?headername=Accept-Encoding", h)
        self.assertStatus("200 OK")
        self.assertBody("gzip;q=1.0\n"
                        "identity;q=0.5\n"
                        "*;q=0")
        
        # Test Accept-Language
        h = [('Accept-Language', 'da, en-gb;q=0.8, en;q=0.7')]
        self.getPage("/headerelements/get_elements?headername=Accept-Language", h)
        self.assertStatus("200 OK")
        self.assertBody("da\n"
                        "en-gb;q=0.8\n"
                        "en;q=0.7")
        
        # Test malformed header parsing. See http://www.cherrypy.org/ticket/763.
        self.getPage("/headerelements/get_elements?headername=Content-Type",
                     # Note the illegal trailing ";"
                     headers=[('Content-Type', 'text/html; charset=utf-8;')])
        self.assertStatus(200)
        self.assertBody("text/html;charset=utf-8")