Example #1
0
def wsgi(request, app, path_info_segments):
    """
    Low-level function to call out to another wsgi application.

    WARNING: This is only a partial implementation for now. It does not handle
    the start_response exc_info arg, nor does it handle calls to the write
    function that must be returned from start_response.
    """
    # Copy and update the environ to set new SCRIPT_NAME and PATH_INFO.
    script_segments = request.path.path_segments
    if path_info_segments:
        script_segments = script_segments[:-len(path_info_segments)]
    environ = dict(request.environ)
    environ['SCRIPT_NAME'] = url.join_path(script_segments)
    environ['PATH_INFO'] = url.join_path(path_info_segments)
    # Call the wsgi application.
    stuff = {}

    def write(body_data):
        raise NotImplementedError()

    def start_response(status, headers, exc_info=None):
        if exc_info is not None:
            raise NotImplementedError()
        stuff['status'] = status
        stuff['headers'] = headers
        return write

    result = app(environ, start_response)
    return http.Response(stuff['status'], stuff['headers'], result)
Example #2
0
def wsgi(request, app, path_info_segments):
    """
    Low-level function to call out to another wsgi application.

    WARNING: This is only a partial implementation for now. It does not handle
    the start_response exc_info arg, nor does it handle calls to the write
    function that must be returned from start_response.
    """
    # Copy and update the environ to set new SCRIPT_NAME and PATH_INFO.
    script_segments = request.path.path_segments
    if path_info_segments:
        script_segments = script_segments[:-len(path_info_segments)]
    environ = dict(request.environ)
    environ['SCRIPT_NAME'] = url.join_path(script_segments)
    environ['PATH_INFO'] = url.join_path(path_info_segments)
    # Call the wsgi application.
    stuff = {}
    def write(body_data):
        raise NotImplementedError()
    def start_response(status, headers, exc_info=None):
        if exc_info is not None:
            raise NotImplementedError()
        stuff['status'] = status
        stuff['headers'] = headers
        return write
    result = app(environ, start_response)
    return http.Response(stuff['status'], stuff['headers'], result)
Example #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 = 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'
Example #4
0
    def test_urlfor_using_object(self):
        class Data(object):
            def __init__(self, a, b, c):
                self.a = a
                self.b = b
                self.c = c

            def __str__(self):
                return (u"%s %s %s " % (self.a, self.b, self.c)).encode("utf-8")

        class Abc(resource.Resource):
            def __init__(self, a, b, c):
                self.data = Data(a, b, c)

            @resource.GET()
            def get(self, request):
                return http.ok([("Content-type", "text/plain; charset=utf-8")],
                               str(self.data))

        class Root(resource.Resource):
            data = resource.child("{a}/{b}/{c}", Abc)
        
        tests = [(("a", "b", "c"), "/a/b/c"),
                 ((1, 2, 3), "/1/2/3"),
                 ((u"£", u"$", u"€"), url.join_path([u"£", u"$", u"€"]))
                ]

        app = make_app(Root())
        for data, path in tests:
            obj = Data(*data)
            # request
            response = app.get(path, status=200)
            assert response.body == str(obj)
            # reverse url
            assert resource.url_for("abc", obj) == path
Example #5
0
 def test_join_path(self):
     self.assertEquals(url.join_path([]), '')
     self.assertEquals(url.join_path(['']), '/')
     self.assertEquals(url.join_path(['', '']), '//')
     self.assertEquals(url.join_path(['foo']), '/foo')
     self.assertEquals(url.join_path(['foo', 'bar']), '/foo/bar')
     self.assertEquals(url.join_path(['/']), '/%2F')
     self.assertEquals(url.join_path([POUND]), '/%C2%A3')
Example #6
0
 def test_join_path(self):
     self.assertEquals(url.join_path([]), '')
     self.assertEquals(url.join_path(['']), '/')
     self.assertEquals(url.join_path(['', '']), '//')
     self.assertEquals(url.join_path(['foo']), '/foo')
     self.assertEquals(url.join_path(['foo', 'bar']), '/foo/bar')
     self.assertEquals(url.join_path(['/']), '/%2F')
     self.assertEquals(url.join_path([POUND]), '/%C2%A3')
Example #7
0
 def test_join_path(self):
     self.assertEquals(url.join_path([]), "")
     self.assertEquals(url.join_path([""]), "/")
     self.assertEquals(url.join_path(["", ""]), "//")
     self.assertEquals(url.join_path(["foo"]), "/foo")
     self.assertEquals(url.join_path(["foo", "bar"]), "/foo/bar")
     self.assertEquals(url.join_path(["/"]), "/%2F")
     self.assertEquals(url.join_path([POUND]), "/%C2%A3")
Example #8
0
    def test_unicode(self):
        class Moo(resource.Resource):
            def __init__(self, arg):
                self.arg = arg
    
            def __call__(self, segments):
                return http.ok([("Content-type", "text/plain; charset=utf-8")],
                                self.arg)

        class Root(resource.Resource):
            moo = resource.child(u"£-{arg}", Moo)

        tests = [(u"£-a", u"a"),
                 (u"£-ä", u"ä")
                ]

        app = make_app(Root())
        for path, body in tests:
            response = app.get(url.join_path([path]), status=200)
            assert response.body == body.encode("utf-8")
             
            assert resource.url_for("moo", arg=body) == url.join_path([path])
 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'