Commits

Atsushi Odagiri committed fa59335

callable predication.

Comments (0)

Files changed (1)

wsgiheaders/__init__.py

 >>> def xrds(environ):
 ...     return [('X-XRDS', environ['wsgi.url_scheme'] + '://' + environ['SERVER_NAME'] + '/svc.xrds')]
 ... 
->>> @addheader([('http://.*', xrds)])
+>>> def predicate(environ):
+...     return environ.get('HTTP_ACCEPT', '').find('application/xrds+xml') != -1
+... 
+>>> @addheader([(predicate, xrds)])
 ... def app(environ, start_response):
 ...     start_response('200 OK',
 ...                    [('Content-type', 'text/plain')])
 ...     return ['Hello, world!']
 >>> app = webtest.TestApp(app)
 >>> res = app.get('/')
+>>> 'X-XRDS' in res.headers
+False
+>>> res = app.get('/', headers={'Accept':'application/xrds+xml'})
 >>> res.headers['X-XRDS']
 'http://localhost/svc.xrds'
 """
             req = Request(environ)
             res = req.get_response(app)
             for pred, headers in headerConditions:
-                if pred.match(req.url):
+                if callable(pred):
+		    flg = pred(environ)
+                else:
+                    flg = pred.match(req.url)
+
+                if flg:
                     if callable(headers):
                         headers = headers(environ)
                     for name, value in headers: