def test_injector_multiple_identical_inclusions_through_dependencies(): bower = bowerstatic.Bower() components = bower.components( 'components', os.path.join(os.path.dirname(__file__), 'bower_components')) def wsgi(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html;charset=UTF-8')]) include = components.includer(environ) # going to pull in jquery-ui and jquery twice include('jquery-ui') include('jquery-ui-bootstrap') return [b'<html><head></head><body>Hello!</body></html>'] injector = bower.injector(wsgi) c = Client(injector) response = c.get('/') assert response.body == ( b'<html><head>' b'<script type="text/javascript" ' b'src="/bowerstatic/components/jquery/2.1.1/dist/jquery.js">' b'</script>\n' b'<script type="text/javascript" ' b'src="/bowerstatic/components/jquery-ui/1.10.4/ui/jquery-ui.js">' b'</script>\n' b'<link rel="stylesheet" type="text/css" ' b'href="/bowerstatic/components/jquery-ui-bootstrap/0.2.5/' b'jquery.ui.theme.css">' b'</head><body>Hello!</body></html>')
def test_injector_endpoint_resource(): bower = bowerstatic.Bower() components = bower.components( 'components', os.path.join(os.path.dirname(__file__), 'bower_components')) jquery = components.resource('jquery') def wsgi(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html;charset=UTF-8')]) include = components.includer(environ) include(jquery) return [b'<html><head></head><body>Hello!</body></html>'] injector = bower.injector(wsgi) c = Client(injector) response = c.get('/') assert response.body == ( b'<html><head>' b'<script type="text/javascript" ' b'src="/bowerstatic/components/jquery/2.1.1/dist/jquery.js">' b'</script></head><body>Hello!</body></html>')
def test_injector_custom_renderer_callable(): bower = bowerstatic.Bower() components = bower.components( 'components', os.path.join(os.path.dirname(__file__), 'bower_components')) def custom_renderer(resource): return '<link src="%s">' % resource.url() def wsgi(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html;charset=UTF-8')]) include = components.includer(environ) include('jquery', custom_renderer) return [b'<html><head></head><body>Hello!</body></html>'] injector = bower.injector(wsgi) c = Client(injector) response = c.get('/') assert response.body == ( b'<html><head>' b'<link src="/bowerstatic/components/jquery/2.1.1/dist/jquery.js">' b'</head><body>Hello!</body></html>')
def test_injector_normal_dependencies_explicit_resource_objects(): bower = bowerstatic.Bower() components = bower.components( 'components', os.path.join(os.path.dirname(__file__), 'bower_components')) jquery_min = components.resource('jquery/dist/jquery.min.js') jquery_ui_min = components.resource( 'jquery-ui/ui/minified/jquery-ui.min.js', dependencies=[jquery_min]) def wsgi(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html;charset=UTF-8')]) include = components.includer(environ) include(jquery_ui_min) return [b'<html><head></head><body>Hello!</body></html>'] injector = bower.injector(wsgi) c = Client(injector) response = c.get('/') assert response.body == ( b'<html><head>' b'<script type="text/javascript" ' b'src="/bowerstatic/components/jquery/2.1.1/dist/jquery.min.js">' b'</script>\n' b'<script type="text/javascript" ' b'src="/bowerstatic/components/jquery-ui/' b'1.10.4/ui/minified/jquery-ui.min.js">' b'</script>' b'</head><body>Hello!</body></html>')
def test_local_with_local_component_specific_file(): bower = bowerstatic.Bower() components = bower.components('components', os.path.join( os.path.dirname(__file__), 'bower_components')) local = bower.local_components('local', components) path = os.path.join( os.path.dirname(__file__), 'local_component') local.component(path, version='2.0') def wsgi(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html;charset=UTF-8')]) include = local.includer(environ) include('local_component/local.js') return [b'<html><head></head><body>Hello!</body></html>'] wrapped = bower.wrap(wsgi) c = Client(wrapped) response = c.get('/') assert response.body == ( b'<html><head>' b'<script type="text/javascript" ' b'src="/bowerstatic/local/local_component/2.0/local.js">' b'</script></head><body>Hello!</body></html>') response = c.get('/bowerstatic/local/local_component/2.0/local.js') assert response.body == b'/* this is local.js */\n'
def test_injector_missing_version_bower_components(): bower = bowerstatic.Bower() components = bower.components( 'components', os.path.join(os.path.dirname(__file__), 'bower_components')) def wsgi(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html;charset=UTF-8')]) include = components.includer(environ) include('missing-version-in-dot-bower-json') return [b'<html><head></head><body>Hello!</body></html>'] injector = bower.injector(wsgi) c = Client(injector) response = c.get('/') # without a main, it should just include nothing assert response.body == ( b'<html><head>' b'<script type="text/javascript" ' b'src="/bowerstatic/components/missing-version-in-dot-bower-json/' b'1.0/example.js"></script>' b'</head><body>Hello!</body></html>')
def test_wrap(): bower = bowerstatic.Bower() components = bower.components( 'components', os.path.join(os.path.dirname(__file__), 'bower_components')) def wsgi(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html;charset=UTF-8')]) include = components.includer(environ) include('jquery/dist/jquery.js') return [b'<html><head></head><body>Hello!</body></html>'] wrapped = bower.wrap(wsgi) c = Client(wrapped) response = c.get('/') assert response.body == ( b'<html><head>' b'<script type="text/javascript" ' b'src="/bowerstatic/components/jquery/2.1.1/dist/jquery.js">' b'</script></head><body>Hello!</body></html>') response = c.get('/bowerstatic/components/jquery/2.1.1/dist/jquery.js') assert response.body == b'/* jquery.js 2.1.1 */\n'
def test_custom_renderer(): bower = bowerstatic.Bower() def render_foo(resource): return '<foo>%s</foo>' % resource.url() bower.register_renderer('.foo', render_foo) components = bower.components( 'components', os.path.join(os.path.dirname(__file__), 'bower_components')) def wsgi(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html;charset=UTF-8')]) include = components.includer(environ) include('jquery/dist/resource.foo') return [b'<html><head></head><body>Hello!</body></html>'] injector = bower.injector(wsgi) c = Client(injector) response = c.get('/') assert response.body == ( b'<html><head><foo>/bowerstatic/components/jquery/2.1.1/dist/' b'resource.foo</foo>' b'</head><body>Hello!</body></html>')
def test_relative_path_bower_components(): bower = bowerstatic.Bower() components = bower.components( 'components', bowerstatic.module_relative_path('bower_components')) assert os.path.join(os.path.dirname(__file__), 'bower_components') == components.path
def test_component_url(): bower = bowerstatic.Bower() components = bower.components( 'components', os.path.join(os.path.dirname(__file__), 'bower_components')) assert (components.get_component('jquery').url() == '/bowerstatic/components/jquery/2.1.1/')
def test_nice_error_message_when_depending_on_not_existing_dependency(): bower = bowerstatic.Bower() with pytest.raises(bowerstatic.error.Error) as excinfo: bower.components( 'components', os.path.join(os.path.dirname(__file__), 'bower_components_error')) assert "Component i-do-not-exist missing." == str(excinfo.value)
def test_relative_path_local_components(): bower = bowerstatic.Bower() components = bower.components( 'components', bowerstatic.module_relative_path('bower_components')) local = bower.local_components('local', components) local_component = local.component( bowerstatic.module_relative_path('local_component'), None) assert os.path.join(os.path.dirname(__file__), 'local_component') == local_component.path
def c(): bower = bowerstatic.Bower() bower.components('components', os.path.join( os.path.dirname(__file__), 'bower_components')) def wsgi(environ, start_response): start_response('200 OK', [('Content-Type', 'text/plain')]) return [b'Hello!'] publisher = bower.publisher(wsgi) return Client(publisher)
def test_local_with_missing_version(): bower = bowerstatic.Bower() components = bower.components('components', os.path.join( os.path.dirname(__file__), 'bower_components')) local = bower.local_components('local', components) path = os.path.join( os.path.dirname(__file__), 'local_component_missing_version') with pytest.raises(ValueError) as err: local.component(path, version=None) assert str(err.value).startswith('Missing _release and version in') assert str(err.value).endswith('/tests/local_component_missing_version')
def test_injector_no_inclusions(): bower = bowerstatic.Bower() bower.components( 'components', os.path.join(os.path.dirname(__file__), 'bower_components')) def wsgi(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html;charset=UTF-8')]) return [b'<html><head></head><body>Hello!</body></html>'] injector = bower.injector(wsgi) c = Client(injector) response = c.get('/') assert response.body == b'<html><head></head><body>Hello!</body></html>'
def test_different_publisher_signature(): bower = bowerstatic.Bower(publisher_signature='static') bower.components('components', os.path.join( os.path.dirname(__file__), 'bower_components')) def wsgi(environ, start_response): start_response('200 OK', [('Content-Type', 'text/plain')]) return [b'Hello!'] publisher = bower.publisher(wsgi) c = Client(publisher) response = c.get('/') assert response.body == b'Hello!' response = c.get( '/static/components/jquery/2.1.1/dist/jquery.js') assert response.body == b'/* jquery.js 2.1.1 */\n'
def test_injector_not_html_no_effect(): bower = bowerstatic.Bower() components = bower.components( 'components', os.path.join(os.path.dirname(__file__), 'bower_components')) def wsgi(environ, start_response): start_response('200 OK', [('Content-Type', 'text/plain')]) include = components.includer(environ) include('jquery/dist/jquery.js') return [b'Hello!'] injector = bower.injector(wsgi) c = Client(injector) response = c.get('/') assert response.body == b'Hello!'
def test_injector_no_content_type_set(): bower = bowerstatic.Bower() components = bower.components( 'components', os.path.join(os.path.dirname(__file__), 'bower_components')) def wsgi(environ, start_response): start_response('200 OK', []) include = components.includer(environ) include('jquery/dist/jquery.js') return [b'SOME-BINARY-OR-NOT-HTML-DATA'] injector = bower.injector(wsgi) c = Client(injector) response = c.get('/') assert response.body == (b'SOME-BINARY-OR-NOT-HTML-DATA')
def test_missing_renderer(): bower = bowerstatic.Bower() components = bower.components( 'components', os.path.join(os.path.dirname(__file__), 'bower_components')) def wsgi(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html;charset=UTF-8')]) include = components.includer(environ) include('jquery/dist/resource.foo') return [b'<html><head></head><body>Hello!</body></html>'] injector = bower.injector(wsgi) c = Client(injector) with pytest.raises(bowerstatic.Error): c.get('/')
def test_injector_PUT_no_effect(): bower = bowerstatic.Bower() components = bower.components( 'components', os.path.join(os.path.dirname(__file__), 'bower_components')) def wsgi(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html;charset=UTF-8')]) include = components.includer(environ) include('jquery/dist/jquery.js') return [b'<html><head></head><body>Hello!</body></html>'] injector = bower.injector(wsgi) c = Client(injector) response = c.put('/') assert response.body == b'<html><head></head><body>Hello!</body></html>'
def test_injector_specific_path_wrong_file_then_added(tmpdir): bower_components_dir = tmpdir.mkdir('bower_components') component_dir = bower_components_dir.mkdir('component') bower_json_file = component_dir.join('.bower.json') bower_json_file.write( json.dumps({ 'name': 'component', 'version': '2.1', 'main': 'main.js' })) main_js_file = component_dir.join('main.js') main_js_file.write('/* this is main.js */') bower = bowerstatic.Bower() components = bower.components('components', bower_components_dir.strpath) def wsgi(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html;charset=UTF-8')]) include = components.includer(environ) include('component/notyet.js') return [b'<html><head></head><body>Hello!</body></html>'] injector = bower.injector(wsgi) c = Client(injector) with pytest.raises(bowerstatic.Error): c.get('/') # now we add the nonexistent file notyet_file = component_dir.join('notyet.js') notyet_file.write('/* this is notyet.js */') response = c.get('/') assert response.body == ( b'<html><head>' b'<script type="text/javascript" ' b'src="/bowerstatic/components/component/2.1/notyet.js">' b'</script></head><body>Hello!</body></html>')
def test_injector_endpoint_main_missing(): bower = bowerstatic.Bower() components = bower.components( 'components', os.path.join(os.path.dirname(__file__), 'bower_components')) def wsgi(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html;charset=UTF-8')]) include = components.includer(environ) include('missing_main') return [b'<html><head></head><body>Hello!</body></html>'] injector = bower.injector(wsgi) c = Client(injector) response = c.get('/') # without a main, it should just include nothing assert response.body == (b'<html><head></head><body>Hello!</body></html>')
def test_injector_inline_renderer(): bower = bowerstatic.Bower() components = bower.components( 'components', os.path.join(os.path.dirname(__file__), 'bower_components')) def wsgi(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html;charset=UTF-8')]) include = components.includer(environ) include('jquery', bowerstatic.renderer.render_inline_js) return [b'<html><head></head><body>Hello!</body></html>'] injector = bower.injector(wsgi) c = Client(injector) response = c.get('/') assert response.body == ( b'<html><head><script type="text/javascript">/* jquery.js 2.1.1 */\n' b'</script></head><body>Hello!</body></html>')
def test_local_external_dependencies(): bower = bowerstatic.Bower() components = bower.components('components', os.path.join( os.path.dirname(__file__), 'bower_components')) local = bower.local_components('local', components) path = os.path.join( os.path.dirname(__file__), 'local_component') local.component(path, version='2.0') local.resource('local_component/local.js', dependencies=[ 'jquery']) def wsgi(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html;charset=UTF-8')]) include = local.includer(environ) include('local_component/local.js') return [b'<html><head></head><body>Hello!</body></html>'] wrapped = bower.wrap(wsgi) c = Client(wrapped) response = c.get('/') assert response.body == ( b'<html><head>' b'<script type="text/javascript" ' b'src="/bowerstatic/components/jquery/2.1.1/dist/jquery.js">' b'</script>\n' b'<script type="text/javascript" ' b'src="/bowerstatic/local/local_component/2.0/local.js"></script>' b'</head><body>Hello!</body></html>')
def test_injector_does_not_fail_for_401_responses_with_no_content_type(): bower = bowerstatic.Bower() components = bower.components( 'components', os.path.join(os.path.dirname(__file__), 'bower_components')) def wsgi(environ, start_response): # Can not use 401 here as webtest only accepts 200 or 3xx, which is ok # as we want to test the behaviour if no content type is given start_response('302', [('Content-Type', None)]) include = components.includer(environ) with pytest.raises(bowerstatic.Error): include('jquery/nonexistent.js') return [b'<html><head></head><body>Hello!</body></html>'] injector = bower.injector(wsgi) c = Client(injector) # webtest checks, in contracy to pyramid, the headers and breaks if one of # them is not a string. with mock.patch('webtest.lint.check_headers'): c.get('/')
def test_local_with_second_auto_version(tmpdir): # need to cut things a bit of slack, as filesystem time can apparently # be ahead slightly after_dt = datetime.now() - timedelta(seconds=1) # create a bower component directory component_dir = tmpdir.mkdir('component') bower_json_file = component_dir.join('bower.json') bower_json_file.write(json.dumps({ 'name': 'component', 'version': '2.1', # should be ignored 'main': 'main.js' })) main_js_file = component_dir.join('main.js') main_js_file.write('/* this is main.js */') # now expose it through local # the default autoversioning scheme uses second granularity bower = bowerstatic.Bower() components = bower.components('components', os.path.join( os.path.dirname(__file__), 'bower_components')) local = bower.local_components('local', components) local.component(component_dir.strpath, version=None) def wsgi(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html;charset=UTF-8')]) include = local.includer(environ) include('component/main.js') return [b'<html><head></head><body>Hello!</body></html>'] wrapped = bower.wrap(wsgi) c = Client(wrapped) response = c.get('/') before_dt = datetime.now() def get_url_dt(response): s = compat.text_type(response.body, 'UTF-8') start = s.find('src="') + len('src="') end = s.find('"', start) url = s[start:end] parts = url.split('/') url_dt_str = parts[4] url_dt = datetime.strptime(url_dt_str, '%Y-%m-%dT%H:%M:%S') return url_dt_str, url_dt url_dt_str, url_dt = get_url_dt(response) assert url_dt >= after_dt assert url_dt <= before_dt response = c.get('/bowerstatic/local/component/%s/main.js' % url_dt_str) assert response.body == b'/* this is main.js */' after_dt = datetime.now() - timedelta(seconds=1) # now we modify a file main_js_file.write('/* this is main.js, modified */') response = c.get('/') before_dt = datetime.now() original_url_dt = url_dt url_dt_str, url_dt = get_url_dt(response) assert url_dt >= after_dt assert url_dt <= before_dt assert url_dt >= original_url_dt response = c.get('/bowerstatic/local/component/%s/main.js' % url_dt_str) assert response.body == b'/* this is main.js, modified */'
import bowerstatic import os from .app import App bower = bowerstatic.Bower() components = bower.components( 'app', os.path.join(os.path.dirname(__file__), 'bower_components')) local = bower.local_components('local', components) local.component(os.path.join(os.path.dirname(__file__), 'my_component'), version=None) @App.static_components() def get_static_components(): return local
def get_static_includer(): bower = bowerstatic.Bower() components = bower.components( 'myapp', bowerstatic.module_relative_path('bower_components')) return components