1*2066Szelenkov@nginx.comfrom packaging import version
21865So.canty@f5.comfrom unit.applications.lang.node import TestApplicationNode
31865So.canty@f5.comfrom unit.applications.websockets import TestApplicationWebsocket
41865So.canty@f5.com
51865So.canty@f5.com
61865So.canty@f5.comclass TestNodeESModules(TestApplicationNode):
71865So.canty@f5.com    prerequisites = {
81865So.canty@f5.com        'modules': {
9*2066Szelenkov@nginx.com            'node': lambda v: version.parse(v) >= version.parse('14.16.0')
101865So.canty@f5.com        }
111865So.canty@f5.com    }
121865So.canty@f5.com
131865So.canty@f5.com    es_modules = True
141865So.canty@f5.com    ws = TestApplicationWebsocket()
151865So.canty@f5.com
161865So.canty@f5.com    def assert_basic_application(self):
171865So.canty@f5.com        resp = self.get()
181865So.canty@f5.com        assert resp['headers']['Content-Type'] == 'text/plain', 'basic header'
191865So.canty@f5.com        assert resp['body'] == 'Hello World\n', 'basic body'
201865So.canty@f5.com
211876So.canty@f5.com    def test_node_es_modules_loader_http(self):
221876So.canty@f5.com        self.load('loader/es_modules_http', name="app.mjs")
231865So.canty@f5.com
241865So.canty@f5.com        self.assert_basic_application()
251865So.canty@f5.com
261876So.canty@f5.com    def test_node_es_modules_loader_http_indirect(self):
271876So.canty@f5.com        self.load('loader/es_modules_http_indirect', name="app.js")
281865So.canty@f5.com
291865So.canty@f5.com        self.assert_basic_application()
301865So.canty@f5.com
311876So.canty@f5.com    def test_node_es_modules_loader_websockets(self):
321876So.canty@f5.com        self.load('loader/es_modules_websocket', name="app.mjs")
331865So.canty@f5.com
341865So.canty@f5.com        message = 'blah'
351865So.canty@f5.com
361865So.canty@f5.com        _, sock, _ = self.ws.upgrade()
371865So.canty@f5.com
381865So.canty@f5.com        self.ws.frame_write(sock, self.ws.OP_TEXT, message)
391865So.canty@f5.com        frame = self.ws.frame_read(sock)
401865So.canty@f5.com
411865So.canty@f5.com        assert message == frame['data'].decode('utf-8'), 'mirror'
421865So.canty@f5.com
431865So.canty@f5.com        self.ws.frame_write(sock, self.ws.OP_TEXT, message)
441865So.canty@f5.com        frame = self.ws.frame_read(sock)
451865So.canty@f5.com
461865So.canty@f5.com        assert message == frame['data'].decode('utf-8'), 'mirror 2'
471865So.canty@f5.com
481865So.canty@f5.com        sock.close()
49