@@ -90,160 +90,158 @@ def wait_for_server(url: str, timeout: int = 30) -> None:
90
90
time .sleep (2 )
91
91
92
92
93
- async def test_connection (aspnet_server : str ) -> None :
94
- """
95
- Tests connection to the SignalR server.
96
- """
97
- url = f'http://{ aspnet_server } /weatherHub'
98
- logging .info ('Testing connection to %s' , url )
99
- client = SignalRClient (url )
100
-
101
- task = asyncio .create_task (client .run ())
102
-
103
- async def _on_open () -> None :
104
- logging .info ('Connection opened, cancelling task' )
105
- task .cancel ()
106
-
107
- client .on_open (_on_open )
108
-
109
- with suppress (asyncio .CancelledError ):
110
- await task
111
-
112
-
113
- async def test_connection_with_token (aspnet_server : str ) -> None :
114
- """
115
- Tests connection to the SignalR server with a valid token.
116
- """
117
- login_url = f'http://{ aspnet_server } /api/auth/login'
118
- logging .info ('Attempting to log in at %s' , login_url )
119
- login_data = {'username' : 'test' , 'password' : 'password' }
120
- response = requests .post (login_url , json = login_data , timeout = 10 )
121
- token = response .json ().get ('token' )
122
- if not token :
123
- pytest .fail ('Failed to obtain token from login response' )
124
-
125
- url = f'http://{ aspnet_server } /weatherHub'
126
- logging .info ('Testing connection with token to %s' , url )
127
-
128
- def token_factory () -> str :
129
- return cast (str , token )
130
-
131
- client = SignalRClient (
132
- url = url ,
133
- access_token_factory = token_factory ,
134
- headers = {'mycustomheader' : 'mycustomheadervalue' },
135
- )
136
-
137
- task = asyncio .create_task (client .run ())
138
-
139
- async def _on_open () -> None :
140
- logging .info ('Connection with token opened, cancelling task' )
141
- task .cancel ()
142
-
143
- client .on_open (_on_open )
144
-
145
- with suppress (asyncio .CancelledError ):
146
- await task
147
-
148
- # Verify the token in the connection headers
149
- assert 'Authorization' in client ._transport ._headers
150
- assert client ._transport ._headers ['Authorization' ] == f'Bearer { token } '
151
-
152
-
153
- async def test_invalid_token (aspnet_server : str ) -> None :
154
- """
155
- Tests connection to the SignalR server with an invalid token.
156
- """
157
- url = f'http://{ aspnet_server } /weatherHub'
158
- logging .info ('Testing connection with invalid token to %s' , url )
93
+ class TestPysignalr :
94
+ async def test_connection (self , aspnet_server : str ) -> None :
95
+ """
96
+ Tests connection to the SignalR server.
97
+ """
98
+ url = f'http://{ aspnet_server } /weatherHub'
99
+ logging .info ('Testing connection to %s' , url )
100
+ client = SignalRClient (url )
101
+
102
+ task = asyncio .create_task (client .run ())
103
+
104
+ async def _on_open () -> None :
105
+ logging .info ('Connection opened, cancelling task' )
106
+ task .cancel ()
159
107
160
- def invalid_token_factory () -> str :
161
- return 'invalid_token' # Simulate an invalid token
108
+ client .on_open (_on_open )
162
109
163
- client = SignalRClient (
164
- url = url ,
165
- access_token_factory = invalid_token_factory ,
166
- headers = {'mycustomheader' : 'mycustomheadervalue' },
167
- )
168
-
169
- task = asyncio .create_task (client .run ())
110
+ with suppress (asyncio .CancelledError ):
111
+ await task
170
112
171
- async def _on_open () -> None :
172
- logging .info ('Connection with invalid token opened, cancelling task' )
173
- task .cancel ()
113
+ async def test_connection_with_token (self , aspnet_server : str ) -> None :
114
+ """
115
+ Tests connection to the SignalR server with a valid token.
116
+ """
117
+ login_url = f'http://{ aspnet_server } /api/auth/login'
118
+ logging .info ('Attempting to log in at %s' , login_url )
119
+ login_data = {'username' : 'test' , 'password' : 'password' }
120
+ response = requests .post (login_url , json = login_data , timeout = 10 )
121
+ token = response .json ().get ('token' )
122
+ if not token :
123
+ pytest .fail ('Failed to obtain token from login response' )
124
+
125
+ url = f'http://{ aspnet_server } /weatherHub'
126
+ logging .info ('Testing connection with token to %s' , url )
127
+
128
+ def token_factory () -> str :
129
+ return cast (str , token )
130
+
131
+ client = SignalRClient (
132
+ url = url ,
133
+ access_token_factory = token_factory ,
134
+ headers = {'mycustomheader' : 'mycustomheadervalue' },
135
+ )
136
+
137
+ task = asyncio .create_task (client .run ())
138
+
139
+ async def _on_open () -> None :
140
+ logging .info ('Connection with token opened, cancelling task' )
141
+ task .cancel ()
174
142
175
- client .on_open (_on_open )
143
+ client .on_open (_on_open )
176
144
177
- with suppress (asyncio .CancelledError ):
178
- try :
145
+ with suppress (asyncio .CancelledError ):
179
146
await task
180
- except AuthorizationError :
181
- logging .info ('AuthorizationError caught as expected' )
182
- pass
183
147
184
- # Verify if the AuthorizationError was raised correctly
185
- assert task .cancelled () is True
148
+ # Verify the token in the connection headers
149
+ assert 'Authorization' in client ._transport ._headers
150
+ assert client ._transport ._headers ['Authorization' ] == f'Bearer { token } '
186
151
152
+ async def test_invalid_token (self , aspnet_server : str ) -> None :
153
+ """
154
+ Tests connection to the SignalR server with an invalid token.
155
+ """
156
+ url = f'http://{ aspnet_server } /weatherHub'
157
+ logging .info ('Testing connection with invalid token to %s' , url )
187
158
188
- async def test_send_and_receive_message (aspnet_server : str ) -> None :
189
- """
190
- Tests sending and receiving a message with the SignalR server.
191
- """
192
- login_url = f'http://{ aspnet_server } /api/auth/login'
193
- logging .info ('Attempting to log in at %s' , login_url )
194
- login_data = {'username' : 'test' , 'password' : 'password' }
195
- response = requests .post (login_url , json = login_data , timeout = 10 )
196
- token = response .json ().get ('token' )
197
- if not token :
198
- logging .error ('Failed to obtain token from login response' )
199
- raise AssertionError ('Failed to obtain token from login response' )
200
- logging .info ('Obtained token: %s' , token )
201
-
202
- url = f'http://{ aspnet_server } /weatherHub'
203
- logging .info ('Testing send and receive message with token to %s' , url )
204
-
205
- def token_factory () -> str :
206
- return cast (str , token )
207
-
208
- client = SignalRClient (
209
- url = url ,
210
- access_token_factory = token_factory ,
211
- headers = {'mycustomheader' : 'mycustomheadervalue' },
212
- )
213
-
214
- received_messages = []
215
-
216
- async def on_message_received (arguments : Any ) -> None :
217
- user , message = arguments
218
- logging .info ('Message received from %s: %s' , user , message )
219
- received_messages .append ((user , message ))
220
- if len (received_messages ) >= 1 :
221
- task .cancel ()
159
+ def invalid_token_factory () -> str :
160
+ return 'invalid_token' # Simulate an invalid token
222
161
223
- client .on ('ReceiveMessage' , on_message_received )
162
+ client = SignalRClient (
163
+ url = url ,
164
+ access_token_factory = invalid_token_factory ,
165
+ headers = {'mycustomheader' : 'mycustomheadervalue' },
166
+ )
224
167
225
- task = asyncio .create_task (client .run ())
168
+ task = asyncio .create_task (client .run ())
226
169
227
- async def _on_open () -> None :
228
- logging .info ('Connection with token opened, sending message ' )
229
- await client . send ( 'SendMessage' , [ 'testuser' , 'Hello, World!' ]) # type: ignore[list-item]
170
+ async def _on_open () -> None :
171
+ logging .info ('Connection with invalid token opened, cancelling task ' )
172
+ task . cancel ()
230
173
231
- client .on_open (_on_open )
174
+ client .on_open (_on_open )
232
175
233
- try :
234
176
with suppress (asyncio .CancelledError ):
235
- await task
236
- except ServerError as e :
237
- logging .error ('Server error: %s' , e )
238
- raise
239
-
240
- # Verify if the message was received correctly
241
- assert received_messages , 'No messages were received'
242
- assert received_messages [0 ] == (
243
- 'testuser' ,
244
- 'Hello, World!' ,
245
- ), f'Unexpected message received: { received_messages [0 ]} '
246
-
247
- # Log detailed messages received
248
- for user , message in received_messages :
249
- logging .info ('Detailed Log: Message from %s - %s' , user , message )
177
+ try :
178
+ await task
179
+ except AuthorizationError :
180
+ logging .info ('AuthorizationError caught as expected' )
181
+ pass
182
+
183
+ # Verify if the AuthorizationError was raised correctly
184
+ assert task .cancelled () is True
185
+
186
+ async def test_send_and_receive_message (self , aspnet_server : str ) -> None :
187
+ """
188
+ Tests sending and receiving a message with the SignalR server.
189
+ """
190
+ login_url = f'http://{ aspnet_server } /api/auth/login'
191
+ logging .info ('Attempting to log in at %s' , login_url )
192
+ login_data = {'username' : 'test' , 'password' : 'password' }
193
+ response = requests .post (login_url , json = login_data , timeout = 10 )
194
+ token = response .json ().get ('token' )
195
+ if not token :
196
+ logging .error ('Failed to obtain token from login response' )
197
+ raise AssertionError ('Failed to obtain token from login response' )
198
+ logging .info ('Obtained token: %s' , token )
199
+
200
+ url = f'http://{ aspnet_server } /weatherHub'
201
+ logging .info ('Testing send and receive message with token to %s' , url )
202
+
203
+ def token_factory () -> str :
204
+ return cast (str , token )
205
+
206
+ client = SignalRClient (
207
+ url = url ,
208
+ access_token_factory = token_factory ,
209
+ headers = {'mycustomheader' : 'mycustomheadervalue' },
210
+ )
211
+
212
+ received_messages = []
213
+
214
+ async def on_message_received (arguments : Any ) -> None :
215
+ user , message = arguments
216
+ logging .info ('Message received from %s: %s' , user , message )
217
+ received_messages .append ((user , message ))
218
+ if len (received_messages ) >= 1 :
219
+ task .cancel ()
220
+
221
+ client .on ('ReceiveMessage' , on_message_received )
222
+
223
+ task = asyncio .create_task (client .run ())
224
+
225
+ async def _on_open () -> None :
226
+ logging .info ('Connection with token opened, sending message' )
227
+ await client .send ('SendMessage' , ['testuser' , 'Hello, World!' ]) # type: ignore[list-item]
228
+
229
+ client .on_open (_on_open )
230
+
231
+ try :
232
+ with suppress (asyncio .CancelledError ):
233
+ await task
234
+ except ServerError as e :
235
+ logging .error ('Server error: %s' , e )
236
+ raise
237
+
238
+ # Verify if the message was received correctly
239
+ assert received_messages , 'No messages were received'
240
+ assert received_messages [0 ] == (
241
+ 'testuser' ,
242
+ 'Hello, World!' ,
243
+ ), f'Unexpected message received: { received_messages [0 ]} '
244
+
245
+ # Log detailed messages received
246
+ for user , message in received_messages :
247
+ logging .info ('Detailed Log: Message from %s - %s' , user , message )
0 commit comments