简体   繁体   English

使用 HttpCommunicator 进行 Django 通道测试

[英]Django channels testing with HttpCommunicator

I have a DRF application, which also has a single websocket consumer我有一个DRF应用程序,它也有一个 websocket 消费者

Now I'm trying to make a test case, which inherits from a Djano 3.1 TestCase.现在我正在尝试制作一个测试用例,它继承自 Djano 3.1 测试用例。 Said test case should register a user via rest_auth registration endpoint and than connect to the channels consumer所述测试用例应通过rest_auth注册端点注册用户,而不是连接到通道消费者

To register a user in a test case I use HttpCommunicator like so:要在测试用例中注册用户,我会像这样使用HttpCommunicator

class TestRoomWebsockets(APITestCase):
    async def test_connect(self):
        communicator = HttpCommunicator(
            application, "POST", reverse(UrlUtils.Users.REGISTER), 
            body=json.dumps({"username": ..., "password1": ..., "password2": ...}).encode("utf-8")
        )
        response = await communicator.get_response()
        self.assertEqual(response["status"], 200)

But it fails with status code 400 .但它失败了,状态码为400 The response is回应是

{'status': 400, 'headers': [(b'Content-Type', b'application/json'), (b'Vary', b'Accept'), (b'Allow', b'POST, OPTIONS'), (b'X-Frame-Options', b'DENY'), (b'Content-Length', b'120'), (b'X-Content-Type-Options', b'nosniff'), (b'Referrer-Policy', b'same-origin')], 'body': b'{"username":["This field is required."],"password1":["This field is required."],"password2":["This field is required."]}'}

I have no idea, why the data is lost somewhere.我不知道,为什么数据在某处丢失了。 Could someone please explain, what am I doing wrong?有人可以解释一下,我做错了什么吗? Please tell, if more details are required to solve the issue.请告知,是否需要更多详细信息来解决问题。

Some additional files一些额外的文件

asgi application looks like this: asgi 应用程序如下所示:

application = ProtocolTypeRouter({
    "http": django_asgi_app,
    "websocket": TokenAuthMiddlewareStack(
        URLRouter([
            path('ws/', my_router),
        ])
    )
})

If you need to use database transactions with channels in test you have to use SimpleTestCase.如果您需要在测试中使用带有通道的数据库事务,则必须使用 SimpleTestCase。 APITestCase or TestCase dont allow transactions, you have to use SimpleTestCase from django test, and set databases to all. APITestCase 或 TestCase 不允许事务,您必须使用来自 django 测试的 SimpleTestCase,并将数据库设置为全部。 Just with that difference i think it will work.就这点不同,我认为它会起作用。 Note that the transactions will be saved between test, and not rolled back after the test.请注意,事务将在测试之间保存,并且不会在测试后回滚。

from django.test import SimpleTestCase
class TestRoomWebsockets(APITestCase):
    databases = '__all__'
    async def test_connect(self):
        communicator = HttpCommunicator(
        application, "POST", reverse(UrlUtils.Users.REGISTER), 
        body=json.dumps({"username": ..., "password1": ..., "password2": ...}).encode("utf-8")
    )
        response = await communicator.get_response()
        self.assertEqual(response["status"], 200)

here are the information of SimpleTestCase https://docs.djangoproject.com/en/4.0/topics/testing/tools/这是SimpleTestCase的信息https://docs.djangoproject.com/en/4.0/topics/testing/tools/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM