def test_session_connection_error_with_safe_mode(self): """ todo """ config = {"safe_mode": True} s = coap.session() r = s.get("coap://localhost:1/nope", timeout=0.1, config=config) self.assertFalse(r.ok) self.assertTrue(r.content is None) s2 = coap.session(config=config) r2 = s2.get("coap://localhost:1/nope", timeout=0.1) self.assertFalse(r2.ok) self.assertTrue(r2.content is None)
def test_safe_mode(self): """ todo """ safe = coap.session(config={"safe_mode": True}) # Safe mode creates empty responses for failed requests. # Iterating on these responses should produce empty sequences r = coap.get('coap://0.0.0.0:700/', session=safe) self.assertEqual(list(r.iter_lines()), []) assert isinstance(r.error, coap.exceptions.ConnectionError) r = coap.get('coap://0.0.0.0:789/', session=safe) self.assertEqual(list(r.iter_content()), []) assert isinstance(r.error, coap.exceptions.ConnectionError) # When not in safe mode, should raise Timeout exception self.assertRaises(coap.exceptions.Timeout, coap.get, self.coapbin('stream', '1000'), timeout=0.0001) # In safe mode, should return a blank response r = coap.get(self.coapbin('stream', '1000'), timeout=0.0001, config={"safe_mode": True}) assert r.content is None assert isinstance(r.error, coap.exceptions.Timeout)
def test_session_persistent_params(self): """ todo """ params = {'a': 'a_test'} s = coap.session() s.params = params # Make 2 requests from Session object, should send header both times r1 = coap.get(self.coapbin('get'), session=s) self.assertTrue(params['a'] in r1.text) params2 = {'b': 'b_test'} r2 = coap.get(self.coapbin('get'), params=params2, session=s) for param in (params['a'], params2['b']): self.assertTrue(param in r2.text) params3 = {'b': 'b_test', 'a': None, 'c': 'c_test'} r3 = coap.get(self.coapbin('get'), params=params3, session=s) self.assertFalse(params['a'] in r3.text) for param in (params3['b'], params3['c']): self.assertTrue(param in r3.text)
def test_session_COAP_200_OK_GET(self): """ todo """ s = coap.session() r = coap.get(self.coapbin('get'), session=s) self.assertEqual(r.status_code, 200)