Skip to content

Commit b642497

Browse files
Tanmay DasPrabhakar Kumar
Tanmay Das
authored and
Prabhakar Kumar
committed
Unit Testing websocket connections in matlab_view
1 parent 75ed39f commit b642497

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

.github/actions/generate-code-coverage/action.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ runs:
1010
with:
1111
python-version: '3.8'
1212

13-
- name: Use Node.js 13.x
13+
- name: Use Node.js 18.x
1414
uses: actions/setup-node@v3
1515
with:
16-
node-version: 13.x
16+
node-version: 18
1717

1818
- name: Install Python build dependencies
1919
run: |

matlab_proxy/app.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,8 @@ async def termination_integration_delete(req):
304304
# End termination with 0 exit code to indicate intentional termination
305305
await req.app.shutdown()
306306
await req.app.cleanup()
307-
"""When testing with pytest, its not possible to catch sys.exit(0) using the construct
308-
'with pytest.raises()', there by causing the test : test_termination_integration_delete()
307+
"""When testing with pytest, its not possible to catch sys.exit(0) using the construct
308+
'with pytest.raises()', there by causing the test : test_termination_integration_delete()
309309
to fail. Inorder to avoid this, adding the below if condition to check to skip sys.exit(0) when testing
310310
"""
311311
logger.debug("Exiting with return code 0")
@@ -408,6 +408,10 @@ async def matlab_view(req):
408408
Returns:
409409
WebSocketResponse or HTTPResponse: based on the Request type.
410410
"""
411+
# Special keys for web socket requests
412+
CONNECTION = "connection"
413+
UPGRADE = "upgrade"
414+
411415
reqH = req.headers.copy()
412416

413417
state = req.app["state"]
@@ -417,11 +421,12 @@ async def matlab_view(req):
417421
matlab_base_url = f"{matlab_protocol}://127.0.0.1:{matlab_port}"
418422

419423
# WebSocket
424+
# According to according to RFC6455 (https://www.rfc-editor.org/rfc/rfc6455.html)
425+
# the values of 'connection' and 'upgrade' keys of request header
426+
# should be ASCII case-insensitive matches.
420427
if (
421-
reqH.get("connection")
422-
and reqH.get("connection").lower() == "upgrade"
423-
and reqH.get("upgrade")
424-
and reqH.get("upgrade").lower() == "websocket"
428+
reqH.get(CONNECTION, "").lower() == UPGRADE
429+
and reqH.get(UPGRADE, "").lower() == "websocket"
425430
and req.method == "GET"
426431
):
427432
ws_server = web.WebSocketResponse()

tests/test_app.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -445,18 +445,37 @@ async def test_matlab_proxy_http_post_request(proxy_payload, test_server):
445445
raise ConnectionError
446446

447447

448-
async def test_matlab_proxy_web_socket(test_server):
448+
# While acceessing matlab-proxy directly, the web socket request looks like
449+
# {
450+
# "connection": "Upgrade",
451+
# "Upgrade": "websocket",
452+
# }
453+
# whereas while accessing matlab-proxy with nginx as the reverse proxy, the nginx server
454+
# modifies the web socket request to
455+
# {
456+
# "connection": "upgrade",
457+
# "upgrade": "websocket",
458+
# }
459+
@pytest.mark.parametrize(
460+
"headers",
461+
[
462+
{
463+
"connection": "Upgrade",
464+
"Upgrade": "websocket",
465+
},
466+
{
467+
"connection": "upgrade",
468+
"upgrade": "websocket",
469+
},
470+
],
471+
)
472+
async def test_matlab_proxy_web_socket(test_server, headers):
449473
"""Test to check if test_server proxies web socket request to fake matlab server
450474
451475
Args:
452476
test_server (aiohttp_client): Test Server to send HTTP Requests.
453477
"""
454478

455-
headers = {
456-
"connection": "Upgrade",
457-
"upgrade": "websocket",
458-
}
459-
460479
resp = await test_server.ws_connect("/http_ws_request.html", headers=headers)
461480
text = await resp.receive()
462481
assert text.type == aiohttp.WSMsgType.CLOSED

0 commit comments

Comments
 (0)