コード例 #1
0
ファイル: plugtest.py プロジェクト: Cereal84/CoAPthon
    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)
コード例 #2
0
ファイル: coap_protocol.py プロジェクト: hushuitian/CoAPthon
 def delete(self, client_callback, *args, **kwargs):
     if isinstance(args[0], str):
         path = str(args[0])
         req = Request()
         req.uri_path = path
         if "Token" in kwargs.keys():
             req.token = kwargs.get("Token")
             del kwargs["Token"]
         if "MID" in kwargs.keys():
             req.mid = kwargs.get("MID")
             del kwargs["MID"]
         if "Server" in kwargs.keys():
             req.destination = kwargs.get("Server")
             del kwargs["Server"]
     else:
         req = args[0]
     for key in kwargs:
         try:
             o = Option()
             o.number = defines.inv_options[key]
             o.value = kwargs[key]
             req.add_option(o)
         except KeyError:
             pass
     req.code = defines.inv_codes['DELETE']
     req.type = defines.inv_types["CON"]
     self.send_callback(req, self.delete_results, client_callback)
コード例 #3
0
    def test_get_separate(self):
        print "\nGET /separate\n"
        args = ("/separate",)
        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['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["CON"]
        expected.code = defines.responses["CONTENT"]
        expected.token = None
        expected.payload = "Separate"

        self.current_mid += 1
        self._test_separate(req, expected)
コード例 #4
0
    def test_long(self):
        print "\nGET /long\n"
        args = ("/long",)
        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['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 = None
        expected.token = None
        expected.payload = None

        expected2 = Response()
        expected2.type = defines.inv_types["CON"]
        expected2.code = defines.responses["CONTENT"]
        expected2.token = None
        expected2.payload = "Long Time"

        self.current_mid += 1
        self._test_modular([(req, expected), (None, expected2)])
コード例 #5
0
    def test_get_not_found(self):
        print "\nGET /not_found\n"
        args = ("/not_found",)
        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['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["NOT_FOUND"]
        expected.token = None
        expected.payload = None

        self.current_mid += 1
        self._test(req, expected)
コード例 #6
0
 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
コード例 #7
0
    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)
コード例 #8
0
ファイル: coap_protocol.py プロジェクト: johanzander/CoAPthon
 def delete(self, client_callback, *args, **kwargs):
     path = args[0]
     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['DELETE']
     req.uri_path = path
     req.type = defines.inv_types["CON"]
     self.send_callback(req, self.delete_results, client_callback)
コード例 #9
0
ファイル: coap_protocol.py プロジェクト: johanzander/CoAPthon
 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)
コード例 #10
0
    def test_get_storage(self):
        args = ("/storage",)
        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['GET']
        req.uri_path = path
        req.type = defines.inv_types["CON"]
        req._mid = self.current_mid

        expected = Response()
        expected.type = defines.inv_types["ACK"]
        expected._mid = self.current_mid
        expected.code = defines.responses["CONTENT"]
        expected.token = None
        expected.payload = "Storage Resource for PUT, POST and DELETE"

        self._test(req, expected)
コード例 #11
0
    def test_big(self):
        print "\nGET /big\n"
        args = ("/big",)
        path = args[0]

        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 = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras sollicitudin fermentum ornare." \
                           " Cras accumsan tellus quis dui lacinia eleifend. Proin ultrices rutrum orci vitae luctus. " \
                           "Nullam malesuada pretium elit, at aliquam odio vehicula in. Etiam nec maximus elit. Etiam " \
                           "at erat ac ex ornare feugiat. Curabitur sed malesuada orci, id aliquet nunc. Phasellus nec " \
                           "leo luctus, blandit lorem sit amet, interdum metus. Duis efficitur volutpat magna, ac " \
                           "ultricies nibh aliquet sit amet. Etiam tempor egestas augue in hendrerit. Nunc eget augue " \
                           "ultricies, dignissim lacus et, vulputate dolor. Nulla eros odio, fringilla vel massa ut, " \
                           "facilisis cursus quam. Fusce faucibus lobortis congue. Fusce consectetur porta neque, id " \
                           "sollicitudin velit maximus eu. Sed pharetra leo quam, vel finibus turpis cursus ac. Aenean " \
                           "ac nisi massa. Cras commodo arcu nec ante tristique ullamcorper. Quisque eu hendrerit urna. " \
                           "Cras fringilla eros ut nunc maximus, non porta nisl mollis. Aliquam in rutrum massa. " \
                           "Praesent tristique turpis dui, at ultri"
        option = Option()
        option.number = defines.inv_options["Block2"]
        option.value = 14
        expected.add_option(option)

        self.current_mid += 1

        req2 = Request()
        req2.code = defines.inv_codes['GET']
        req2.uri_path = path
        req2.type = defines.inv_types["CON"]
        req2._mid = self.current_mid
        req.destination = self.server_address

        option = Option()
        option.number = defines.inv_options["Block2"]
        option.value = 22
        req2.add_option(option)

        expected2 = Response()
        expected2.type = defines.inv_types["ACK"]
        expected2.code = defines.responses["CONTENT"]
        expected2._mid = self.current_mid
        expected2.token = None
        expected2.payload = "cies lorem fermentum at. Vivamus sit amet ornare neque, a imperdiet nisl. Quisque a " \
                            "iaculis libero, id tempus lacus. Aenean convallis est non justo consectetur, a hendrerit " \
                            "enim consequat. In accumsan ante a egestas luctus. Etiam quis neque nec eros vestibulum " \
                            "faucibus. Nunc viverra ipsum lectus, vel scelerisque dui dictum a. Ut orci enim, ultrices " \
                            "a ultrices nec, pharetra in quam. Donec accumsan sit amet eros eget fermentum.Vivamus ut " \
                            "odio ac odio malesuada accumsan. Aenean vehicula diam at tempus ornare. Phasellus dictum " \
                            "mauris a mi consequat, vitae mattis nulla fringilla. Ut laoreet tellus in nisl efficitur, " \
                            "a luctus justo tempus. Fusce finibus libero eget velit finibus iaculis. Morbi rhoncus " \
                            "purus vel vestibulum ullamcorper. Sed ac metus in urna fermentum feugiat. Nulla nunc " \
                            "diam, sodales aliquam mi id, varius porta nisl. Praesent vel nibh ac turpis rutrum " \
                            "laoreet at non odio. Phasellus ut posuere mi. Suspendisse malesuada velit nec mauris " \
                            "convallis porta. Vivamus sed ultrices sapien, at cras amet."
        option = Option()
        option.number = defines.inv_options["Block2"]
        option.value = 22
        expected2.add_option(option)
        self.current_mid += 1
        self._test_modular([(req, expected), (req2, expected2)])
コード例 #12
0
ファイル: plugtest.py プロジェクト: hushuitian/CoAPthon
    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])