def test_child_is_a_response(self): class Resource(resource.Resource): @resource.child() def foo(self, request, segments): return http.ok([('Content-Type', 'text/plain')], 'foobar') # Check a leaf child (no more segments). A = app.RestishApp(Resource()) R = webtest.TestApp(A).get('/foo') assert R.body == 'foobar' # Check a branch child (additional segments) A = app.RestishApp(Resource()) R = webtest.TestApp(A).get('/foo/bar') assert R.body == 'foobar'
def test_resource_returns_resource(self): """ Check resources can return other resources. """ class WrappedResource(resource.Resource): def __init__(self, segments=None): self.segments = segments def resource_child(self, request, segments): return self.__class__(segments), [] def __call__(self, request): return http.ok([('Content-Type', 'text/plain')], repr(self.segments)) class WrapperResource(object): def resource_child(self, request, segments): return WrappedResource() def __call__(self, request): return WrappedResource() A = app.RestishApp(WrapperResource()) # Call the wrapper R = webtest.TestApp(A).get('/', status=200) assert R.body == "None" # Call a child of the wrapper R = webtest.TestApp(A).get('/foo/bar', status=200) assert R.body == "[u'foo', u'bar']"
def test_not_found_on_none(self): class Resource(resource.Resource): def resource_child(self, request, segments): return None A = app.RestishApp(Resource()) R = webtest.TestApp(A).get('/not_found', status=404)
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 = webtest.TestApp(A).get('/explicitly_named_child') assert R.status.startswith('200') assert R.body == 'explicitly_named_child' R = webtest.TestApp(A).get( url.join_path([u'éxpliçítly_nämed_child_with_unicøde'])) assert R.status.startswith('200') assert R.body == 'explicitly_named_child_with_unicode'
def test_server_error(self): class Resource(resource.Resource): def __call__(self, request): raise http.BadGatewayError() A = app.RestishApp(Resource()) R = webtest.TestApp(A).get('/', status=502)
def test_client_error(self): class Resource(resource.Resource): def __call__(self, request): raise http.BadRequestError() A = app.RestishApp(Resource()) R = webtest.TestApp(A).get('/', status=400)
def test_see_other_headers(self): r = http.see_other('/', [('Set-Cookie', 'name=value')]) # Pass through WebTest for lint-like checks webtest.TestApp(app.RestishApp(r)).get('/') # Test response details. assert r.headers['Location'] == '/' assert r.headers['Set-Cookie'] == 'name=value'
def test_moved_permanently_headers(self): r = http.moved_permanently('/', headers=[('Set-Cookie', 'name=value')]) # Pass through WebTest for lint-like checks webtest.TestApp(app.RestishApp(r)).get('/') # Test response details. assert r.headers['Location'] == '/' assert r.headers['Set-Cookie'] == 'name=value'
def test_root_wsgi_resource(self): """ Test a WSGIResource that is the root resource. """ testapp = webtest.TestApp(app.RestishApp(util.WSGIResource(wsgi_app))) response = testapp.get('/foo/bar', status=200) assert response.headers['Content-Type'] == 'text/plain' assert response.body == 'SCRIPT_NAME: , PATH_INFO: /foo/bar'
def test_matcher_404(self): class Resource(resource.Resource): @resource.child(resource.any) def child(self, request, segments): return A = app.RestishApp(Resource()) R = webtest.TestApp(A).get('/404', status=404)
def test_resource_returns_resource_when_called(self): class WrapperResource(resource.Resource): def __call__(self, request): return Resource('root') A = app.RestishApp(WrapperResource()) R = webtest.TestApp(A).get('/', status=200) assert R.body == 'root'
def test_match_names_with_regex_chars(self): class Resource(resource.Resource): @resource.child('[0-9]*+') def static_child(self, request, segments): return http.ok([('Content-Type', 'text/plain')], 'static') A = app.RestishApp(Resource()) R = webtest.TestApp(A).get('/[0-9]*+') assert R.status.startswith('200') assert R.body == 'static'
def test_moved_permanently(self): location = 'http://localhost/abc?a=1&b=2' r = http.moved_permanently(location) # Pass through WebTest for lint-like checks webtest.TestApp(app.RestishApp(r)).get('/') # Test response details. assert r.status.startswith('301') assert r.headers['Location'] == location assert r.headers['Content-Length'] assert '301 Moved Permanently' in r.body assert cgi.escape(location) in r.body
def test_children(self): A = app.RestishApp( Resource('root', { 'foo': Resource('foo'), 'bar': Resource('bar') })) R = webtest.TestApp(A).get('/', status=200) assert R.body == 'root' R = webtest.TestApp(A).get('/foo', status=200) assert R.body == 'foo' R = webtest.TestApp(A).get('/bar', status=200) assert R.body == 'bar'
def test_resource_returns_func(self): def func(request): return http.ok([('Content-Type', 'text/plain')], 'func') class WrapperResource(resource.Resource): @resource.GET() def GET(self, request): return func A = app.RestishApp(WrapperResource()) R = webtest.TestApp(A).get('/', status=200) assert R.body == 'func'
def test_child_wsgi_resource(self): """ Test a WSGIResource that is a child of the root resource. """ class Root(object): def resource_child(self, request, segments): return util.WSGIResource(wsgi_app), segments[1:] testapp = webtest.TestApp(app.RestishApp(Root())) response = testapp.get('/foo/bar', status=200) assert response.headers['Content-Type'] == 'text/plain' assert response.body == 'SCRIPT_NAME: /foo, PATH_INFO: /bar'
def test_iterable_response_body(self): def resource(request): def gen(): yield "Three ... " yield "two ... " yield "one ... " yield "BANG!" return http.ok([('Content-Type', 'text/plain')], gen()) A = app.RestishApp(resource) assert webtest.TestApp(A).get( '/').body == 'Three ... two ... one ... BANG!'
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 = webtest.TestApp(A).get('/foo') assert R.status.startswith('200') assert R.body == "[u'foo']"
def test_weird_path_segments(self): class Resource(resource.Resource): def __init__(self, segment): self.segment = segment def resource_child(self, request, segments): return self.__class__(segments[0]), segments[1:] def __call__(self, request): return http.ok([('Content-Type', 'text/plain')], self.segment.encode('utf-8')) A = app.RestishApp(Resource('')) assert webtest.TestApp(A).get('/').body == '' assert webtest.TestApp(A).get('/foo').body == 'foo' assert webtest.TestApp(A).get( url.URL('/').child( '*****@*****.**').path).body == '*****@*****.**'
def test_implicitly_named(self): class Resource(resource.Resource): def __init__(self, segments=[]): self.segments = segments @resource.child() def implicitly_named_child(self, request, segments): return self.__class__(self.segments + ['implicitly_named_child']) def __call__(self, request): return http.ok([('Content-Type', 'text/plain')], '/'.join(self.segments)) A = app.RestishApp(Resource()) R = webtest.TestApp(A).get('/implicitly_named_child') assert R.status.startswith('200') assert R.body == 'implicitly_named_child'
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([('Content-Type', 'text/plain')], self.match.encode('utf-8')) A = app.RestishApp(Resource()) R = webtest.TestApp(A).get('/%C2%A3') assert R.body == '£'
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 = webtest.TestApp(A).get('/users/foo') assert R.status.startswith('200') assert R.body == "['users', u'foo'] {'username': u'foo'}"
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).encode('utf-8')) A = app.RestishApp(Resource()) R = webtest.TestApp(A).get('/foo/bar') assert R.status.startswith('200') assert R.body == 'foo/bar' R = webtest.TestApp(A).get('/foo/bar/a/b/c') assert R.status.startswith('200') assert R.body == 'foo/bar/a/b/c'
def test_accepting_resource_returns_resource(self): """ Check resource with accept match that returns another resource. This test was added because of a bug in resource.Resource where it assumed the request handler would return a response object but only broke when some accept matching had occurred. """ class WrappedResource(resource.Resource): def __call__(self, request): return http.ok([('Content-Type', 'text/html')], "WrappedResource") class WrapperResource(resource.Resource): @resource.GET(accept='html') def html(self, request): return WrappedResource() A = app.RestishApp(WrapperResource()) R = webtest.TestApp(A).get('/', status=200) assert R.body == "WrappedResource"
def test_no_root_application(self): class Resource(resource.Resource): def __init__(self, segment): self.segment = segment def resource_child(self, request, segments): return self.__class__(segments[0]), segments[1:] def __call__(self, request): return http.ok([('Content-Type', 'text/plain')], self.segment.encode('utf-8')) A = app.RestishApp(Resource('')) assert webtest.TestApp(A).get('/').body == '' assert webtest.TestApp(A).get('/', extra_environ={ 'SCRIPT_NAME': '/base' }).body == '' assert webtest.TestApp(A).get('/foo', extra_environ={ 'SCRIPT_NAME': '/base' }).body == 'foo'
def test_file(self): class FileStreamer(object): def __init__(self, f): self.f = f def __iter__(self): return self def next(self): data = self.f.read(100) if data: return data raise StopIteration() def close(self): self.f.close() (fd, filename) = tempfile.mkstemp() f = os.fdopen(fd, 'w') f.write('file') f.close() f = open(filename) R = webtest.TestApp(app.RestishApp(Resource(FileStreamer(f)))).get('/') assert R.status.startswith('200') assert R.body == 'file' assert f.closed os.remove(filename)
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).encode('utf-8')) A = app.RestishApp(Resource()) R = webtest.TestApp(A).get('/first') assert R.status.startswith('200') assert R.body == 'first' R = webtest.TestApp(A).get('/first/second') assert R.status.startswith('200') assert R.body == 'first/second' R = webtest.TestApp(A).get('/first/a/b/c/d/e') assert R.status.startswith('200') assert R.body == 'first/a/b/c/d/e'
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 = webtest.TestApp(A).get('/foo/') assert R.status.startswith('200') assert R.body == 'foo/' R = webtest.TestApp(A).get('/foo//foo/foo///') assert R.status.startswith('200') assert R.body == 'foo//foo/foo///'
def test_cstringio(self): R = webtest.TestApp(app.RestishApp(Resource(cStringIO.StringIO('cstringio')))).get('/') assert R.status.startswith('200') assert R.body == 'cstringio'
def test_string(self): R = webtest.TestApp(app.RestishApp(Resource('string'))).get('/') assert R.status.startswith('200') assert R.body == 'string'