def _update_environ(self, environ, user): if user: environ = environ or {} if isinstance(user, User): environ['WEBTEST_USER'] = to_string(user.username) else: environ['WEBTEST_USER'] = to_string(user) return environ
def do_request(self, req, status, expect_errors): # Django closes the database connection after every request; # this breaks the use of transactions in your tests. signals.request_finished.disconnect(close_connection) try: req.environ.setdefault('REMOTE_ADDR', '127.0.0.1') # is this a workaround for https://code.djangoproject.com/ticket/11111 ? req.environ['REMOTE_ADDR'] = to_string(req.environ['REMOTE_ADDR']) req.environ['PATH_INFO'] = to_string(req.environ['PATH_INFO']) # Curry a data dictionary into an instance of the template renderer # callback function. data = {} on_template_render = curry(store_rendered_templates, data) template_rendered.connect(on_template_render) response = super(DjangoTestApp, self).do_request(req, status, expect_errors) # Add any rendered template detail to the response. # If there was only one template rendered (the most likely case), # flatten the list to a single element. def flattend(detail): if len(data[detail]) == 1: return data[detail][0] return data[detail] response.context = None response.template = None response.templates = data.get('templates', None) if data.get('context'): response.context = flattend('context') if data.get('template'): response.template = flattend('template') elif data.get('templates'): response.template = flattend('templates') response.__class__ = DjangoWebtestResponse return response finally: signals.request_finished.connect(close_connection)
def test_to_string(self): self.assertEqual(to_string('foo'), 'foo') self.assertEqual(to_string(b'foo'), 'foo')