django send request to self
有两种方法可以从"正在工作"的Django服务器获得响应。工作版本是硬编码的,在单元测试时不工作
1 2 3 4 5 6 7 8 | # working # a = requests.post('http://localhost:8000/ImportKeys/', # data=json.dumps({'user_id': key_obj.email, #'key': self.restore_pubkey(key_obj.fingerprint)})) # not working a = requests.post('http://' + request.get_host() + reverse('import_keys'),data=json.dumps({'user_id': key_obj.email,'key': self.restore_pubkey(key_obj.fingerprint)})) |
在那个版本上,我不想开始工作,我得到了这个(结束stacktrace):
File"/home/PycharmProjects/lib/python3.4/site-packages/requests/sessions.py", line 576, in send
r = adapter.send(request, **kwargs)
File"/home/PycharmProjects/lib/python3.4/site-packages/requests/adapters.py", line 437, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='testserver', port=80): Max retries exceeded with url: /ImportKeys/ (Caused by NewConnectionError(': Failed to establish a new connection: [Errno -2] Name or service not known',))
是的,我看到它正在尝试连接80个端口,这很糟糕。
要在
1 2 3 4 5 6 7 8 9 | class YourTestCase(TestCase): def test_import_keys_posting(self): data = { 'user_id': key_obj.email, 'key': self.restore_pubkey(key_obj.fingerprint) } response = self.client.post(reverse('import_keys'), data) self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), {'result': 'ok'}) |
如果您使用django-rest框架,可以考虑使用其出色的
如果您需要在测试期间向服务器发送请求(在这种情况下,这可能不是来自测试代码本身,而是来自某个模拟或JS代码):
扩展
如果在正在测试的常规代码中使用
1 2 3 4 5 6 | checkout_url = '{}{}'.format(self.live_server_url, reverse('checkout', kwargs={'pk': article.id})) parsed_url = parse.urlparse(self.live_server_url) # add the info on host and port to the http header to make subsequent # request.build_absolute_uri() calls work response = self.client.get(checkout_url, SERVER_NAME=parsed_url.hostname, SERVER_PORT=parsed_url.port) |