Ejemplo n.º 1
0
    def mock_request(self,
                     username='******',
                     path_info='/',
                     request_body='',
                     **kwargs):
        response = ValueObject(headers=dict(), body='', code=None)
        as_json = lambda self: json.loads(self.body)
        response.body_as_json = new.instancemethod(as_json, response,
                                                   response.__class__)

        perm = PermissionCache(self.env, username)
        attributes = dict(
            args=dict(),
            tz=localtz,
            perm=perm,
            method='GET',
            path_info=path_info,
            environ={},
            session={},
            form_token=None,
        )
        attributes.update(kwargs)

        def read():
            return request_body

        def write(string):
            response['body'] += string

        def redirect(url, permanent=False):
            raise RequestDone

        def get_header(header_name):
            header_name = header_name.lower()
            if header_name == 'content-length':
                return str(len(request_body))
            return None

        req = Mock(authname=username,
                   base_path=None,
                   href=Href(path_info),
                   chrome=dict(warnings=[], notices=[], scripts=[]),
                   incookie=Cookie(),
                   outcookie=Cookie(),
                   response=response,
                   end_headers=lambda: None,
                   get_header=get_header,
                   read=read,
                   redirect=redirect,
                   send_response=lambda code: response.update({'code': code}),
                   send_header=lambda name, value: response['headers'].update(
                       {name: value}),
                   write=write,
                   **attributes)

        # our jquery injects wines if it does not find trac's jquery
        req.chrome['scripts'].append(dict(href='/foo/jquery.js'))
        return req
Ejemplo n.º 2
0
    def mock_request(self, username='******', path_info='/', request_body='', **kwargs):
        response = ValueObject(headers=dict(), body='', code=None)
        as_json = lambda self: json.loads(self.body)
        response.body_as_json = new.instancemethod(as_json, response, response.__class__)

        perm = PermissionCache(self.env, username)
        attributes = dict(args=dict(), tz=localtz, perm=perm, method='GET',
                          path_info=path_info, environ={}, session={},
                          form_token=None, )
        attributes.update(kwargs)

        def read():
            return request_body

        def write(string):
            response['body'] += string

        def redirect(url, permanent=False):
            raise RequestDone

        def get_header(header_name):
            header_name = header_name.lower()
            if header_name == 'content-length':
                return str(len(request_body))
            return None

        req = Mock(authname=username, base_path=None, href=Href(path_info),
                   chrome=dict(warnings=[], notices=[], scripts=[]),
                   incookie=Cookie(), outcookie=Cookie(),
                   response=response,

                   end_headers=lambda: None,
                   get_header=get_header,
                   read=read,
                   redirect=redirect,
                   send_response=lambda code: response.update({'code': code}),
                   send_header=lambda name, value: response['headers'].update({name: value}),
                   write=write,

                   **attributes)

        # our jquery injects wines if it does not find trac's jquery
        req.chrome['scripts'].append(dict(href='/foo/jquery.js'))
        return req