class CarelessTest(unittest.TestCase): """ Careless testing suite """ def setUp(self): """ Set up of a careless server for request testing """ self.server = Endpoint(__name__) def careless(self, request): """ This implements a 'separate' resource for demonstration purposes. Defines a resource that returns a response in a separate CoAP Message 1 °) promise the client that this request will be acted upon by sending an Acknowledgement... :param request: """ request.accept() # ... and then do nothing. Pretty mean. self.server.route("careless", title="This resource will ACK anything, " "but never send a separate response", resourceType="SeparateResponseTester", function=careless) def test_careless(self): """ todo """ pass
def setUp(self): """ Set up a configuration as specified in CoAP_CFG_01 """ server = Endpoint() res = Observe() server.addResource(res)
def setUp(self): """ TODO """ server = Endpoint() res = ImageResource() server.register(res)
def setUp(self): """ Setup a CoAP server and assign a TimeResource to it """ self.server = Endpoint(__name__) class TimeResource(Resource): """ Simple Time Resource that give the time and support observation. :return: """ def __call__(self): """ Resource that returns the current time on a GET request. It also Supports observing. """ self.time = datetime.datetime.now() self.changed() # Call changed to notify subscribers return self.time self.server.add_url_rule("/time", obs=True, methods=["GET", "POST"], resource=TimeResource(title="CurrentTime"))
def setUp(self): """ TODO """ res = ResourceTest() server = Endpoint() server.addResource(res)
def setUp(self): """ Setting up a CoAP server running on localhost for testing. """ server = Endpoint() server.discoveryResource() server.run()
class TestTimeResource(unittest.TestCase): """ Observing time resource """ def setUp(self): """ Setup a CoAP server and assign a TimeResource to it """ self.server = Endpoint(__name__) class TimeResource(Resource): """ Simple Time Resource that give the time and support observation. :return: """ def __call__(self): """ Resource that returns the current time on a GET request. It also Supports observing. """ self.time = datetime.datetime.now() self.changed() # Call changed to notify subscribers return self.time self.server.add_url_rule("/time", obs=True, methods=["GET", "POST"], resource=TimeResource(title="CurrentTime")) def test_get(self): """ Simple get test """ r = coap.get(self.server.url + "/time") self.assertEqual(r.status, codes.ok) def test_observe(self): """ Implement a simple observe process """ r = coap.observe(self.server.url + "/time") self.assertEqual(codes.ok, r.code)
def setUp(self): """ Set up a loop to fetch the weather. :return: """ self.server = Endpoint(__name__) def weather(self): """ Infinite loop checking weather on a exterior website """ while True: r = urlopen("http://openweather.com/") publish(content=r.readall()) self.server.add_url_rule("/weather", function=weather, observable=True, t=["json", "text"], title="GET the current weather in Paris")
class TestWebCoAP(unittest.TestCase): """ Interface CoAP, HTTP and observing in a simple weather setting. """ def setUp(self): """ Set up a loop to fetch the weather. :return: """ self.server = Endpoint(__name__) def weather(self): """ Infinite loop checking weather on a exterior website """ while True: r = urlopen("http://openweather.com/") publish(content=r.readall()) self.server.add_url_rule("/weather", function=weather, observable=True, t=["json", "text"], title="GET the current weather in Paris") def test_simple_get(self): """ Simple get """ temp = coap.get(self.server.url + "/weather") self.assertTrue(-50 < temp < 50) def test_simple_observe(self): """ Simple observe """ obs = coap.observe(self.server.url + "/weather", sub=subscribe) for item in obs.listen(5): self.assertTrue(-50 < item < 50)
def setUp(self): """ Setup a simple server with an hello world resource. Defines a resource that returns text with special characters on GET. """ self.server = Endpoint(__name__) @self.server.route("/hello", title="Hello-World Resource", rt="HelloWorld") def hello(): return self.message
class BasicFunctionalityTestCase(unittest.TestCase): """ Basic routing and endpoint testing """ def setUp(self): """ Set up a simple Endpoint. All the settings will be done in the further tests. """ self.server = Endpoint(__name__) def test_request_dispatching(self): """ Simple request dispatching """ def index(request): """ Simple mirror view :param request: Incoming request """ return request.method self.server.route("/", index) self.server.route("/more", index, methods=['GET', 'POST']) self.assertEqual(coap.get(self.server.url + '/').data, 'GET') self.assertEqual(coap.post(self.server.url + '/').status_code, 405) self.assertEqual(coap.get(self.server.url + '/').status_code, 200) self.assertEqual(coap.post(self.server.url + '/more').data, 'POST') self.assertEqual(coap.get(self.server.url + '/more').data, 'GET') self.assertEqual( coap.delete(self.server.url + '/more').status_code, 405) def test_url_mapping(self): """ Simple URL mapping """ method = lambda request: request.method self.server.add_url_rule('/', 'index', method) self.server.add_url_rule('/more', 'more', method, methods=['GET', 'POST']) self.assertEqual(coap.get(self.server.url + '/').data, 'GET') rv = coap.post(self.server.url + '/') self.assertEqual(rv.status_code, 405) rv = coap.delete(self.server.url + '/more') self.assertEqual(rv.status_code, 405) def test_routing(self): """ Routing testing """ self.server.url_map.add(('/foo', [ Rule('/bar', endpoint='bar'), Rule('/', endpoint='index') ])) self.server.view_functions['bar'] = lambda: "bar" self.server.view_functions['index'] = lambda: "index" self.assertEqual(coap.get('/foo/').data, 'index') self.assertEqual(coap.get('/foo/bar').data, 'bar') def test_endpoint_decorator(self): """ Test sub rule """ self.server.url_map.add( ('/foo', [ Rule('/bar', endpoint='bar'), Rule('/', endpoint='index') ])) self.server.destination('bar', lambda: "bar") self.server.destination('index', lambda: "index") self.assertEqual(coap.get('/foo/').data, "index") self.assertEqual(coap.get('/foo/bar').data, "bar") def test_error_handling(self): """ Testing of the exception in the Endpoint """ self.server.errorhandler(404, 'not found') self.server.errorhandler(500, 'internal server error') def index(): """ not_found called """ self.server.abort(404) def error(): """ internal_server_error called """ return 1 // 0 self.server.route('/', index) self.server.route("/error", error) rv = coap.get(self.server.url + '/') self.assertEqual(rv.status_code, 404) self.assertEqual(rv.data, 'not found') rv = coap.get(self.server.url + '/error') self.assertEqual(rv.status_code, 500) self.assertEqual('internal server error', rv.data) def test_unicode(self): """ Test unicode encoding """ self.server.route('/unicode', lambda: 'Hällo Wörld') self.assertEqual( coap.get('/unicode').data, 'Hällo Wörld'.encode()) def test_max_content_length(self): """ Test of the limitation of posting data on an endpoint :return: """ self.server.config['MAX_CONTENT_LENGTH'] = 64 rv = coap.post('/accept', data={'my_file': 'foo' * 100}) self.assertEqual(rv.data, '42')
- Resources used in TD_COAP_CORE tests should not exceed 64 bytes - Large resources used in TD_COAP_BLOCK tests shall not exceed 2048 bytes - TD_COAP_LINK tests may require usage of Block options with some implementations """ import datetime from threading import Timer import unittest from pycolo import codes from pycolo.codes import mediaCodes from pycolo.endpoint import Endpoint from pycolo.message import Response from pycolo.request import request from pycolo.resource import Resource server = Endpoint("PlugTestServer", PLUGTEST_BLOCK_SIZE=64) @server.add("/large", rt="BlockWiseTransferTester", methods=["GET"], title="This is a large resource (>1024 bytes) for testing block-wise transfer.") def large(): """ This class implements a resource that returns a larger amount of data on GET requests in order to test blockwise transfers. Large resources used in TD_COAP_BLOCK tests shall not exceed 2048 bytes This resource implements a test of specification for the ETSI IoT CoAP Plugtests.
def setUp(self): """ Set up a simple Endpoint. All the settings will be done in the further tests. """ self.server = Endpoint(__name__)
def setUp(self): """ Set up a basic endpoint for CoAP message exchange """ self.server = Endpoint(__name__)