def test_wrong_ep(self): print("Endpoint name already exists") client = HelperClient(self.server_address) path = "rd?ep=node1&con=coap://local-proxy-old.example.com:5683<=60" ct = {'content_type': defines.Content_types["application/link-format"]} payload = '</sensors/temp>;ct=41;rt="temperature-c";if="sensor";anchor="coap://spurious.example.com:5683",' \ '</sensors/light>;ct=41;rt="light-lux";if="sensor"' client.post(path, payload, None, None, **ct) client.stop() path = "rd?ep=node1&con=coap://local-proxy-old.example.com:5683" req = Request() req.code = defines.Codes.POST.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address req.content_type = defines.Content_types["application/link-format"] req.payload = '</sensors/temp>;ct=41;rt="temperature-c";if="sensor";' \ 'anchor="coap://spurious.example.com:5683",</sensors/light>;ct=41;rt="light-lux";if="sensor"' expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.SERVICE_UNAVAILABLE.number expected.token = None expected.content_type = 0 expected.payload = None self.current_mid += 1 self._test_check([(req, expected)])
def test_td_coap_core_07(self): print "TD_COAP_CORE_07" path = "/test" req = Request() req.code = defines.inv_codes['PUT'] req.uri_path = path req.type = defines.inv_types["NON"] o = Option() o.number = defines.inv_options["Content-Type"] o.value = defines.inv_content_types["application/xml"] req.add_option(o) req._mid = self.current_mid req.destination = self.server_address req.payload = "<value>test</value>" expected = Response() expected.type = defines.inv_types["NON"] expected._mid = None expected.code = defines.responses["CHANGED"] expected.token = None expected.payload = None self.current_mid += 1 self._test_plugtest(req, expected)
def test_lookup_expired_ep(self): print("Expired endpoint lookup") client = HelperClient(self.server_address) path = "rd?ep=node1&con=coap://local-proxy-old.example.com:5683<=60" ct = {'content_type': defines.Content_types["application/link-format"]} payload = '</sensors/temp>;ct=41;rt="temperature-c";if="sensor";anchor="coap://spurious.example.com:5683",' \ '</sensors/light>;ct=41;rt="light-lux";if="sensor"' client.post(path, payload, None, None, **ct) client.stop() path = "rd-lookup/ep?ep=node1&rt=temperature-c" req = Request() req.code = defines.Codes.GET.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address req.content_type = 0 req.payload = None expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CONTENT.number expected.token = None expected.content_type = defines.Content_types["application/link-format"] expected.payload = None self.current_mid += 1 # After 61 seconds the resource will be expired self._test_check([(req, expected)], 61)
def map(self, request): path = request.uri_path if request.uri_path == defines.DISCOVERY_URL: response = Response() response.destination = request.source response = self._resource_layer.discover(request, response) self.result_forward(response, request) server = self.root.find_complete(path) if server is not None: new_request = Request() segments = server.find_path().split("/") path = segments[2:] path = "/".join(path) segments = segments[1].split(":") host = segments[0] port = int(segments[1]) # new_request.destination = (host, port) new_request.source = request.source new_request.type = request.type new_request._mid = (self._currentMID + 1) % (1 << 16) new_request.code = request.code new_request.proxy_uri = "coap://" + str(host) + ":" + str(port) + "/" + path new_request.payload = request.payload for option in request.options: if option.name == defines.inv_options["Uri-Path"]: continue if option.name == defines.inv_options["Uri-Query"]: continue if option.name == defines.inv_options["Uri-Host"]: continue new_request.add_option(option) return new_request return None
def test_delete(self): print("Delete") client = HelperClient(self.server_address) path = "rd?ep=endpoint1<=500&con=coap://local-proxy-old.example.com:5683&et=oic.d.sensor" ct = {'content_type': defines.Content_types["application/link-format"]} payload = '</sensors/temp>;ct=41;rt="temperature-c";if="sensor";anchor="coap://spurious.example.com:5683",' \ '</sensors/light>;ct=41;rt="light-lux";if="sensor"' response = client.post(path, payload, None, None, **ct) loc_path = response.location_path client.stop() path = loc_path req = Request() req.code = defines.Codes.DELETE.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address req.content_type = 0 req.payload = None expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.DELETED.number expected.token = None expected.content_type = 0 expected.payload = None self.current_mid += 1 self._test_check([(req, expected)])
def test_post_and_get_storage(self): print "\nPOST /storage/data1 - GET /storage/data1\n" args = ("/storage/data1",) kwargs = {} path = args[0] req = Request() for key in kwargs: o = Option() o.number = defines.inv_options[key] o.value = kwargs[key] req.add_option(o) req.code = defines.inv_codes['POST'] req.uri_path = path req.type = defines.inv_types["CON"] req._mid = self.current_mid req.payload = "Created" req.destination = self.server_address expected = Response() expected.type = defines.inv_types["ACK"] expected._mid = self.current_mid expected.code = defines.responses["CREATED"] expected.token = None expected.payload = None option = Option() option.number = defines.inv_options["Location-Path"] option.value = "/storage/data1" expected.add_option(option) self.current_mid += 1 self._test(req, expected) req = Request() for key in kwargs: o = Option() o.number = defines.inv_options[key] o.value = kwargs[key] req.add_option(o) req.code = defines.inv_codes['GET'] req.uri_path = path req.type = defines.inv_types["CON"] req._mid = self.current_mid req.destination = self.server_address expected = Response() expected.type = defines.inv_types["ACK"] expected._mid = self.current_mid expected.code = defines.responses["CONTENT"] expected.token = None expected.payload = "Created" self.current_mid += 1 self._test(req, expected)
def put(self, path, payload, callback=None): # pragma: no cover request = Request() request.destination = self.server request.code = defines.Codes.PUT.number request.uri_path = path request.payload = payload if callback is not None: thread = threading.Thread(target=self._thread_body, args=(request, callback)) thread.start() else: self.protocol.send_message(request) response = self.queue.get(block=True) return response
def _create_test_sequence(self, bValidResponse=True): exchange = list() req = Request() req.code = defines.Codes.POST.number req.uri_path = "/storage/new_res?id=1" req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address req.payload = "test" req.add_if_none_match() if bValidResponse: expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CREATED.number expected.token = None expected.payload = None expected.location_path = "storage/new_res" expected.location_query = "id=1" else: expected = None exchange.append((req, expected)) self.current_mid += 1 req = Request() req.code = defines.Codes.GET.number req.uri_path = "/storage/new_res" req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address req.if_match = ["test", "not"] if bValidResponse: expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CONTENT.number expected.token = None expected.payload = "test" else: expected = None exchange.append((req, expected)) self.current_mid += 1 return exchange
def put(self, client_callback, *args, **kwargs): path, payload = args req = Request() if "Token" in kwargs.keys(): req.token = kwargs.get("Token") del kwargs["Token"] for key in kwargs: o = Option() o.number = defines.inv_options[key] o.value = kwargs[key] req.add_option(o) req.code = defines.inv_codes['PUT'] req.uri_path = path req.type = defines.inv_types["CON"] req.payload = payload self.send_callback(req, self.put_results, client_callback)
def _handle_request(self, transaction, new_resource): """ Forward requests. Used by reverse proxies to also create new virtual resources on the proxy in case of created resources :type new_resource: bool :type transaction: Transaction :param transaction: the transaction that owns the request :rtype : Transaction :param new_resource: if the request will generate a new resource :return: the edited transaction """ client = HelperClient(transaction.resource.remote_server) request = Request() request.options = copy.deepcopy(transaction.request.options) del request.block2 del request.block1 del request.uri_path del request.proxy_uri del request.proxy_schema # TODO handle observing del request.observe # request.observe = transaction.request.observe request.uri_path = "/".join(transaction.request.uri_path.split("/")[1:]) request.destination = transaction.resource.remote_server request.payload = transaction.request.payload request.code = transaction.request.code response = client.send_request(request) client.stop() transaction.response.payload = response.payload transaction.response.code = response.code transaction.response.options = response.options if response.code == defines.Codes.CREATED.number: lp = transaction.response.location_path del transaction.response.location_path transaction.response.location_path = transaction.request.uri_path.split("/")[0] + "/" + lp # TODO handle observing if new_resource: resource = RemoteResource('server', transaction.resource.remote_server, lp, coap_server=self, visible=True, observable=False, allow_children=True) self._server.add_resource(transaction.response.location_path, resource) if response.code == defines.Codes.DELETED.number: del self._server.root["/" + transaction.request.uri_path] return transaction
def put(self, *args, **kwargs): """ :param args: request object :param kwargs: dictionary with parameters """ if len(args) > 0: request = args[0] assert isinstance(request, Request) endpoint = request.destination ip, port = endpoint payload = request.payload else: request = Request() path = kwargs["path"] payload = kwargs["payload"] assert isinstance(path, str) ip, port, path = self.parse_path(path) request.destination = (ip, port) request.uri_path = path endpoint = (ip, port) request.code = defines.inv_codes["PUT"] request.payload = payload self.send(request, endpoint) self.condition.acquire() self.condition.wait() message = self._response key = hash(str(ip) + str(port) + str(message.mid)) if ( message.type == defines.inv_types["ACK"] and message.code == defines.inv_codes["EMPTY"] and key in self.sent.keys() ): # Separate Response self.send(request, endpoint) self.condition.acquire() self.condition.wait() message = self._response return message
def test_td_coap_core_07(self): print "TD_COAP_CORE_07" path = "/test" req = Request() req.code = defines.Codes.PUT.number req.uri_path = path req.type = defines.Types["NON"] req.content_type = defines.Content_types["application/xml"] req._mid = self.current_mid req.destination = self.server_address req.payload = "<value>test</value>" expected = Response() expected.type = defines.Types["NON"] expected._mid = None expected.code = defines.Codes.CHANGED.number expected.token = None expected.payload = None self.current_mid += 1 self._test_with_client([(req, expected)])
def test_edit_resource(self): print "TEST_EDIT_RESOURCE" path = "/obs" req = Request() req.code = defines.Codes.POST.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address req.payload = "<value>test</value>" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CREATED.number expected.token = None expected.payload = None expected.location_path = "/obs" self.current_mid += 1 self._test_with_client([(req, expected)])
def test_delete_res_not_found(self): print("Resource not found on delete") path = "rd/4521" req = Request() req.code = defines.Codes.DELETE.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address req.content_type = 0 req.payload = None expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.NOT_FOUND.number expected.token = None expected.content_type = 0 expected.payload = None self.current_mid += 1 self._test_check([(req, expected)])
def test_uri_discovery(self): print("Uri discovery") path = ".well-known/core" req = Request() req.code = defines.Codes.GET.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address req.content_type = 0 req.payload = None expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CONTENT.number expected.token = None expected.content_type = defines.Content_types["application/link-format"] expected.payload = '</rd-lookup/res>;rt="core.rd-lookup-res";ct=40,</rd>;rt="core.rd";ct=40,' \ '</rd-lookup/ep>;rt="core.rd-lookup-ep";ct=40' self.current_mid += 1 self._test_check([(req, expected)])
def test_no_ep(self): print("Registration without endpoint name") path = "rd?con=coap://local-proxy-old.example.com:5683" req = Request() req.code = defines.Codes.POST.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address req.content_type = defines.Content_types["application/link-format"] req.payload = '</sensors/temp>;ct=41;rt="temperature-c";if="sensor";' \ 'anchor="coap://spurious.example.com:5683",</sensors/light>;ct=41;rt="light-lux";if="sensor"' expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.BAD_REQUEST.number expected.token = None expected.content_type = 0 expected.payload = None self.current_mid += 1 self._test_check([(req, expected)])
def _forward_request(transaction, destination, path): client = HelperClient(destination) request = Request() request.options = copy.deepcopy(transaction.request.options) del request.block2 del request.block1 del request.uri_path del request.proxy_uri del request.proxy_schema # TODO handle observing del request.observe # request.observe = transaction.request.observe request.uri_path = path request.destination = destination request.payload = transaction.request.payload request.code = transaction.request.code response = client.send_request(request) client.stop() transaction.response.payload = response.payload transaction.response.code = response.code transaction.response.options = response.options return transaction
def test_wildcard_ep(self): print("Use wildcard * to find endpoints") client = HelperClient(self.server_address) path = "rd?ep=node1&con=coap://local-proxy-old.example.com:5683" ct = {'content_type': defines.Content_types["application/link-format"]} payload = '</sensors/temp>;ct=41;rt="temperature-c";if="sensor";anchor="coap://spurious.example.com:5683",' \ '</sensors/light>;ct=41;rt="light-lux";if="sensor"' response = client.post(path, payload, None, None, **ct) loc_path1 = response.location_path path = "rd?ep=node2&con=coap://[2001:db8:3::123]:61616" payload = '</temp>;rt="temperature";anchor="coap://[2001:db8:3::123]:61616"' response = client.post(path, payload, None, None, **ct) loc_path2 = response.location_path client.stop() path = "rd-lookup/ep?rt=temp*" req = Request() req.code = defines.Codes.GET.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address req.content_type = 0 req.payload = None expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CONTENT.number expected.token = None expected.content_type = defines.Content_types["application/link-format"] expected.payload = '</' + loc_path1 + '>;con="coap://local-proxy-old.example.com:5683";ep="node1";lt=500,' \ '</' + loc_path2 + '>;con="coap://[2001:db8:3::123]:61616";' \ 'ep="node2";lt=500' self.current_mid += 1 self._test_check([(req, expected)])
def _forward_request(transaction, destination, path): """ Forward requests. :type transaction: Transaction :param transaction: the transaction that owns the request :param destination: the destination of the request (IP, port) :param path: the path of the request. :rtype : Transaction :return: the edited transaction """ client = HelperClient(destination) request = Request() request.options = copy.deepcopy(transaction.request.options) del request.block2 del request.block1 del request.uri_path del request.proxy_uri del request.proxy_schema # TODO handle observing del request.observe # request.observe = transaction.request.observe request.uri_path = path request.destination = destination request.payload = transaction.request.payload request.code = transaction.request.code response = client.send_request(request) client.stop() if response is not None: transaction.response.payload = response.payload transaction.response.code = response.code transaction.response.options = response.options else: transaction.response.code = defines.Codes.SERVICE_UNAVAILABLE.number return transaction
def _handle_request(self, transaction, new_resource): client = HelperClient(transaction.resource.remote_server) request = Request() request.options = copy.deepcopy(transaction.request.options) del request.block2 del request.block1 del request.uri_path del request.proxy_uri del request.proxy_schema # TODO handle observing del request.observe # request.observe = transaction.request.observe request.uri_path = "/".join(transaction.request.uri_path.split("/")[1:]) request.destination = transaction.resource.remote_server request.payload = transaction.request.payload request.code = transaction.request.code response = client.send_request(request) client.stop() transaction.response.payload = response.payload transaction.response.code = response.code transaction.response.options = response.options if response.code == defines.Codes.CREATED.number: lp = transaction.response.location_path del transaction.response.location_path transaction.response.location_path = transaction.request.uri_path.split("/")[0] + "/" + lp # TODO handle observing if new_resource: resource = RemoteResource('server', transaction.resource.remote_server, lp, coap_server=self, visible=True, observable=False, allow_children=True) self._server.add_resource(transaction.response.location_path, resource) if response.code == defines.Codes.DELETED.number: del self._server.root["/" + transaction.request.uri_path] return transaction
def test_separate(self): print "TEST_SEPARATE" path = "/separate" req = Request() req.code = defines.Codes.GET.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address expected = Response() expected.type = defines.Types["CON"] expected._mid = None expected.code = defines.Codes.CONTENT.number expected.token = None exchange1 = (req, expected) self.current_mid += 1 req = Request() req.code = defines.Codes.POST.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address req.payload = "POST" expected = Response() expected.type = defines.Types["CON"] expected._mid = None expected.code = defines.Codes.CREATED.number expected.token = None expected.options = None exchange2 = (req, expected) self.current_mid += 1 req = Request() req.code = defines.Codes.PUT.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address req.payload = "PUT" expected = Response() expected.type = defines.Types["CON"] expected._mid = None expected.code = defines.Codes.CHANGED.number expected.token = None expected.options = None exchange3 = (req, expected) self.current_mid += 1 req = Request() req.code = defines.Codes.DELETE.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address expected = Response() expected.type = defines.Types["CON"] expected._mid = None expected.code = defines.Codes.DELETED.number expected.token = None exchange4 = (req, expected) self.current_mid += 1 self._test_with_client([exchange1, exchange2, exchange3, exchange4])
def test_get_delete(self): print "TEST_GET_DELETE" path = "/basic" req2 = Request() req2.code = defines.Codes.GET.number req2.uri_path = path req2.type = defines.Types["CON"] req2._mid = self.current_mid req2.destination = self.server_address req2.proxy_uri = "coap://127.0.0.1:5684/storage/new" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.NOT_FOUND.number expected.token = None expected.payload = None exchange0 = (req2, expected) self.current_mid += 1 req = Request() req.code = defines.Codes.POST.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address req.proxy_uri = "coap://127.0.0.1:5684/storage/new" req.payload = "Hello" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CREATED.number expected.token = None expected.payload = None exchange1 = (req, expected) self.current_mid += 1 # PREPARING SECOND EXPECTED RESPONSE req2 = Request() req2.code = defines.Codes.GET.number req2.uri_path = path req2.type = defines.Types["CON"] req2._mid = self.current_mid req2.destination = self.server_address req2.proxy_uri = "coap://127.0.0.1:5684/storage/new" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CONTENT.number expected.token = None expected.payload = "Hello" exchange2 = (req2, expected) self.current_mid += 1 # PREPARING THIRD EXPECTED RESPONSE req3 = Request() req3.code = defines.Codes.DELETE.number req3.uri_path = path req3.type = defines.Types["CON"] req3._mid = self.current_mid req3.destination = self.server_address req3.proxy_uri = "coap://127.0.0.1:5684/storage/new" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.DELETED.number expected.token = None expected.payload = None exchange3 = (req3, expected) self.current_mid += 1 # PREPARING FOURTH EXPECTED RESPONSE req4 = Request() req4.code = defines.Codes.GET.number req4.uri_path = path req4.type = defines.Types["CON"] req4._mid = self.current_mid req4.destination = self.server_address req4.proxy_uri = "coap://127.0.0.1:5684/storage/new" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.NOT_FOUND.number expected.token = None exchange4 = (req4, expected) self.current_mid += 1 self._test_with_client_delayed([exchange0, exchange1, exchange2, exchange3, exchange4])
def test_get_etag(self): print "TEST_GET_ETAG" path = "/etag" req = Request() req.code = defines.Codes.GET.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address req.proxy_uri = "coap://127.0.0.1:5684/etag" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CONTENT.number expected.token = None expected.payload = None expected.etag = str(0) exchange1 = (req, expected) self.current_mid += 1 # PREPARING SECOND EXPECTED RESPONSE req2 = Request() req2.code = defines.Codes.GET.number req2.uri_path = path req2.type = defines.Types["CON"] req2._mid = self.current_mid req2.destination = self.server_address req2.proxy_uri = "coap://127.0.0.1:5684/etag" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CONTENT.number expected.token = None expected.etag = str(0) expected.max_age = 1 exchange2 = (req2, expected) self.current_mid += 1 # PREPARING THIRD EXPECTED RESPONSE req3 = Request() req3.code = defines.Codes.POST.number req3.uri_path = path req3.type = defines.Types["CON"] req3._mid = self.current_mid req3.destination = self.server_address req3.proxy_uri = "coap://127.0.0.1:5684/etag" req3.payload = "Hello" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CHANGED.number expected.token = None expected.payload = None expected.etag = str(1) expected.location_path = "etag" exchange3 = (req3, expected) self.current_mid += 1 # PREPARING FOURTH EXPECTED RESPONSE req4 = Request() req4.code = defines.Codes.GET.number req4.uri_path = path req4.type = defines.Types["CON"] req4._mid = self.current_mid req4.destination = self.server_address req4.proxy_uri = "coap://127.0.0.1:5684/etag" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CONTENT.number expected.token = None expected.payload = "Hello" expected.etag = str(1) exchange4 = (req4, expected) self.current_mid += 1 self._test_with_client_delayed([exchange1, exchange2, exchange3, exchange4])
def test_td_coap_block_03(self): print "TD_COAP_BLOCK_03" path = "/large-update" req = Request() req.code = defines.inv_codes['PUT'] req.uri_path = path req.type = defines.inv_types["CON"] req._mid = self.current_mid req.destination = self.server_address req.payload = """"Me sabbee plenty"—grunted Queequeg, puffing away at his pipe """ req.block1 = (1, 1, 64) expected = Response() expected.type = defines.inv_types["ACK"] expected._mid = self.current_mid expected.code = defines.responses["CONTINUE"] expected.token = None expected.payload = None expected.block1 = (1, 1, 64) exchange1 = (req, expected) self.current_mid += 1 self.server_mid += 1 req = Request() req.code = defines.inv_codes['PUT'] req.uri_path = path req.type = defines.inv_types["CON"] req._mid = self.current_mid req.destination = self.server_address req.payload = """and sitting up in bed. "You gettee in," he added, motioning""" req.block1 = (2, 0, 64) expected = Response() expected.type = defines.inv_types["ACK"] expected._mid = self.current_mid expected.code = defines.responses["CHANGED"] expected.token = None expected.payload = None exchange2 = (req, expected) self.current_mid += 1 self.server_mid += 1 req = Request() req.code = defines.inv_codes['GET'] req.uri_path = path req.type = defines.inv_types["CON"] req._mid = self.current_mid req.destination = self.server_address expected = Response() expected.type = defines.inv_types["ACK"] expected._mid = self.current_mid expected.code = defines.responses["CONTENT"] expected.token = None expected.payload = """"Me sabbee plenty"—grunted Queequeg, puffing away at his pipe and sitting up in bed. "You gettee in," he added, motioning""" exchange3 = (req, expected) self.current_mid += 1 self._test_plugtest([exchange1, exchange2, exchange3])
def test_td_coap_core_03(self): print "TD_COAP_CORE_03" path = "/test" req = Request() req.code = defines.inv_codes['PUT'] req.uri_path = path req.type = defines.inv_types["CON"] o = Option() o.number = defines.inv_options["Content-Type"] o.value = defines.inv_content_types["application/xml"] req.add_option(o) req._mid = self.current_mid req.destination = self.server_address req.payload = "<value>test</value>" expected = Response() expected.type = defines.inv_types["ACK"] expected._mid = self.current_mid expected.code = defines.responses["CHANGED"] expected.token = None expected.payload = None self.current_mid += 1 exchange1 = (req, expected) req = Request() req.code = defines.inv_codes['GET'] req.uri_path = path req.type = defines.inv_types["CON"] req._mid = self.current_mid req.destination = self.server_address expected = Response() expected.type = defines.inv_types["ACK"] expected._mid = self.current_mid expected.code = defines.responses["CONTENT"] expected.token = None expected.payload = "<value>test</value>" option = Option() option.number = defines.inv_options["Content-Type"] option.value = defines.inv_content_types["application/xml"] expected.add_option(option) self.current_mid += 1 exchange2 = (req, expected) req = Request() req.code = defines.inv_codes['GET'] req.uri_path = path req.type = defines.inv_types["CON"] req._mid = self.current_mid req.destination = self.server_address option = Option() option.number = defines.inv_options["Accept"] option.value = defines.inv_content_types["application/xml"] req.add_option(option) expected = Response() expected.type = defines.inv_types["ACK"] expected._mid = self.current_mid expected.code = defines.responses["CONTENT"] expected.token = None expected.payload = "<value>test</value>" option = Option() option.number = defines.inv_options["Content-Type"] option.value = defines.inv_content_types["application/xml"] expected.add_option(option) self.current_mid += 1 exchange3 = (req, expected) self._test_plugtest([exchange1, exchange2, exchange3])
def test_get_etag(self): print "TEST_GET_ETAG" path = "/etag" req = Request() req.code = defines.Codes.GET.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address req.proxy_uri = "coap://127.0.0.1:5684/etag" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CONTENT.number expected.token = None expected.payload = None expected.etag = str(0) exchange1 = (req, expected) self.current_mid += 1 # PREPARING SECOND EXPECTED RESPONSE req2 = Request() req2.code = defines.Codes.GET.number req2.uri_path = path req2.type = defines.Types["CON"] req2._mid = self.current_mid req2.destination = self.server_address req2.proxy_uri = "coap://127.0.0.1:5684/etag" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CONTENT.number expected.token = None expected.etag = str(0) expected.max_age = 1 exchange2 = (req2, expected) self.current_mid += 1 # PREPARING THIRD EXPECTED RESPONSE req3 = Request() req3.code = defines.Codes.POST.number req3.uri_path = path req3.type = defines.Types["CON"] req3._mid = self.current_mid req3.destination = self.server_address req3.proxy_uri = "coap://127.0.0.1:5684/etag" req3.payload = "Hello" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CHANGED.number expected.token = None expected.payload = None expected.etag = str(1) expected.location_path = "etag" exchange3 = (req3, expected) self.current_mid += 1 # PREPARING FOURTH EXPECTED RESPONSE req4 = Request() req4.code = defines.Codes.GET.number req4.uri_path = path req4.type = defines.Types["CON"] req4._mid = self.current_mid req4.destination = self.server_address req4.proxy_uri = "coap://127.0.0.1:5684/etag" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CONTENT.number expected.token = None expected.payload = "Hello" expected.etag = str(1) exchange4 = (req4, expected) self.current_mid += 1 self._test_with_client_delayed( [exchange1, exchange2, exchange3, exchange4])
def test_get_delete(self): six.print_("TEST_GET_DELETE") path = "/basic" req2 = Request() req2.code = defines.Codes.GET.number req2.uri_path = path req2.type = defines.Types["CON"] req2._mid = self.current_mid req2.destination = self.server_address req2.proxy_uri = "coap://127.0.0.1:5684/storage/new" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.NOT_FOUND.number expected.token = None expected.payload = None exchange0 = (req2, expected) self.current_mid += 1 req = Request() req.code = defines.Codes.POST.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address req.proxy_uri = "coap://127.0.0.1:5684/storage/new" req.payload = "Hello" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CREATED.number expected.token = None expected.payload = None exchange1 = (req, expected) self.current_mid += 1 # PREPARING SECOND EXPECTED RESPONSE req2 = Request() req2.code = defines.Codes.GET.number req2.uri_path = path req2.type = defines.Types["CON"] req2._mid = self.current_mid req2.destination = self.server_address req2.proxy_uri = "coap://127.0.0.1:5684/storage/new" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CONTENT.number expected.token = None expected.payload = "Hello" exchange2 = (req2, expected) self.current_mid += 1 # PREPARING THIRD EXPECTED RESPONSE req3 = Request() req3.code = defines.Codes.DELETE.number req3.uri_path = path req3.type = defines.Types["CON"] req3._mid = self.current_mid req3.destination = self.server_address req3.proxy_uri = "coap://127.0.0.1:5684/storage/new" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.DELETED.number expected.token = None expected.payload = None exchange3 = (req3, expected) self.current_mid += 1 # PREPARING FOURTH EXPECTED RESPONSE req4 = Request() req4.code = defines.Codes.GET.number req4.uri_path = path req4.type = defines.Types["CON"] req4._mid = self.current_mid req4.destination = self.server_address req4.proxy_uri = "coap://127.0.0.1:5684/storage/new" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.NOT_FOUND.number expected.token = None exchange4 = (req4, expected) self.current_mid += 1 self._test_with_client_delayed( [exchange0, exchange1, exchange2, exchange3, exchange4])
def test_td_coap_core_03(self): print "TD_COAP_CORE_03" path = "/test" req = Request() req.code = defines.Codes.PUT.number req.uri_path = path req.type = defines.Types["CON"] req.content_type = defines.Content_types["application/xml"] req._mid = self.current_mid req.destination = self.server_address req.payload = "<value>test</value>" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CHANGED.number expected.token = None expected.payload = None self.current_mid += 1 exchange1 = (req, expected) req = Request() req.code = defines.Codes.GET.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CONTENT.number expected.token = None expected.payload = "<value>test</value>" expected.content_type = defines.Content_types["application/xml"] self.current_mid += 1 exchange2 = (req, expected) req = Request() req.code = defines.Codes.GET.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address req.accept = defines.Content_types["application/xml"] expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CONTENT.number expected.token = None expected.payload = "<value>test</value>" expected.content_type = defines.Content_types["application/xml"] self.current_mid += 1 exchange3 = (req, expected) self._test_with_client([exchange1, exchange2, exchange3])
def forward_request(self, request): """ Forward an incoming request to the specified server. :param request: the request to be forwarded :return: None if success, send an error otherwise """ # print "FORWARD REQUEST\n" uri = request.proxy_uri response = Response() response.destination = request.source token = self.generate_token() if uri is None: return self.send_error(request, response, "BAD_REQUEST") host, port, path = self.parse_path(uri) request.uri_path = path self._currentMID += 1 method = defines.codes[request.code] if method == 'GET': function = self.client.protocol.get req = Request() req.destination = (str(host), int(port)) req.mid = self._currentMID % (1 << 16) req.token = str(token) req.uri_path = path req.type = defines.inv_types["CON"] req.code = defines.inv_codes['GET'] args = (req,) kwargs = {} callback = self.result_forward err_callback = self.error elif method == 'POST': function = self.client.protocol.post req = Request() req.destination = (str(host), int(port)) req.mid = self._currentMID % (1 << 16) req.token = str(token) req.uri_path = path req.payload = request.payload req.type = defines.inv_types["CON"] req.code = defines.inv_codes['POST'] args = (req,) kwargs = {} callback = self.result_forward err_callback = self.error elif method == 'PUT': function = self.client.protocol.put req = Request() req.destination = (str(host), int(port)) req.mid = self._currentMID % (1 << 16) req.token = str(token) req.uri_path = path req.payload = request.payload req.type = defines.inv_types["CON"] req.code = defines.inv_codes['PUT'] args = (req,) kwargs = {} callback = self.result_forward err_callback = self.error elif method == 'DELETE': function = self.client.protocol.delete req = Request() req.destination = (str(host), int(port)) req.mid = self._currentMID % (1 << 16) req.token = str(token) req.uri_path = path req.type = defines.inv_types["CON"] req.code = defines.inv_codes['DELETE'] args = (req,) kwargs = {} callback = self.result_forward err_callback = self.error else: return self.send_error(request, response, "BAD_REQUEST") for option in request.options: if option.safe: kwargs[option.name] = option.value self.send_ack([request]) operations = [(function, args, kwargs, (callback, err_callback))] key = hash(str(host) + str(port) + str(token)) self._forward[key] = request key = hash(str(host) + str(port) + str(self._currentMID % (1 << 16))) self._forward_mid[key] = request # print "************" # print str(host), str(port), str(self._currentMID % (1 << 16)) # print req # print "************" key = req.mid self.sent[key] = (req, time.time()) # self.sent[str(self._currentMID % (1 << 16))] = (req, time.time()) self.client.start(operations) # Render_GET # key_timer = hash(str(request.source[0]) + str(request.source[1]) + str(request.mid)) # self.timer[key_timer] = reactor.callLater(defines.SEPARATE_TIMEOUT, self.send_ack, [request]) return None