Exemplo n.º 1
0
def add_static_route(api, files_dir):
    file_dir = os.path.dirname(os.path.realpath(__file__))
    static_dir = os.path.join(file_dir, files_dir)
    api.add_route('/', StaticFiles(static_dir, 'index.html'))
    api.add_route('/favicon.ico', StaticFiles(static_dir, 'favicon.ico', False))
    api.add_route('/styles/monitorrent.css', StaticFiles(os.path.join(static_dir, 'styles'), 'monitorrent.css', False))
    api.add_route('/login', StaticFiles(static_dir, 'login.html', False))
    for d, dirnames, files in os.walk(static_dir):
        parts = d[len(file_dir) + 1:].split(os.path.sep)
        url = '/' + '/'.join(parts[1:] + ['{filename}'])
        api.add_route(url, StaticFiles(d))
    def test_static_index(self, index_text):
        # with not ascii chars in index_text test fails on travis-ci
        stat_class = namedtuple('stat_class', ['st_mtime'])
        time_time = 1105022732.0
        stat = stat_class(st_mtime=time_time)
        with patch("monitorrent.rest.static_file.open", mock_open(read_data=index_text), create=True), \
                patch("monitorrent.rest.static_file.os.path.isfile", Mock(return_value=True), create=True), \
                patch("monitorrent.rest.static_file.os.stat", Mock(return_value=stat), create=True), \
                patch("monitorrent.rest.static_file.mimetypes.guess_type", Mock(return_value=('text/html', 'utf-8')),
                      create=True), \
                patch("monitorrent.rest.static_file.os.path.getsize", MagicMock(return_value=len(index_text)),
                      create=True):
            s = StaticFiles('folder', 'index.html', False)
            self.api.add_route('/index.html', s)

            body = self.simulate_request('/index.html')
            self.assertEqual(self.srmock.status, falcon.HTTP_OK)
            self.assertEqual('text/html',
                             self.srmock.headers_dict['content-type'])
            self.assertEqual(str(len(index_text)),
                             self.srmock.headers_dict['content-length'])
            self.assertEqual('Thu, 06 Jan 2005 14:45:32 GMT',
                             self.srmock.headers_dict['last-modified'])
            self.assertEqual('1105022732.0', self.srmock.headers_dict['etag'])
            self.assertEqual(str(next(body)), index_text)
    def test_not_found(self):
        if_file_mock = MagicMock(return_value=False)
        with patch("monitorrent.rest.static_file.os.path.isfile",
                   if_file_mock,
                   create=True):
            s = StaticFiles('folder', 'index.html', False)
            self.api.add_route('/index.html', s)

            self.simulate_request('/index.html')
            self.assertEqual(self.srmock.status, falcon.HTTP_NOT_FOUND)
    def test_redirect_to_login(self):
        index_text = '<HTML></HTML>'
        m = mock_open(read_data=index_text)
        getsize = MagicMock(return_value=len(index_text))
        with patch("monitorrent.rest.static_file.open", m, create=True), \
                patch("monitorrent.rest.static_file.os.path.getsize", getsize, create=True):
            s = StaticFiles('folder', 'index.html')
            self.api.add_route('/index.html', s)

            self.simulate_request('/index.html')
            self.assertEqual(self.srmock.status, falcon.HTTP_FOUND)
            self.assertEqual('/login', self.srmock.headers_dict['location'])
    def test_static_index_if_modified_since(self):
        index_text = u'<HTML></HTML>'
        # with not ascii chars in index_text test fails on travis-ci
        stat_class = namedtuple('stat_class', ['st_mtime'])
        time_time = 1105022732.0
        stat = stat_class(st_mtime=time_time)
        with patch("monitorrent.rest.static_file.open", mock_open(read_data=index_text), create=True), \
                patch("monitorrent.rest.static_file.os.path.isfile", Mock(return_value=True), create=True), \
                patch("monitorrent.rest.static_file.os.stat", Mock(return_value=stat), create=True), \
                patch("monitorrent.rest.static_file.mimetypes.guess_type", Mock(return_value=('text/html', 'utf-8')),
                      create=True), \
                patch("monitorrent.rest.static_file.os.path.getsize", MagicMock(return_value=len(index_text)),
                      create=True):
            s = StaticFiles('folder', 'index.html', False)
            self.api.add_route('/index.html', s)

            headers = {'If-Modified-Since': formatdate(time_time, usegmt=True)}
            self.simulate_request('/index.html', headers=headers)
            self.assertEqual(self.srmock.status, falcon.HTTP_NOT_MODIFIED)
            self.assertEqual('text/html',
                             self.srmock.headers_dict['content-type'])
            self.assertEqual('Thu, 06 Jan 2005 14:45:32 GMT',
                             self.srmock.headers_dict['last-modified'])
            self.assertEqual('1105022732.0', self.srmock.headers_dict['etag'])