@@ -165,3 +165,145 @@ def test_connect_audio_to_websocket_missing_uri_error(self):
165165 with self .assertRaises (InvalidWebSocketOptionsError ) as context :
166166 self .opentok .connect_audio_to_websocket (self .session_id , self .token , websocket_options )
167167 self .assertTrue ("Provide a WebSocket URI." in str (context .exception ))
168+
169+ @httpretty .activate
170+ def test_connect_audio_to_websocket_with_audio_transport_json (self ):
171+ httpretty .register_uri (
172+ httpretty .POST ,
173+ u (f"https://api.opentok.com/v2/project/{ self .api_key } /connect" ),
174+ body = self .response_body ,
175+ status = 200 ,
176+ content_type = u ("application/json" ),
177+ )
178+
179+ websocket_options = {
180+ "uri" : "wss://service.com/ws-endpoint" ,
181+ "audio_transport" : {
182+ "transport" : "json" ,
183+ "encoding" : "base64" ,
184+ },
185+ }
186+
187+ websocket_audio_connection = self .opentok .connect_audio_to_websocket (
188+ self .session_id , self .token , websocket_options
189+ )
190+
191+ if PY3 :
192+ body = json .loads (httpretty .last_request ().body .decode ("utf-8" ))
193+
194+ expect (body ["websocket" ]).to (have_key ("audioTransport" ))
195+ expect (body ["websocket" ]).not_to (have_key ("audio_transport" ))
196+ parsed = json .loads (body ["websocket" ]["audioTransport" ])
197+ expect (parsed ["transport" ]).to (equal ("json" ))
198+ expect (parsed ["encoding" ]).to (equal ("base64" ))
199+ expect (websocket_audio_connection ).to (be_a (WebSocketAudioConnection ))
200+
201+ @httpretty .activate
202+ def test_connect_audio_to_websocket_with_audio_transport_full (self ):
203+ httpretty .register_uri (
204+ httpretty .POST ,
205+ u (f"https://api.opentok.com/v2/project/{ self .api_key } /connect" ),
206+ body = self .response_body ,
207+ status = 200 ,
208+ content_type = u ("application/json" ),
209+ )
210+
211+ websocket_options = {
212+ "uri" : "wss://service.com/ws-endpoint" ,
213+ "audio_transport" : {
214+ "transport" : "json" ,
215+ "encoding" : "base64" ,
216+ "audio_field" : "data" ,
217+ "static_fields" : {"event" : "media" },
218+ },
219+ }
220+
221+ websocket_audio_connection = self .opentok .connect_audio_to_websocket (
222+ self .session_id , self .token , websocket_options
223+ )
224+
225+ if PY3 :
226+ body = json .loads (httpretty .last_request ().body .decode ("utf-8" ))
227+
228+ parsed = json .loads (body ["websocket" ]["audioTransport" ])
229+ expect (parsed ["transport" ]).to (equal ("json" ))
230+ expect (parsed ["encoding" ]).to (equal ("base64" ))
231+ expect (parsed ["audio_field" ]).to (equal ("data" ))
232+ expect (parsed ["static_fields" ]).to (equal ({"event" : "media" }))
233+ expect (websocket_audio_connection ).to (be_a (WebSocketAudioConnection ))
234+
235+ @httpretty .activate
236+ def test_connect_audio_to_websocket_with_binary_transport (self ):
237+ httpretty .register_uri (
238+ httpretty .POST ,
239+ u (f"https://api.opentok.com/v2/project/{ self .api_key } /connect" ),
240+ body = self .response_body ,
241+ status = 200 ,
242+ content_type = u ("application/json" ),
243+ )
244+
245+ websocket_options = {
246+ "uri" : "wss://service.com/ws-endpoint" ,
247+ "audio_transport" : {
248+ "transport" : "binary" ,
249+ },
250+ }
251+
252+ websocket_audio_connection = self .opentok .connect_audio_to_websocket (
253+ self .session_id , self .token , websocket_options
254+ )
255+
256+ if PY3 :
257+ body = json .loads (httpretty .last_request ().body .decode ("utf-8" ))
258+
259+ expect (body ["websocket" ]["audioTransport" ]).to (equal ('{"transport":"binary"}' ))
260+ expect (websocket_audio_connection ).to (be_a (WebSocketAudioConnection ))
261+
262+ @httpretty .activate
263+ def test_connect_audio_to_websocket_without_audio_transport (self ):
264+ httpretty .register_uri (
265+ httpretty .POST ,
266+ u (f"https://api.opentok.com/v2/project/{ self .api_key } /connect" ),
267+ body = self .response_body ,
268+ status = 200 ,
269+ content_type = u ("application/json" ),
270+ )
271+
272+ websocket_options = {"uri" : "wss://service.com/ws-endpoint" }
273+
274+ self .opentok .connect_audio_to_websocket (
275+ self .session_id , self .token , websocket_options
276+ )
277+
278+ if PY3 :
279+ body = json .loads (httpretty .last_request ().body .decode ("utf-8" ))
280+
281+ expect (body ["websocket" ]).not_to (have_key ("audioTransport" ))
282+ expect (body ["websocket" ]).not_to (have_key ("audio_transport" ))
283+
284+ def test_connect_audio_to_websocket_invalid_audio_transport_type (self ):
285+ websocket_options = {
286+ "uri" : "wss://service.com/ws-endpoint" ,
287+ "audio_transport" : "invalid" ,
288+ }
289+ with self .assertRaises (InvalidWebSocketOptionsError ) as context :
290+ self .opentok .connect_audio_to_websocket (self .session_id , self .token , websocket_options )
291+ self .assertTrue ("'audio_transport' must be a dictionary if provided." in str (context .exception ))
292+
293+ def test_connect_audio_to_websocket_audio_transport_missing_transport (self ):
294+ websocket_options = {
295+ "uri" : "wss://service.com/ws-endpoint" ,
296+ "audio_transport" : {"encoding" : "base64" },
297+ }
298+ with self .assertRaises (InvalidWebSocketOptionsError ) as context :
299+ self .opentok .connect_audio_to_websocket (self .session_id , self .token , websocket_options )
300+ self .assertTrue ("'audio_transport' must include a 'transport' field." in str (context .exception ))
301+
302+ def test_connect_audio_to_websocket_audio_transport_invalid_transport (self ):
303+ websocket_options = {
304+ "uri" : "wss://service.com/ws-endpoint" ,
305+ "audio_transport" : {"transport" : "invalid" },
306+ }
307+ with self .assertRaises (InvalidWebSocketOptionsError ) as context :
308+ self .opentok .connect_audio_to_websocket (self .session_id , self .token , websocket_options )
309+ self .assertTrue ("'transport' must be one of" in str (context .exception ))
0 commit comments