1*1865So.canty@f5.comimport pytest
2*1865So.canty@f5.com
3*1865So.canty@f5.comfrom distutils.version import LooseVersion
4*1865So.canty@f5.comfrom unit.applications.lang.node import TestApplicationNode
5*1865So.canty@f5.comfrom unit.applications.websockets import TestApplicationWebsocket
6*1865So.canty@f5.com
7*1865So.canty@f5.com
8*1865So.canty@f5.comclass TestNodeESModules(TestApplicationNode):
9*1865So.canty@f5.com    prerequisites = {
10*1865So.canty@f5.com        'modules': {
11*1865So.canty@f5.com            'node': lambda v:  LooseVersion(v) >= LooseVersion("14.16.0")
12*1865So.canty@f5.com        }
13*1865So.canty@f5.com    }
14*1865So.canty@f5.com
15*1865So.canty@f5.com    es_modules = True
16*1865So.canty@f5.com    ws = TestApplicationWebsocket()
17*1865So.canty@f5.com
18*1865So.canty@f5.com    def assert_basic_application(self):
19*1865So.canty@f5.com        resp = self.get()
20*1865So.canty@f5.com        assert resp['headers']['Content-Type'] == 'text/plain', 'basic header'
21*1865So.canty@f5.com        assert resp['body'] == 'Hello World\n', 'basic body'
22*1865So.canty@f5.com
23*1865So.canty@f5.com    def test_node_es_modules_require_shim_http(self):
24*1865So.canty@f5.com        self.load('require_shim/es_modules_http', name="app.mjs")
25*1865So.canty@f5.com
26*1865So.canty@f5.com        self.assert_basic_application()
27*1865So.canty@f5.com
28*1865So.canty@f5.com    def test_node_es_modules_require_shim_http_indirect(self):
29*1865So.canty@f5.com        self.load('require_shim/es_modules_http_indirect', name="app.js")
30*1865So.canty@f5.com
31*1865So.canty@f5.com        self.assert_basic_application()
32*1865So.canty@f5.com
33*1865So.canty@f5.com    def test_node_es_modules_require_shim_websockets(self):
34*1865So.canty@f5.com        self.load('require_shim/es_modules_websocket', name="app.mjs")
35*1865So.canty@f5.com
36*1865So.canty@f5.com        message = 'blah'
37*1865So.canty@f5.com
38*1865So.canty@f5.com        _, sock, _ = self.ws.upgrade()
39*1865So.canty@f5.com
40*1865So.canty@f5.com        self.ws.frame_write(sock, self.ws.OP_TEXT, message)
41*1865So.canty@f5.com        frame = self.ws.frame_read(sock)
42*1865So.canty@f5.com
43*1865So.canty@f5.com        assert message == frame['data'].decode('utf-8'), 'mirror'
44*1865So.canty@f5.com
45*1865So.canty@f5.com        self.ws.frame_write(sock, self.ws.OP_TEXT, message)
46*1865So.canty@f5.com        frame = self.ws.frame_read(sock)
47*1865So.canty@f5.com
48*1865So.canty@f5.com        assert message == frame['data'].decode('utf-8'), 'mirror 2'
49*1865So.canty@f5.com
50*1865So.canty@f5.com        sock.close()
51