Пример #1
0
 def test_static_match(self):
     class Resource(resource.Resource):
         def __init__(self, segments=[]):
             self.segments = segments
         @resource.child('foo/bar')
         def static_child(self, request, segments):
             return self.__class__(self.segments + ['foo', 'bar'] + segments), []
         def __call__(self, request):
             return http.ok([('Content-Type', 'text/plain')], '/'.join(self.segments))
     A = app.RestishApp(Resource())
     R = wsgi_out(A, http.Request.blank('/foo/bar').environ)
     assert R['status'].startswith('200')
     assert R['body'] == 'foo/bar'
     R = wsgi_out(A, http.Request.blank('/foo/bar/a/b/c').environ)
     assert R['status'].startswith('200')
     assert R['body'] == 'foo/bar/a/b/c'
Пример #2
0
 def test_matcher_404(self):
     class Resource(resource.Resource):
         @resource.child(resource.any)
         def child(self, request, segments):
             return
     A = app.RestishApp(Resource())
     R = wsgi_out(A, http.Request.blank('/404').environ)
     assert R['status'].startswith('404')
Пример #3
0
 def test_explicitly_named(self):
     class Resource(resource.Resource):
         def __init__(self, segments=[]):
             self.segments = segments
         @resource.child('explicitly_named_child')
         def find_me_a_child(self, request, segments):
             return self.__class__(self.segments + ['explicitly_named_child'])
         @resource.child(u'éxpliçítly_nämed_child_with_unicøde')
         def find_me_a_child_with_unicode(self, request, segments):
             return self.__class__(self.segments + ['explicitly_named_child_with_unicode'])
         def __call__(self, request):
             return http.ok([('Content-Type', 'text/plain')], '/'.join(self.segments))
     A = app.RestishApp(Resource())
     R = wsgi_out(A, http.Request.blank('/explicitly_named_child').environ)
     assert R['status'].startswith('200')
     assert R['body'] == 'explicitly_named_child'
     R = wsgi_out(A, http.Request.blank(url.join_path([u'éxpliçítly_nämed_child_with_unicøde'])).environ)
     assert R['status'].startswith('200')
     assert R['body'] == 'explicitly_named_child_with_unicode'
Пример #4
0
 def test_nameless_child(self):
     class Resource(resource.Resource):
         def __init__(self, segments=[]):
             self.segments = segments
         @resource.child()
         def foo(self, request, segments):
             return self.__class__(self.segments + ['foo'])
         @resource.child('')
         def nameless_child(self, request, segments):
             return self.__class__(self.segments + [''])
         def __call__(self, request):
             return http.ok([('Content-Type', 'text/plain')], '/'.join(self.segments))
     A = app.RestishApp(Resource())
     R = wsgi_out(A, http.Request.blank('/foo/').environ)
     assert R['status'].startswith('200')
     assert R['body'] == 'foo/'
     R = wsgi_out(A, http.Request.blank('/foo//foo/foo///').environ)
     assert R['status'].startswith('200')
     assert R['body'] == 'foo//foo/foo///'
Пример #5
0
 def test_segment_consumption(self):
     class Resource(resource.Resource):
         def __init__(self, segments=[]):
             self.segments = segments
         @resource.child()
         def first(self, request, segments):
             return self.__class__(self.segments + ['first'] + segments), []
         def __call__(self, request):
             return http.ok([('Content-Type', 'text/plain')], '/'.join(self.segments))
     A = app.RestishApp(Resource())
     R = wsgi_out(A, http.Request.blank('/first').environ)
     assert R['status'].startswith('200')
     assert R['body'] == 'first'
     R = wsgi_out(A, http.Request.blank('/first/second').environ)
     assert R['status'].startswith('200')
     assert R['body'] == 'first/second'
     R = wsgi_out(A, http.Request.blank('/first/a/b/c/d/e').environ)
     assert R['status'].startswith('200')
     assert R['body'] == 'first/a/b/c/d/e'
Пример #6
0
 def test_any_match(self):
     class Resource(resource.Resource):
         def __init__(self, segments=[]):
             self.segments = segments
         @resource.child(resource.any)
         def any_child(self, request, segments):
             return self.__class__(self.segments + segments), []
         def __call__(self, request):
             return http.ok([('Content-Type', 'text/plain')], '%r' % (self.segments,))
     A = app.RestishApp(Resource())
     R = wsgi_out(A, http.Request.blank('/foo').environ)
     assert R['status'].startswith('200')
     assert R['body'] == "[u'foo']"
Пример #7
0
 def test_dynamic_match(self):
     class Resource(resource.Resource):
         def __init__(self, segments=[], args={}):
             self.segments = segments
             self.args = args
         @resource.child('users/{username}')
         def dynamic_child(self, request, segments, **kwargs):
             return self.__class__(self.segments + ['users', kwargs['username']] + segments, kwargs), []
         def __call__(self, request):
             body = '%r %r' % (self.segments, self.args)
             return http.ok([('Content-Type', 'text/plain')], body)
     A = app.RestishApp(Resource())
     R = wsgi_out(A, http.Request.blank('/users/foo').environ)
     assert R['status'].startswith('200')
     assert R['body'] == "['users', u'foo'] {'username': u'foo'}"
Пример #8
0
 def test_unquoted(self):
     """
     Check match args are unquoted.
     """
     class Resource(resource.Resource):
         def __init__(self, match=None):
             self.match = match
         @resource.child('{match}')
         def child(self, request, segments, match):
             return Resource(match)
         @resource.GET()
         def GET(self, request):
             return http.ok([], self.match.encode('utf-8'))
     A = app.RestishApp(Resource())
     R = wsgi_out(A, http.Request.blank('/%C2%A3').environ)
     assert R['body'] == '£'
Пример #9
0
 def test_specificity(self):
     """
     Check the child match specificity.
     """
     def make_resource(body):
         def resource(request):
             return http.ok([], body)
         return resource
     class Resource(resource.Resource):
         @resource.child('a/b/c')
         def _1(self, request, segments):
             return make_resource('a/b/c'), []
         @resource.child('a/b/{c}')
         def _2(self, request, segments, c):
             return make_resource('a/b/{c}'), []
         @resource.child('a/{b}/c/{d}')
         def _3(self, request, segments, b, d):
             return make_resource('a/{b}/c/{d}'), []
         @resource.child('a/b/{c}/{d}')
         def _4(self, request, segments, c, d):
             return make_resource('a/b/{c}/{d}'), []
         @resource.child('a/{b}/{c}')
         def _5(self, request, segments, b, c):
             return make_resource('a/{b}/{c}'), []
         @resource.child('a')
         def _6(self, request, segments):
             return make_resource('a'), []
         @resource.child('{a}/b/c')
         def _7(self, request, segments, a):
             return make_resource('{a}/b/c'), []
         @resource.child(resource.any)
         def any(self, request, segments):
             return make_resource('any'), []
     tests = [
             ('/a/b/c', 'a/b/c'),
             ('/a/b/foo', 'a/b/{c}'),
             ('/a/foo/c/bar', 'a/{b}/c/{d}'),
             ('/a/b/foo/bar', 'a/b/{c}/{d}'),
             ('/a/foo/bar', 'a/{b}/{c}'),
             ('/a', 'a'),
             ('/foo/b/c', '{a}/b/c'),
             ('/foo', 'any'),
             ]
     A = app.RestishApp(Resource())
     for path, expected in tests:
         R = wsgi_out(A, http.Request.blank(path).environ)
         assert R['body'] == expected
Пример #10
0
 def test_file(self):
     def file_closing_iter(f):
         try:
             while True:
                 data = f.read(100)
                 if not data:
                     return
                 yield data
         finally:
             f.close()
     (fd, filename) = tempfile.mkstemp()
     f = os.fdopen(fd, 'w')
     f.write('file')
     f.close()
     f = open(filename)
     R = wsgi_out(app.RestishApp(Resource(file_closing_iter(f))),
                  http.Request.blank('/').environ)
     assert R['status'].startswith('200')
     assert R['body'] == 'file'
     assert f.closed
     os.remove(filename)
Пример #11
0
 def test_404(self):
     class Resource(resource.Resource):
         pass
     A = app.RestishApp(Resource())
     R = wsgi_out(A, http.Request.blank('/404').environ)
     assert R['status'].startswith('404')
Пример #12
0
 def test_cstringio(self):
     R = wsgi_out(app.RestishApp(Resource(cStringIO.StringIO('cstringio'))),
                  http.Request.blank('/').environ)
     assert R['status'].startswith('200')
     assert R['body'] == 'cstringio'
Пример #13
0
 def test_string(self):
     R = wsgi_out(app.RestishApp(Resource('string')),
                  http.Request.blank('/').environ)
     assert R['status'].startswith('200')
     assert R['body'] == 'string'