Commits

Jan-Jaap Driessen  committed 66a74e0 Merge

Merged branch janjaapdriessen-ticket#76-pullrequest#1 , cherry picking pull request#1.

  • Participants
  • Parent commits 981b625, 7bc2c1b

Comments (0)

Files changed (4)

File fanstatic/core.py

 
     :param base_url: This URL will be prefixed in front of all resource
       URLs. This can be useful if your web framework wants the resources
-      to be published on a sub-URL. Note that this can also be set
-      with the set_base_url method on a ``NeededResources`` instance.
+      to be published on a sub-URL. By default, there is no ``base_url``,
+      and resources are served in the script root. Note that this can
+      also be set with the set_base_url method on a ``NeededResources``
+      instance.
+
+    :param script_name: The script_name is a fallback for computing
+      library URLs. The base_url parameter should be honoured if
+      it is provided.
 
     :param publisher_signature: The name under which resource libraries
       should be served in the URL. By default this is ``fanstatic``, so
                  debug=False,
                  rollup=False,
                  base_url=None,
+                 script_name=None,
                  publisher_signature=DEFAULT_SIGNATURE,
                  bundle=False,
                  resources=None,
         self._bottom = bottom
         self._force_bottom = force_bottom
         self._base_url = base_url
+        self._script_name = script_name
         self._publisher_signature = publisher_signature
         self._rollup = rollup
         self._bundle = bundle
 
         :param library: A :py:class:`Library` instance.
         """
-        path = [self._base_url or '']
+        # The script_name is a fallback and base_url should be honoured
+        # if it is provided.
+        path = [self._base_url or self._script_name or '']
         if self._publisher_signature:
             path.append(self._publisher_signature)
         path.append(library.name)

File fanstatic/injector.py

         # XXX this will set the needed on the thread local data, even
         # if the wrapped framework only gets the needed from the WSGI
         # environ.
-        needed = fanstatic.init_needed(**self.config)
+        needed = fanstatic.init_needed(script_name=request.environ.get('SCRIPT_NAME'),
+                                       **self.config
+                                       )
 
         # Make sure the needed resource object is put in the WSGI
         # environment as well, for frameworks that choose to use it

File fanstatic/test_core.py

     assert (needed.library_url(foo) ==
             'http://example.com/something/fanstatic/foo')
 
+def test_library_url_script_name():
+    foo = Library('foo', '')
+    needed = NeededResources(script_name='/root')
+    assert needed.library_url(foo) == '/root/fanstatic/foo'
+
+def test_library_url_script_name_base_url():
+    foo = Library('foo', '')
+    needed = NeededResources(
+        script_name='/root', base_url="http://example.com/something")
+
+    # base_url is set so script_name should be ignored
+    assert (needed.library_url(foo) ==
+            'http://example.com/something/fanstatic/foo')
 
 def test_library_url_version_hashing(tmpdir):
     foo = Library('foo', tmpdir.strpath)

File fanstatic/test_wsgi.py

     wrapped_app = Fanstatic(app, base_url='http://testapp')
 
     request = webob.Request.blank('/')
+    request.environ['SCRIPT_NAME'] = '/root' # base_url is defined so SCRIPT_NAME
+                                             # shouldn't be taken into account
     response = request.get_response(wrapped_app)
     assert response.body == '''\
 <html><head>
 <script type="text/javascript" src="http://testapp/fanstatic/foo/c.js"></script>
 </head><body</body></html>'''
 
+def test_inject_script_name():
+    foo = Library('foo', '')
+    x1 = Resource(foo, 'a.js')
+    x2 = Resource(foo, 'b.css')
+    y1 = Resource(foo, 'c.js', depends=[x1, x2])
+
+    def app(environ, start_response):
+        start_response('200 OK', [])
+        needed = get_needed()
+        needed.need(y1)
+        return ['<html><head></head><body</body></html>']
+
+    wrapped_app = Fanstatic(app)
+
+    request = webob.Request.blank('/path')
+    request.environ['SCRIPT_NAME'] = '/root'
+    response = request.get_response(wrapped_app)
+    assert response.body == '''\
+<html><head>
+    <link rel="stylesheet" type="text/css" href="/root/fanstatic/foo/b.css" />
+<script type="text/javascript" src="/root/fanstatic/foo/a.js"></script>
+<script type="text/javascript" src="/root/fanstatic/foo/c.js"></script>
+</head><body</body></html>'''
 
 def test_incorrect_configuration_options():
     app = None