xref: /unit/test/test_node_es_modules.py (revision 2066:242192963d93)
1from packaging import version
2from unit.applications.lang.node import TestApplicationNode
3from unit.applications.websockets import TestApplicationWebsocket
4
5
6class TestNodeESModules(TestApplicationNode):
7    prerequisites = {
8        'modules': {
9            'node': lambda v: version.parse(v) >= version.parse('14.16.0')
10        }
11    }
12
13    es_modules = True
14    ws = TestApplicationWebsocket()
15
16    def assert_basic_application(self):
17        resp = self.get()
18        assert resp['headers']['Content-Type'] == 'text/plain', 'basic header'
19        assert resp['body'] == 'Hello World\n', 'basic body'
20
21    def test_node_es_modules_loader_http(self):
22        self.load('loader/es_modules_http', name="app.mjs")
23
24        self.assert_basic_application()
25
26    def test_node_es_modules_loader_http_indirect(self):
27        self.load('loader/es_modules_http_indirect', name="app.js")
28
29        self.assert_basic_application()
30
31    def test_node_es_modules_loader_websockets(self):
32        self.load('loader/es_modules_websocket', name="app.mjs")
33
34        message = 'blah'
35
36        _, sock, _ = self.ws.upgrade()
37
38        self.ws.frame_write(sock, self.ws.OP_TEXT, message)
39        frame = self.ws.frame_read(sock)
40
41        assert message == frame['data'].decode('utf-8'), 'mirror'
42
43        self.ws.frame_write(sock, self.ws.OP_TEXT, message)
44        frame = self.ws.frame_read(sock)
45
46        assert message == frame['data'].decode('utf-8'), 'mirror 2'
47
48        sock.close()
49