def test_custom_local_uri(self):
        """test a local uri with custom fields"""
        custom_uri = uri.build_uri(namespace='custom', relative_path='path/to/stuff')
        self.assertTrue('/' + uri.DEFAULT_SCALE_URI_NAMESPACE not in custom_uri,
                        'custom uri should not contain default namespace: %s' % custom_uri)
        self.assertTrue('path/to/stuff' in custom_uri)

        custom_uri = uri.build_uri(namespace=None, relative_path='path/to/stuff')
        self.assertTrue('/' + uri.DEFAULT_SCALE_URI_NAMESPACE not in custom_uri,
                        'custom uri with no namespace should not contain default namespace: %s' % custom_uri)

        # Should generate an exception if we don't specify any path components!
        self.assertRaises(ValueError, uri.build_uri)

        # Absolute path overrides relative path!
        rel_path = 'relative/path/to/something'
        abs_path = 'absolute/path/to/nothing'
        custom_uri = uri.build_uri(path=abs_path, relative_path=rel_path)
        self.assertTrue(rel_path not in custom_uri, "Absolute path should override relative path: %s" % custom_uri)

        # Absolute path overrides namespace!
        namespace = 'anon/ns'
        abs_path = 'absolute/path/to/nothing'
        custom_uri = uri.build_uri(path=abs_path, namespace=namespace)
        self.assertTrue(rel_path not in custom_uri, "Absolute path should override namespace: %s" % custom_uri)
示例#2
0
    def test_custom_local_uri(self):
        """test a local uri with custom fields"""
        custom_uri = uri.build_uri(namespace='custom',
                                   relative_path='path/to/stuff')
        self.assertTrue(
            '/' + uri.DEFAULT_SCALE_URI_NAMESPACE not in custom_uri,
            'custom uri should not contain default namespace: %s' % custom_uri)
        self.assertTrue('path/to/stuff' in custom_uri)

        custom_uri = uri.build_uri(namespace=None,
                                   relative_path='path/to/stuff')
        self.assertTrue(
            '/' + uri.DEFAULT_SCALE_URI_NAMESPACE not in custom_uri,
            'custom uri with no namespace should not contain default namespace: %s'
            % custom_uri)

        # Should generate an exception if we don't specify any path components!
        self.assertRaises(ValueError, uri.build_uri)

        # Absolute path overrides relative path!
        rel_path = 'relative/path/to/something'
        abs_path = 'absolute/path/to/nothing'
        custom_uri = uri.build_uri(path=abs_path, relative_path=rel_path)
        self.assertTrue(
            rel_path not in custom_uri,
            "Absolute path should override relative path: %s" % custom_uri)

        # Absolute path overrides namespace!
        namespace = 'anon/ns'
        abs_path = 'absolute/path/to/nothing'
        custom_uri = uri.build_uri(path=abs_path, namespace=namespace)
        self.assertTrue(
            rel_path not in custom_uri,
            "Absolute path should override namespace: %s" % custom_uri)
    def test_path_formatting(self):
        """test proper path formatting e.g. sanitization of input, especially for leading/trailing slashes"""

        # these should all build the same URI
        no_slash_uri = uri.build_uri(relative_path='path/to/stuff')
        leading_slashes_uri = uri.build_uri(relative_path='//path/to/stuff')
        trailing_slashes_uri = uri.build_uri(relative_path='path/to/stuff///')
        surrounding_slashes_uri = uri.build_uri(relative_path='/path/to/stuff/')

        self.assertEqual(no_slash_uri, leading_slashes_uri)
        self.assertEqual(trailing_slashes_uri, leading_slashes_uri)
        self.assertEqual(surrounding_slashes_uri, trailing_slashes_uri)
        self.assertEqual(no_slash_uri, surrounding_slashes_uri)

        self.assertTrue(uri.parse_uri(no_slash_uri).path.startswith('/'), "URI path must always start with /")
        self.assertFalse(uri.parse_uri(no_slash_uri).path.endswith('/'), "URI path must never end with /")

        # same tests but with absolute path and namespace
        no_slash_uri = uri.build_uri(path='path/to/stuff', namespace='custom/ns')
        leading_slashes_uri = uri.build_uri(path='//path/to/stuff', namespace='/custom/ns')
        trailing_slashes_uri = uri.build_uri(path='path/to/stuff///', namespace='custom/ns//')
        surrounding_slashes_uri = uri.build_uri(path='/path/to/stuff/', namespace='//custom/ns//')

        self.assertEqual(no_slash_uri, leading_slashes_uri)
        self.assertEqual(trailing_slashes_uri, leading_slashes_uri)
        self.assertEqual(surrounding_slashes_uri, trailing_slashes_uri)
        self.assertEqual(no_slash_uri, surrounding_slashes_uri)

        self.assertTrue(uri.parse_uri(no_slash_uri).path.startswith('/'), "URI path must always start with /")
        self.assertFalse(uri.parse_uri(no_slash_uri).path.endswith('/'), "URI path must never end with /")

        # verify we don't get double slashes in paths
        no_ns_uri = uri.build_uri(namespace='', relative_path='path/to/stuff')
        self.assertTrue('//' not in no_ns_uri, "double slash // present when requesting empty namespace!")
示例#4
0
 def extract_event(request):
     """
     Extracts a SensedEvent from the payload of the request.  Tries to convert it to a remote event if it was
     left as a local one by setting the host/port/protocol.
     :param request:
     :type request: Request
     :return: the SensedEvent
     :rtype: SensedEvent
     """
     event = SensedEvent.from_json(request.payload)
     host, port = request.source
     try:
         # TODO: specify coaps if this event came through an encrypted channel?
         networks.util.process_remote_event(event,
                                            hostname=host,
                                            port=port,
                                            protocol='coap')
         # save the local resource URI so we know where exactly it entered our local client
         event.metadata['local_resource_uri'] = uri.build_uri(
             relative_path=request.uri_path)
         # QUESTION: should we do something with uri_query?  probably not used in a PUT/POST request...
     except BaseException as e:
         log.error(
             "error during converting local source to remote source in event extracted from CoAP request: %s"
             % e)
     return event
示例#5
0
 def path(self):
     """
     Get the canonical path for this Application as determined by its name and any user-specified
     path components from derived class implementations.
     :return:
     """
     return uri.build_uri(relative_path="applications/%s" % self.name)
 def path(self):
     """
     Get the canonical path for this Application as determined by its name and any user-specified
     path components from derived class implementations.
     :return:
     """
     return uri.build_uri(relative_path="sensors/%s" % self.name)
 def test_default_uri(self):
     """test a local uri with defaults"""
     default_uri = uri.build_uri(relative_path='my/events')
     self.assertTrue('my/events' in default_uri, 'relative path missing!')
     self.assertTrue(uri.DEFAULT_SCALE_URI_NAMESPACE in default_uri, 'should have default namespace!')
     self.assertTrue(uri.DEFAULT_SCALE_URI_SCHEME in default_uri, 'should have default scheme!')
     has_ip = uri.parse_uri(default_uri).gethost()
     self.assertFalse(has_ip, 'default_uri should not have IP address!')
 def remote_path(self, uri_path):
     userinfo = None
     if self._username:
         userinfo = self._username
         if self._password:
             userinfo += ':' + self._password
     return uri.build_uri(scheme='coap' if not userinfo else 'coaps',
                          host=self._hostname, port=self._port if self._port != DEFAULT_COAP_PORT else None,
                          path=uri_path, userinfo=userinfo)
示例#9
0
    def test_path_formatting(self):
        """test proper path formatting e.g. sanitization of input, especially for leading/trailing slashes"""

        # these should all build the same URI
        no_slash_uri = uri.build_uri(relative_path='path/to/stuff')
        leading_slashes_uri = uri.build_uri(relative_path='//path/to/stuff')
        trailing_slashes_uri = uri.build_uri(relative_path='path/to/stuff///')
        surrounding_slashes_uri = uri.build_uri(
            relative_path='/path/to/stuff/')

        self.assertEqual(no_slash_uri, leading_slashes_uri)
        self.assertEqual(trailing_slashes_uri, leading_slashes_uri)
        self.assertEqual(surrounding_slashes_uri, trailing_slashes_uri)
        self.assertEqual(no_slash_uri, surrounding_slashes_uri)

        self.assertTrue(
            uri.parse_uri(no_slash_uri).path.startswith('/'),
            "URI path must always start with /")
        self.assertFalse(
            uri.parse_uri(no_slash_uri).path.endswith('/'),
            "URI path must never end with /")

        # same tests but with absolute path and namespace
        no_slash_uri = uri.build_uri(path='path/to/stuff',
                                     namespace='custom/ns')
        leading_slashes_uri = uri.build_uri(path='//path/to/stuff',
                                            namespace='/custom/ns')
        trailing_slashes_uri = uri.build_uri(path='path/to/stuff///',
                                             namespace='custom/ns//')
        surrounding_slashes_uri = uri.build_uri(path='/path/to/stuff/',
                                                namespace='//custom/ns//')

        self.assertEqual(no_slash_uri, leading_slashes_uri)
        self.assertEqual(trailing_slashes_uri, leading_slashes_uri)
        self.assertEqual(surrounding_slashes_uri, trailing_slashes_uri)
        self.assertEqual(no_slash_uri, surrounding_slashes_uri)

        self.assertTrue(
            uri.parse_uri(no_slash_uri).path.startswith('/'),
            "URI path must always start with /")
        self.assertFalse(
            uri.parse_uri(no_slash_uri).path.endswith('/'),
            "URI path must never end with /")

        # verify we don't get double slashes in paths
        no_ns_uri = uri.build_uri(namespace='', relative_path='path/to/stuff')
        self.assertTrue(
            '//' not in no_ns_uri,
            "double slash // present when requesting empty namespace!")
示例#10
0
 def test_default_uri(self):
     """test a local uri with defaults"""
     default_uri = uri.build_uri(relative_path='my/events')
     self.assertTrue('my/events' in default_uri, 'relative path missing!')
     self.assertTrue(uri.DEFAULT_SCALE_URI_NAMESPACE in default_uri,
                     'should have default namespace!')
     self.assertTrue(uri.DEFAULT_SCALE_URI_SCHEME in default_uri,
                     'should have default scheme!')
     has_ip = uri.parse_uri(default_uri).gethost()
     self.assertFalse(has_ip, 'default_uri should not have IP address!')
示例#11
0
 def path(self):
     """
     If the path was unspecified at creation time, we dynamically try to get it from the device or
     build a default version in order to ensure its path is always available.
     :return:
     """
     if self._path is None:
         try:
             return self.device.path
         except AttributeError:
             self._path = uri.build_uri(relative_path='devices/%s' % self.name)
     return self._path
    def _on_message(self, mqtt_client, payload, topic, qos, retain):
        """Publishes the SensedEvent internally upon receiving it"""

        try:
            event = SensedEvent.from_json(payload)
            # NOTE: we probably don't actually have to do this as its source should already be set,
            # but just in case we add additional handling later...
            process_remote_event(event)
        except BaseException as e:
            log.error("failed to parse SensedEvent from JSON payload: %s\nError was: %s" % (payload, e))
            return

        event.metadata['mqtt_topic'] = topic
        event.metadata['mqtt_broker'] = uri.build_uri(scheme='mqtt', path='broker', host=self._hostname, port=self._hostport)
        event.metadata['time_rcvd'] = SensedEvent.get_timestamp()
        self.publish(event)
        log.debug("MqttSensor received SensedEvent from topics %s: %s" % (topic, event))
示例#13
0
    def test_remote_uri(self):
        """
        Tests whether we can properly determine whether a SensedEvent came from our local node or not.
        :return:
        """
        local_path = 'my/uri/path'
        local_uri = uri.build_uri(path=local_path)
        # test some simple remote uri strings
        self.assertFalse(uri.is_remote_uri('my_uri'))
        self.assertFalse(uri.is_remote_uri(local_uri), "default URI isn't local!")
        self.assertTrue(uri.is_remote_uri('http://www.google.com'))
        self.assertFalse(uri.is_remote_uri('file:///home/my/stuff'))

        # test custom
        remote_uri = uri.get_remote_uri(local_uri, protocol='mqtt', host='www.google.com', port=1884)
        self.assertEqual(remote_uri, 'mqtt://www.google.com:1884/%s' % local_path)
        self.assertTrue(uri.is_remote_uri(remote_uri))
 def extract_event(request):
     """
     Extracts a SensedEvent from the payload of the request.  Tries to convert it to a remote event if it was
     left as a local one by setting the host/port/protocol.
     :param request:
     :type request: Request
     :return: the SensedEvent
     :rtype: SensedEvent
     """
     event = SensedEvent.from_json(request.payload)
     host, port = request.source
     try:
         # TODO: specify coaps if this event came through an encrypted channel?
         networks.util.process_remote_event(event, hostname=host, port=port, protocol='coap')
         # save the local resource URI so we know where exactly it entered our local client
         event.metadata['local_resource_uri'] = uri.build_uri(relative_path=request.uri_path)
         # QUESTION: should we do something with uri_query?  probably not used in a PUT/POST request...
     except BaseException as e:
         log.error("error during converting local source to remote source in event extracted from CoAP request: %s" % e)
     return event
示例#15
0
    def test_remote_uri(self):
        """
        Tests whether we can properly determine whether a SensedEvent came from our local node or not.
        :return:
        """
        local_path = 'my/uri/path'
        local_uri = uri.build_uri(path=local_path)
        # test some simple remote uri strings
        self.assertFalse(uri.is_remote_uri('my_uri'))
        self.assertFalse(uri.is_remote_uri(local_uri),
                         "default URI isn't local!")
        self.assertTrue(uri.is_remote_uri('http://www.google.com'))
        self.assertFalse(uri.is_remote_uri('file:///home/my/stuff'))

        # test custom
        remote_uri = uri.get_remote_uri(local_uri,
                                        protocol='mqtt',
                                        host='www.google.com',
                                        port=1884)
        self.assertEqual(remote_uri,
                         'mqtt://www.google.com:1884/%s' % local_path)
        self.assertTrue(uri.is_remote_uri(remote_uri))
示例#16
0
    def _on_message(self, mqtt_client, payload, topic, qos, retain):
        """Publishes the SensedEvent internally upon receiving it"""

        try:
            event = SensedEvent.from_json(payload)
            # NOTE: we probably don't actually have to do this as its source should already be set,
            # but just in case we add additional handling later...
            process_remote_event(event)
        except BaseException as e:
            log.error(
                "failed to parse SensedEvent from JSON payload: %s\nError was: %s"
                % (payload, e))
            return

        event.metadata['mqtt_topic'] = topic
        event.metadata['mqtt_broker'] = uri.build_uri(scheme='mqtt',
                                                      path='broker',
                                                      host=self._hostname,
                                                      port=self._hostport)
        event.metadata['time_rcvd'] = SensedEvent.get_timestamp()
        self.publish(event)
        log.debug("MqttSensor received SensedEvent from topics %s: %s" %
                  (topic, event))