# coding=utf-8 import unittest from unittest import TestCase from lxml.etree import ElementTree, Element from webdav3.client import WebDavXmlUtils as utils class ClientTestCase(TestCase): def test_parse_get_list_response(self): content = '/test_dir/' \ 'HTTP/1.1 200 OK' \ 'Mon, 16 Oct 2017 04:18:00 GMT' \ 'test_dir2017-10-16T04:18:00Z' \ '/test_dir/test.txt/' \ 'HTTP/1.1 200 OK' \ 'Mon, 16 Oct 2017 04:18:18 GMTtest.txt' \ '2017-10-16T04:18:18Z' \ '' result = utils.parse_get_list_response(content) self.assertEquals(result.__len__(), 2) def test_create_free_space_request_content(self): result = utils.create_free_space_request_content() self.assertEquals(result, '\n' '') def test_parse_free_space_response(self): content = '/' \ 'HTTP/1.1 200 OK697' \ '10737417543' \ '' result = utils.parse_free_space_response(content, 'localhost') self.assertEquals(result, 10737417543) def test_parse_info_response(self): content = '' \ '/test_dir/test.txtHTTP/1.1 200 OK' \ 'Wed, 18 Oct 2017 15:16:04 GMT' \ 'ab0b4b7973803c03639b848682b5f38ctext/plain' \ '41test.txt' \ '2017-10-18T15:16:04Z' \ '' result = utils.parse_info_response(content, '/test_dir/test.txt', 'localhost') self.assertEquals(result['created'], '2017-10-18T15:16:04Z') self.assertEquals(result['name'], 'test.txt') self.assertEquals(result['modified'], 'Wed, 18 Oct 2017 15:16:04 GMT') self.assertEquals(result['size'], '41') def test_create_get_property_request_content(self): option = { 'namespace': 'test', 'name': 'aProperty' } result = utils.create_get_property_request_content(option=option, ) self.assertEquals(result, '\n' '') def test_create_get_property_request_content_name_only(self): option = { 'name': 'aProperty' } result = utils.create_get_property_request_content(option=option) self.assertEquals(result, '\n' '') def test_parse_get_property_response(self): content = '' \ '/test_dir/test.txtHTTP/1.1 200 OK' \ 'aValue' result = utils.parse_get_property_response(content=content, name='aProperty') self.assertEquals(result, 'aValue') def test_create_set_one_property_request_content(self): option = { 'namespace': 'test', 'name': 'aProperty', 'value': 'aValue' } result = utils.create_set_property_batch_request_content(options=[option]) self.assertEquals(result, '\n' 'aValue') def test_create_set_one_property_request_content_name_only(self): option = { 'name': 'aProperty' } result = utils.create_set_property_batch_request_content(options=[option]) self.assertEquals(result, '\n' '') def test_create_set_property_batch_request_content(self): options = [ { 'namespace': 'test', 'name': 'aProperty', 'value': 'aValue' }, { 'namespace': 'test2', 'name': 'aProperty2', 'value': 'aValue2' } ] result = utils.create_set_property_batch_request_content(options=options) self.assertEquals(result, '\n' 'aValueaValue2' '') def test_create_set_property_batch_request_content_name_only(self): options = [ { 'name': 'aProperty' }, { 'name': 'aProperty2' } ] result = utils.create_set_property_batch_request_content(options=options) self.assertEquals(result, '\n' '' '') def test_etree_to_string(self): tree = ElementTree(Element('test')) result = utils.etree_to_string(tree) self.assertEquals(result, '\n') def test_parse_is_dir_response_directory(self): content = '/HTTP/1.1 200 OK2012-04-04T20:00:00ZdiskWed, 04 Apr 2012 20:00:00 GMT' \ '/test_dir/HTTP/1.1 200 OK2018-05-10T07:31:13Ztest_dirThu, 10 May 2018 07:31:13 GMT/%D0%93%D0%BE%D1%80%D1%8B.jpg' \ 'HTTP/1.1 200 OK1392851f0668017168ee4b' \ '5a59d66e7b2018-05-09T14:44:28ZГоры.jpg' \ 'Wed, 09 May 2018 14:44:28 GMTimage/jpeg1762478' \ '/%D0%97%D0%B8%D0%BC%D0%B0.jpgHTTP/1.1 200 OKa64146fee5e15b3b94c204e544426d432018-05-09T14:44:28ZЗима.jpgWed, 09 May 2018 14:44:28 GMTimage/jpeg' \ '1394575/%D0%9C%D0%B8%D1%88%D0%BA%D0%B8.jpg<' \ 'd:status>HTTP/1.1 200 OK569a1c98696050439b5b2a1ecfa52d19' \ '2018-05-09T14:44:27ZМишки.jpgWed, 09 May 2018 14:44:27 GMTimage/jpeg1555830/%D0%9C%D0%BE%D1%80%D0%B5.jpgHTTP' \ '/1.1 200 OKab903d9cab031eca2a8f12f37bbc9d372018-05-09T14:44:27ZМоре.jpg' \ 'Wed, 09 May 2018 14:44:27 GMTimage/jpeg1080301' \ '/%D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0%B0.jpgHTTP/1' \ '.1 200 OKd27d72a3059ad5ebed7a5470459d26702018-05-09T14:44:27ZМосква.jpg' \ 'Wed, 09 May 2018 14:44:27 GMTimage/jpeg1454228' \ '/%D0%A1%D0%B0%D0%BD%D0%BA%D1%82-%D0%9F%D0%B5%D1%82%D0%B5%D1%80%D0%B1%D1%83%D1%' \ '80%D0%B3.jpgHTTP/1.1 200 OKf1abe3b27b' \ '410128623fd1ca00a45c292018-05-09T14:44:27ZСанкт-Петербург.jpgWed, 09 May 2018 14:44:27 GMTimage/jpeg2573704/%D0%A5%D0%BB%D0%B5%' \ 'D0%B1%D0%BD%D1%8B%D0%B5%20%D0%BA%D1%80%D0%BE%D1%88%D0%BA%D0%B8.mp4H' \ 'TTP/1.1 200 OKea977f513074d5524bee3638798183b92018-05-09T14:44:28ZХлебные крошки.mp4Wed, 09 May 2018 14:44:28 GMTvideo/mp431000079' \ '' path = '/test_dir' hostname = 'https://webdav.yandex.ru' result = utils.parse_is_dir_response(content, path, hostname) self.assertTrue(result, 'It should be directory') def test_parse_is_dir_response_directory(self): content = '/test_' \ 'dir/HTTP/1.1 200 OK2018-05-10T07' \ ':40:11Ztest_dirThu, 10 May 2018' \ ' 07:40:11 GMT<' \ 'd:href>/test_dir/test.txtHTTP/1.1 200 OKab0b4b7973803c03639b848682b5f38c2018-05-10T07:40:12Ztest.txtThu, 10 May 2018 07:40:12 GMTtext/plain41' path = '/test_dir/test.txt' hostname = 'https://webdav.yandex.ru' result = utils.parse_is_dir_response(content, path, hostname) self.assertFalse(result, 'It should be file') if __name__ == '__main__': unittest.main()