55import asyncio
66import os
77import logging
8+ import urllib .request
9+ import tempfile
10+ import shutil
811from hbmqtt .client import MQTTClient , ConnectException
912from hbmqtt .broker import Broker
1013from hbmqtt .mqtt .constants import QOS_0 , QOS_1 , QOS_2
@@ -43,8 +46,15 @@ def setUp(self):
4346 self .loop = asyncio .new_event_loop ()
4447 asyncio .set_event_loop (self .loop )
4548
49+ self .temp_dir = tempfile .mkdtemp (prefix = 'hbmqtt-test-' )
50+ url = "http://test.mosquitto.org/ssl/mosquitto.org.crt"
51+ self .ca_file = os .path .join (self .temp_dir , 'mosquitto.org.crt' )
52+ urllib .request .urlretrieve (url , self .ca_file )
53+ log .info ("stored mosquitto cert at %s" % self .ca_file )
54+
4655 def tearDown (self ):
4756 self .loop .close ()
57+ shutil .rmtree (self .temp_dir )
4858
4959 def test_connect_tcp (self ):
5060 @asyncio .coroutine
@@ -68,8 +78,7 @@ def test_connect_tcp_secure(self):
6878 def test_coro ():
6979 try :
7080 client = MQTTClient (config = {'check_hostname' : False })
71- ca = os .path .join (os .path .dirname (os .path .realpath (__file__ )), 'mosquitto.org.crt' )
72- yield from client .connect ('mqtts://test.mosquitto.org/' , cafile = ca )
81+ yield from client .connect ('mqtts://test.mosquitto.org/' , cafile = self .ca_file )
7382 self .assertIsNotNone (client .session )
7483 yield from client .disconnect ()
7584 future .set_result (True )
@@ -115,7 +124,6 @@ def test_coro():
115124 self .loop .run_until_complete (test_coro ())
116125 if future .exception ():
117126 raise future .exception ()
118- raise future .exception ()
119127
120128 def test_reconnect_ws_retain_username_password (self ):
121129 @asyncio .coroutine
@@ -148,8 +156,7 @@ def test_coro():
148156 broker = Broker (broker_config , plugin_namespace = "hbmqtt.test.plugins" )
149157 yield from broker .start ()
150158 client = MQTTClient ()
151- ca = os .path .join (os .path .dirname (os .path .realpath (__file__ )), 'mosquitto.org.crt' )
152- yield from client .connect ('ws://127.0.0.1:8081/' , cafile = ca )
159+ yield from client .connect ('ws://127.0.0.1:8081/' , cafile = self .ca_file )
153160 self .assertIsNotNone (client .session )
154161 yield from client .disconnect ()
155162 yield from broker .shutdown ()
0 commit comments