1010import requests
1111import re
1212from requests .adapters import HTTPAdapter , Retry
13- from urllib .parse import urlparse
13+ from urllib .parse import urlparse , parse_qs
1414from logging_util import create_test_logger
1515
1616_logger = create_test_logger (
@@ -66,6 +66,14 @@ def __enter__(self):
6666
6767 utils .wait_server_info_ready (self .mwi_app_port )
6868 parsed_url = urlparse (utils .get_connection_string (self .mwi_app_port ))
69+
70+ self .headers = {
71+ "mwi_auth_token" : (
72+ parse_qs (parsed_url .query )["mwi_auth_token" ][0 ]
73+ if "mwi_auth_token" in parse_qs (parsed_url .query )
74+ else ""
75+ )
76+ }
6977 self .connection_scheme = parsed_url .scheme
7078 self .url = parsed_url .scheme + "://" + parsed_url .netloc + parsed_url .path
7179 return self
@@ -89,7 +97,7 @@ def _move(source, destination):
8997 _logger .exception (err )
9098
9199
92- def _send_http_get_request (uri , connection_scheme , http_endpoint = "" ):
100+ def _send_http_get_request (uri , connection_scheme , headers , http_endpoint = "" ):
93101 """Send HTTP request to matlab-proxy server.
94102 Returns HTTP response JSON"""
95103
@@ -99,20 +107,24 @@ def _send_http_get_request(uri, connection_scheme, http_endpoint=""):
99107 with requests .Session () as s :
100108 retries = Retry (total = 10 , backoff_factor = 0.1 )
101109 s .mount (f"{ connection_scheme } ://" , HTTPAdapter (max_retries = retries ))
102- response = s .get (request_uri , verify = False )
110+ response = s .get (request_uri , headers = headers , verify = False )
103111 json_response = json .loads (response .text )
104112
105113 return json_response
106114
107115
108- def _check_matlab_status (connection_scheme , status , uri ):
116+ def _check_matlab_status (matlab_proxy_app_fixture , status ):
117+ uri = matlab_proxy_app_fixture .url
118+ connection_scheme = matlab_proxy_app_fixture .connection_scheme
119+ headers = matlab_proxy_app_fixture .headers
120+
109121 matlab_status = None
110122
111123 start_time = time .time ()
112124 while matlab_status != status and (time .time () - start_time < MAX_TIMEOUT ):
113125 time .sleep (1 )
114126 res = _send_http_get_request (
115- uri , connection_scheme , http_endpoint = "/get_status"
127+ uri , connection_scheme , headers , http_endpoint = "/get_status"
116128 )
117129 matlab_status = res ["matlab" ]["status" ]
118130
@@ -146,11 +158,7 @@ def test_matlab_is_up(matlab_proxy_app_fixture):
146158 matlab_proxy_app_fixture: A pytest fixture which yields a real matlab server to be used by tests.
147159 """
148160
149- status = _check_matlab_status (
150- matlab_proxy_app_fixture .connection_scheme ,
151- "up" ,
152- matlab_proxy_app_fixture .url ,
153- )
161+ status = _check_matlab_status (matlab_proxy_app_fixture , "up" )
154162 assert status == "up"
155163
156164
@@ -162,12 +170,7 @@ def test_stop_matlab(matlab_proxy_app_fixture):
162170 matlab_proxy_app_fixture: A pytest fixture which yields a real matlab server to be used by tests.
163171 """
164172
165- _logger .info ("Testing stopping" )
166- status = _check_matlab_status (
167- matlab_proxy_app_fixture .connection_scheme ,
168- "up" ,
169- matlab_proxy_app_fixture .url ,
170- )
173+ status = _check_matlab_status (matlab_proxy_app_fixture , "up" )
171174 assert status == "up"
172175
173176 http_endpoint_to_test = "/stop_matlab"
@@ -179,15 +182,10 @@ def test_stop_matlab(matlab_proxy_app_fixture):
179182 f"{ matlab_proxy_app_fixture .connection_scheme } ://" ,
180183 HTTPAdapter (max_retries = retries ),
181184 )
182- s .delete (stop_url , verify = False )
185+ s .delete (stop_url , headers = matlab_proxy_app_fixture . headers , verify = False )
183186
184- status = _check_matlab_status (
185- matlab_proxy_app_fixture .connection_scheme ,
186- "down" ,
187- matlab_proxy_app_fixture .url ,
188- )
187+ status = _check_matlab_status (matlab_proxy_app_fixture , "down" )
189188 assert status == "down"
190- _logger .info ("stop_matlab test passed" )
191189
192190
193191async def test_print_message (matlab_proxy_app_fixture ):
@@ -198,9 +196,7 @@ async def test_print_message(matlab_proxy_app_fixture):
198196
199197 """
200198 # Checks if matlab proxy is in "up" state or not
201- status = _check_matlab_status (
202- matlab_proxy_app_fixture .connection_scheme , "up" , matlab_proxy_app_fixture .url
203- )
199+ status = _check_matlab_status (matlab_proxy_app_fixture , "up" )
204200 assert status == "up"
205201
206202 uri_regex = f"{ matlab_proxy_app_fixture .connection_scheme } ://[a-zA-Z0-9\-_.]+:{ matlab_proxy_app_fixture .mwi_app_port } { matlab_proxy_app_fixture .mwi_base_url } "
0 commit comments