Esempio n. 1
0
        public async Task UT_WebDavClient_PropFind_AllPropDepthZero()
        {
            var mockHandler    = new MockHttpMessageHandler();
            var requestContent = "<?xml version=\"1.0\" encoding=\"utf-8\"?><D:propfind xmlns:D=\"DAV:\"><D:allprop /></D:propfind>";

            var requestHeaders = new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>(WebDavConstants.Depth, WebDavDepthHeaderValue.Zero.ToString())
            };

            var responseContent = "<?xml version=\"1.0\" encoding=\"utf-8\"?><D:multistatus xmlns:D=\"DAV:\"><D:response><D:href>http://127.0.0.1/webdav</D:href><D:propstat><D:status>HTTP/1.1 200 OK</D:status><D:prop><D:getcontenttype/><D:getlastmodified>Sat, 08 Apr 2017 10:07:38 GMT</D:getlastmodified><D:lockdiscovery/><D:ishidden>0</D:ishidden><D:supportedlock><D:lockentry><D:lockscope><D:exclusive/></D:lockscope><D:locktype><D:write/></D:locktype></D:lockentry><D:lockentry><D:lockscope><D:shared/></D:lockscope><D:locktype><D:write/></D:locktype></D:lockentry></D:supportedlock><D:getetag/><D:displayname>/</D:displayname><D:getcontentlanguage/><D:getcontentlength>0</D:getcontentlength><D:iscollection>1</D:iscollection><D:creationdate>2017-04-06T09:32:20.983Z</D:creationdate><D:resourcetype><D:collection/></D:resourcetype></D:prop></D:propstat></D:response></D:multistatus>";

            mockHandler.When(WebDavMethod.PropFind, WebDavRootFolder).WithContent(requestContent).WithHeaders(requestHeaders).Respond(HttpStatusCode.OK, new StringContent(responseContent));

            using (var client = CreateWebDavClient(mockHandler))
            {
                PropFind pf       = PropFind.CreatePropFindAllProp();
                var      response = await client.PropFindAsync(WebDavRootFolder, WebDavDepthHeaderValue.Zero, pf);

                var multistatus = await WebDavResponseContentParser.ParseMultistatusResponseContentAsync(response.Content);

                Assert.IsTrue(response.IsSuccessStatusCode);
                Assert.IsNotNull(multistatus);
            }
        }
Esempio n. 2
0
        public async Task UT_WebDavClient_PropPatchSet()
        {
            var mockHandler = new MockHttpMessageHandler();

            var testFile = UriHelper.CombineUrl(WebDavRootFolder, TestFile, true);
            var requestContentProppatch  = "<?xml version=\"1.0\" encoding=\"utf-8\"?><D:propertyupdate xmlns:D=\"DAV:\"><D:set><D:prop><D:displayname>TestFileDisplayName</D:displayname></D:prop></D:set></D:propertyupdate>";
            var responseContentProppatch = "<?xml version=\"1.0\" encoding=\"utf-8\"?><D:multistatus xmlns:D=\"DAV:\"><D:response><D:href>http://127.0.0.1/TextFile1.txt</D:href><D:propstat><D:prop><D:displayname/></D:prop><D:status>HTTP/1.1 200 OK</D:status></D:propstat></D:response></D:multistatus>";

            mockHandler.When(WebDavMethod.PropPatch, testFile).WithContent(requestContentProppatch).Respond(HttpStatusCode.OK, new StringContent(responseContentProppatch));

            var propertyUpdate = new PropertyUpdate();
            var set            = new Set();

            var prop = new Prop()
            {
                DisplayName = "TestFileDisplayName"
            };

            set.Prop             = prop;
            propertyUpdate.Items = new object[] { set };

            using (var client = CreateWebDavClient(mockHandler))
            {
                var response = await client.PropPatchAsync(testFile, propertyUpdate);

                var multistatusPropPatch = await WebDavResponseContentParser.ParseMultistatusResponseContentAsync(response.Content);

                Assert.IsNotNull(multistatusPropPatch);
                Assert.IsTrue(response.IsSuccessStatusCode);
            }
        }
Esempio n. 3
0
        public async Task UT_WebDavClient_PropFind_PropName()
        {
            var mockHandler    = new MockHttpMessageHandler();
            var requestContent = "<?xml version=\"1.0\" encoding=\"utf-8\"?><D:propfind xmlns:D=\"DAV:\"><D:propname /></D:propfind>";

            var requestHeaders = new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>(WebDavConstants.Depth, WebDavDepthHeaderValue.Infinity.ToString())
            };

            var responseContent = "<?xml version=\"1.0\" encoding=\"utf-8\"?><D:multistatus xmlns:D=\"DAV:\"><D:response><D:href>http://127.0.0.1/webdav</D:href><D:propstat><D:status>HTTP/1.1 200 OK</D:status><D:prop><D:getcontenttype/><D:getlastmodified/><D:lockdiscovery/><D:ishidden/><D:supportedlock/><D:getetag/><D:displayname/><D:getcontentlanguage/><D:getcontentlength/><D:iscollection/><D:creationdate/><D:resourcetype/></D:prop></D:propstat></D:response></D:multistatus>";

            mockHandler.When(WebDavMethod.PropFind, WebDavRootFolder).WithContent(requestContent).WithHeaders(requestHeaders).Respond(HttpStatusCode.OK, new StringContent(responseContent));

            using (var client = CreateWebDavClient(mockHandler))
            {
                PropFind pf       = PropFind.CreatePropFindWithPropName();
                var      response = await client.PropFindAsync(WebDavRootFolder, WebDavDepthHeaderValue.Infinity, pf);

                var multistatus = await WebDavResponseContentParser.ParseMultistatusResponseContentAsync(response.Content);

                Assert.IsTrue(response.IsSuccessStatusCode);
                Assert.IsNotNull(multistatus);
            }
        }
        public async Task AddLockToRootAndQueryLockDiscoveryTest()
        {
            var lockResponse = await Client.LockAsync(
                "/",
                WebDavTimeoutHeaderValue.CreateInfiniteWebDavTimeout(),
                WebDavDepthHeaderValue.Infinity,
                new LockInfo()
            {
                LockScope = LockScope.CreateExclusiveLockScope(),
                LockType  = LockType.CreateWriteLockType(),
            })
            ;

            lockResponse.EnsureSuccessStatusCode();
            var propFindResponse = await Client.PropFindAsync(
                "/",
                WebDavDepthHeaderValue.Zero,
                new PropFind()
            {
                Item = new Prop()
                {
                    LockDiscovery = new LockDiscovery(),
                },
            });

            Assert.Equal(WebDavStatusCode.MultiStatus, propFindResponse.StatusCode);
            var multiStatus = await WebDavResponseContentParser.ParseMultistatusResponseContentAsync(propFindResponse.Content);

            Assert.Collection(
                multiStatus.Response,
                response =>
            {
                Assert.Equal("/", response.Href);
                Assert.Collection(
                    response.ItemsElementName,
                    n => Assert.Equal(ItemsChoiceType.propstat, n));
                Assert.Collection(
                    response.Items,
                    item =>
                {
                    var propStat = Assert.IsType <Propstat>(item);
                    var status   = WebDav.Server.Model.Status.Parse(propStat.Status);
                    Assert.Equal(200, status.StatusCode);
                    Assert.NotNull(propStat.Prop?.LockDiscovery?.ActiveLock);
                    Assert.Collection(
                        propStat.Prop.LockDiscovery.ActiveLock,
                        activeLock =>
                    {
                        Assert.Equal("/", activeLock.LockRoot.Href);
                        Assert.Equal(WebDavDepthHeaderValue.Infinity.ToString(), activeLock.Depth, StringComparer.OrdinalIgnoreCase);
                        Assert.IsType <Exclusive>(activeLock.LockScope.Item);
                        Assert.Null(activeLock.OwnerRaw);
                        Assert.Equal(WebDavTimeoutHeaderValue.CreateInfiniteWebDavTimeout().ToString(), activeLock.Timeout, StringComparer.OrdinalIgnoreCase);
                        Assert.NotNull(activeLock.LockToken?.Href);
                        Assert.True(Uri.IsWellFormedUriString(activeLock.LockToken.Href, UriKind.RelativeOrAbsolute));
                    });
                });
            });
        }
        public void UIT_WebDavClient_Copy()
        {
            using (var client = CreateWebDavClientWithDebugHttpMessageHandler())
            {
                var testCollectionSource      = UriHelper.CombineUrl(webDavRootFolder, TestCollection, true);
                var testCollectionDestination = UriHelper.CombineUrl(webDavRootFolder, TestCollection + "2", true);
                var testFile = UriHelper.CombineUrl(testCollectionSource, TestFile, true);

                // Create source collection.
                var response             = client.MkcolAsync(testCollectionSource).Result;
                var mkColResponseSuccess = response.IsSuccessStatusCode;

                // Put file.
                using (var fileStream = File.OpenRead(TestFile))
                {
                    var content = new StreamContent(fileStream);
                    response = client.PutAsync(testFile, content).Result;
                }

                var putResponseSuccess = response.IsSuccessStatusCode;

                // Copy.
                response = client.CopyAsync(testCollectionSource, testCollectionDestination).Result;
                var copyResponseSuccess = response.IsSuccessStatusCode;

                // PropFind.
                PropFind pf = PropFind.CreatePropFindAllProp();
                response = client.PropFindAsync(testCollectionDestination, WebDavDepthHeaderValue.Infinity, pf).Result;
                var propFindResponseSuccess = response.IsSuccessStatusCode;

                var multistatus = (Multistatus)WebDavResponseContentParser.ParseMultistatusResponseContentAsync(response.Content).Result;

                bool collectionfound = false;

                foreach (var item in multistatus.Response)
                {
                    if (item.Href.EndsWith(TestFile))
                    {
                        collectionfound = true;
                        break;
                    }
                }

                // Delete source and destination.
                response = client.DeleteAsync(testCollectionSource).Result;
                var deleteSourceResponseSuccess = response.IsSuccessStatusCode;

                response = client.DeleteAsync(testCollectionDestination).Result;
                var deleteDestinationResponseSuccess = response.IsSuccessStatusCode;

                Assert.IsTrue(mkColResponseSuccess);
                Assert.IsTrue(putResponseSuccess);
                Assert.IsTrue(copyResponseSuccess);
                Assert.IsTrue(propFindResponseSuccess);
                Assert.IsTrue(collectionfound);
                Assert.IsTrue(deleteSourceResponseSuccess);
                Assert.IsTrue(deleteDestinationResponseSuccess);
            }
        }
Esempio n. 6
0
        public async Task UT_WebDavContentParser_ParseMultistatusResponseContentAsync()
        {
            var str         = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><D:multistatus xmlns:D=\"DAV:\"><D:response><D:href>/container/</D:href><D:propstat><D:prop xmlns:R=\"http://ns.example.com/boxschema/\"><R:bigbox><R:BoxType>Box type A</R:BoxType></R:bigbox><R:author><R:Name>Hadrian</R:Name></R:author><D:creationdate>1997-12-01T17:42:21-08:00</D:creationdate><D:displayname>Example collection</D:displayname><D:resourcetype><D:collection/></D:resourcetype><D:supportedlock><D:lockentry><D:lockscope><D:exclusive/></D:lockscope><D:locktype><D:write/></D:locktype></D:lockentry><D:lockentry><D:lockscope><D:shared/></D:lockscope><D:locktype><D:write/></D:locktype></D:lockentry></D:supportedlock></D:prop><D:status>HTTP/1.1 200 OK</D:status></D:propstat></D:response><D:response><D:href>/container/front.html</D:href><D:propstat><D:prop xmlns:R=\"http://ns.example.com/boxschema/\"><R:bigbox><R:BoxType>Box type B</R:BoxType></R:bigbox><D:creationdate>1997-12-01T18:27:21-08:00</D:creationdate><D:displayname>Example HTML resource</D:displayname><D:getcontentlength>4525</D:getcontentlength><D:getcontenttype>text/html</D:getcontenttype><D:getetag>\"zzyzx\"</D:getetag><D:getlastmodified>Mon, 12 Jan 1998 09:25:56 GMT</D:getlastmodified><D:resourcetype/><D:supportedlock><D:lockentry><D:lockscope><D:exclusive/></D:lockscope><D:locktype><D:write/></D:locktype></D:lockentry><D:lockentry><D:lockscope><D:shared/></D:lockscope><D:locktype><D:write/></D:locktype></D:lockentry></D:supportedlock></D:prop><D:status>HTTP/1.1 200 OK</D:status></D:propstat></D:response></D:multistatus>";
            var content     = new StringContent(str);
            var multistatus = await WebDavResponseContentParser.ParseMultistatusResponseContentAsync(content);

            Assert.IsNotNull(multistatus);
        }
Esempio n. 7
0
        public void UIT_WebDavClient_Move()
        {
            var client = CreateWebDavClientWithDebugHttpMessageHandler();
            var testCollectionSource      = UriHelper.CombineUrl(this.webDavRootFolder, TestCollection, true);
            var testCollectionDestination = UriHelper.CombineUrl(this.webDavRootFolder, TestCollection + "2", true);
            var testFile = UriHelper.CombineUrl(testCollectionSource, TestFile, true);

            // Create source collection.
            var response             = client.MkcolAsync(testCollectionSource).Result;
            var mkColResponseSuccess = response.IsSuccessStatusCode;

            // Put file.
            var content = new StreamContent(File.OpenRead(TestFile));

            response = client.PutAsync(testFile, content).Result;
            var putResponseSuccess = response.IsSuccessStatusCode;

            // Move.
            response = client.MoveAsync(testCollectionSource, testCollectionDestination).Result;
            var moveResponseSuccess = response.IsSuccessStatusCode;

            // PropFind.
            PropFind pf = PropFind.CreatePropFindAllProp();

            response = client.PropFindAsync(this.webDavRootFolder, WebDavDepthHeaderValue.Infinity, pf).Result;
            var propFindResponseSuccess = response.IsSuccessStatusCode;

            var multistatus = (Multistatus)WebDavResponseContentParser.ParseMultistatusResponseContentAsync(response.Content).Result;

            bool foundCollection1 = false;
            bool foundCollection2 = false;

            foreach (var item in multistatus.Response)
            {
                if (item.Href.EndsWith(TestCollection + "/"))
                {
                    foundCollection1 = true;
                }

                if (item.Href.EndsWith(TestCollection + "2/"))
                {
                    foundCollection2 = true;
                }
            }

            // Delete source and destination.
            // Delete file.
            response = client.DeleteAsync(testCollectionDestination).Result;
            var deleteResponseSuccess = response.IsSuccessStatusCode;

            Assert.IsTrue(mkColResponseSuccess);
            Assert.IsTrue(putResponseSuccess);
            Assert.IsTrue(moveResponseSuccess);
            Assert.IsTrue(propFindResponseSuccess);
            Assert.IsFalse(foundCollection1);
            Assert.IsTrue(foundCollection2);
            Assert.IsTrue(deleteResponseSuccess);
        }
Esempio n. 8
0
        public void UIT_WebDavClient_PropPatch()
        {
            var client   = CreateWebDavClientWithDebugHttpMessageHandler();
            var testFile = UriHelper.CombineUrl(this.webDavRootFolder, TestFile, true);

            // Put file.
            var content            = new StreamContent(File.OpenRead(TestFile));
            var response           = client.PutAsync(testFile, content).Result;
            var putResponseSuccess = response.IsSuccessStatusCode;

            // PropPatch (set).
            var propertyUpdate = new PropertyUpdate();
            var set            = new Set();
            var prop           = new Prop();

            prop.DisplayName     = "TestFileDisplayName";
            set.Prop             = prop;
            propertyUpdate.Items = new object[] { set };
            response             = client.PropPatchAsync(testFile, propertyUpdate).Result;
            var propPatchResponseSuccess = response.IsSuccessStatusCode;

            // PropFind.
            PropFind pf = PropFind.CreatePropFindWithEmptyProperties("displayname");

            response = client.PropFindAsync(testFile, WebDavDepthHeaderValue.Zero, pf).Result;
            var propFindResponseSuccess = response.IsSuccessStatusCode;
            var multistatus             = (Multistatus)WebDavResponseContentParser.ParseMultistatusResponseContentAsync(response.Content).Result;
            var displayName             = ((Propstat)multistatus.Response[0].Items[0]).Prop.DisplayName;
            // IIS ignores display name and always puts the file name as display name.
            var displayNameResult = "TestFileDisplayName" == displayName || TestFile == displayName;

            // PropPatch (remove).
            propertyUpdate = new PropertyUpdate();
            var remove = new Remove();

            prop                 = Prop.CreatePropWithEmptyProperties("displayname");
            remove.Prop          = prop;
            propertyUpdate.Items = new object[] { remove };
            response             = client.PropPatchAsync(testFile, propertyUpdate).Result;
            var propPatchRemoveResponseSuccess = response.IsSuccessStatusCode;

            multistatus = (Multistatus)WebDavResponseContentParser.ParseMultistatusResponseContentAsync(response.Content).Result;
            var multistatusResult = ((Propstat)multistatus.Response[0].Items[0]).Prop.DisplayName;

            // Delete file.
            response = client.DeleteAsync(testFile).Result;
            var deleteResponseSuccess = response.IsSuccessStatusCode;

            Assert.IsTrue(putResponseSuccess);
            Assert.IsTrue(propPatchResponseSuccess);
            Assert.IsTrue(propFindResponseSuccess);
            Assert.IsTrue(displayNameResult);
            Assert.IsTrue(propPatchRemoveResponseSuccess);
            Assert.AreEqual(string.Empty, multistatusResult);
            Assert.IsTrue(deleteResponseSuccess);
        }
Esempio n. 9
0
        public void UIT_WebDavClient_PropFind_PropName()
        {
            var      client   = CreateWebDavClientWithDebugHttpMessageHandler();
            PropFind pf       = PropFind.CreatePropFindWithPropName();
            var      response = client.PropFindAsync(this.webDavRootFolder, WebDavDepthHeaderValue.Infinity, pf).Result;
            var      propFindResponseSuccess = response.IsSuccessStatusCode;
            var      multistatus             = WebDavResponseContentParser.ParseMultistatusResponseContentAsync(response.Content).Result;

            Assert.IsTrue(propFindResponseSuccess);
            Assert.IsNotNull(multistatus);
        }
        public void UT_WebDavContentParser_ParseMultistatusResponseContentAsyncWithPropWithInvalidXmlLangAttribute()
        {
            var str         = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><D:multistatus xmlns:D=\"DAV:\"><D:response><D:href>/container/</D:href><D:propstat><D:prop xml:lang=\"invalid\" xmlns:R=\"http://ns.example.com/boxschema/\"><R:bigbox><R:BoxType>Box type A</R:BoxType></R:bigbox><R:author><R:Name>Hadrian</R:Name></R:author><D:creationdate>1997-12-01T17:42:21-08:00</D:creationdate><D:displayname>Example collection</D:displayname><D:resourcetype><D:collection/></D:resourcetype><D:supportedlock><D:lockentry><D:lockscope><D:exclusive/></D:lockscope><D:locktype><D:write/></D:locktype></D:lockentry><D:lockentry><D:lockscope><D:shared/></D:lockscope><D:locktype><D:write/></D:locktype></D:lockentry></D:supportedlock></D:prop><D:status>HTTP/1.1 200 OK</D:status></D:propstat></D:response><D:response><D:href>/container/front.html</D:href><D:propstat><D:prop xmlns:R=\"http://ns.example.com/boxschema/\"><R:bigbox><R:BoxType>Box type B</R:BoxType></R:bigbox><D:creationdate>1997-12-01T18:27:21-08:00</D:creationdate><D:displayname>Example HTML resource</D:displayname><D:getcontentlength>4525</D:getcontentlength><D:getcontenttype>text/html</D:getcontenttype><D:getetag>\"zzyzx\"</D:getetag><D:getlastmodified>Mon, 12 Jan 1998 09:25:56 GMT</D:getlastmodified><D:resourcetype/><D:supportedlock><D:lockentry><D:lockscope><D:exclusive/></D:lockscope><D:locktype><D:write/></D:locktype></D:lockentry><D:lockentry><D:lockscope><D:shared/></D:lockscope><D:locktype><D:write/></D:locktype></D:lockentry></D:supportedlock></D:prop><D:status>HTTP/1.1 200 OK</D:status></D:propstat></D:response></D:multistatus>";
            var content     = new StringContent(str);
            var multistatus = WebDavResponseContentParser.ParseMultistatusResponseContentAsync(content).Result;

            Assert.IsNotNull(multistatus);
            var language = ((Propstat)multistatus.Response[0].Items[0]).Prop.Language;
            var expected = "invalid";

            Assert.AreEqual(expected, language);
        }
Esempio n. 11
0
        public async Task <bool> LoadQuotaAsync()
        {
            quota = new QuotaLimiter();

            Progress_Start();

            try
            {
                // ||=========||====\\
                // ||*костыль*||     ||========{|
                // ||=========||====//
                using (WebDavClient client = new WebDavClient(
                           new HttpClientHandler()
                {
                    PreAuthenticate = true,
                    // Учетные данные пользователя.
                    Credentials = new NetworkCredential(User, Pass)
                },
                           version))
                {
                    var response = await client.PropFindAsync(Server);

                    var multistatus = await WebDavResponseContentParser.ParseMultistatusResponseContentAsync(response.Content);

                    foreach (var responseItem in multistatus.Response)
                    {
                        if (responseItem.Href == "/remote.php/webdav/")
                        {
                            foreach (var item in responseItem.Items)
                            {
                                var propStat = item as Propstat;
                                var prop     = propStat.Prop;
                                quota.QuotaAvailableBytes = prop.QuotaAvailableBytes;
                                quota.QuotaUsedBytes      = prop.QuotaUsedBytes;
                            }
                        }
                    }

                    //LogController.Info(logger, $"Получена квота");
                    Show_Quota(quota);
                }
            }
            catch (Exception e)
            {
                LogController.Error(logger, e, $"Ошибка, не удалось получить квоту");
                return(false);
            }

            Progress_Stop();

            return(true);
        }
        public void UIT_WebDavClient_PropFind_AllPropDepthZero()
        {
            using (var client = CreateWebDavClientWithDebugHttpMessageHandler())
            {
                PropFind pf       = PropFind.CreatePropFindAllProp();
                var      response = client.PropFindAsync(webDavRootFolder, WebDavDepthHeaderValue.Zero, pf).Result;
                var      propFindResponseSuccess = response.IsSuccessStatusCode;
                var      multistatus             = WebDavResponseContentParser.ParseMultistatusResponseContentAsync(response.Content).Result;

                Assert.IsTrue(propFindResponseSuccess);
                Assert.IsNotNull(multistatus);
            }
        }
        public async Task UIT_WebDavClient_PropName()
        {
            using (var client = CreateWebDavClientWithDebugHttpMessageHandler())
            {
                PropFind pf       = PropFind.CreatePropFindWithPropName();
                var      response = await client.PropFindAsync(webDavRootFolder, WebDavDepthHeaderValue.Infinity, pf);

                var propFindResponseSuccess = response.IsSuccessStatusCode;
                var multistatus             = await WebDavResponseContentParser.ParseMultistatusResponseContentAsync(response.Content);

                Assert.IsTrue(propFindResponseSuccess);
                Assert.IsNotNull(multistatus);
            }
        }
        public void UT_WebDavContentParser_ParsePropResponseContentAsyncWithMixedAndAttibuteOwner()
        {
            var str               = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><D:multistatus xmlns:D='DAV:'><D:response><D:href>http://www.example.com/container/</D:href><D:propstat><D:prop><D:lockdiscovery><D:activelock><D:locktype><D:write/></D:locktype><D:lockscope><D:exclusive/></D:lockscope><D:depth>0</D:depth><D:owner xmlns:x='urn:other-schema-provider:other-schema' x:myattr='test-attr'>Test<x:whatever>test</x:whatever></D:owner><D:timeout>Infinite</D:timeout><D:locktoken><D:href>urn:uuid:f81de2ad-7f3d-a1b2-4f3c-00a0c91a9d76</D:href></D:locktoken><D:lockroot><D:href>http://www.example.com/container/</D:href></D:lockroot></D:activelock></D:lockdiscovery></D:prop><D:status>HTTP/1.1 200 OK</D:status></D:propstat></D:response></D:multistatus>";
            var content           = new StringContent(str);
            var multistatus       = WebDavResponseContentParser.ParseMultistatusResponseContentAsync(content).Result;
            var ownerRaw          = ((Propstat)multistatus.Response[0].Items[0]).Prop.LockDiscovery.ActiveLock[0].OwnerRaw;
            var ownerHref         = ((Propstat)multistatus.Response[0].Items[0]).Prop.LockDiscovery.ActiveLock[0].OwnerHref;
            var ownerString       = ownerRaw.ToString(SaveOptions.DisableFormatting);
            var expected          = "<owner xmlns:x=\"urn:other-schema-provider:other-schema\" x:myattr=\"test-attr\" xmlns=\"DAV:\">Test<x:whatever>test</x:whatever></owner>";
            var expectedOwnerHref = string.Empty;

            Assert.IsNotNull(multistatus);
            Assert.AreEqual(expected, ownerString);
            Assert.AreEqual(expectedOwnerHref, ownerHref);
        }
        public void UT_WebDavContentParser_ParsePropResponseContentAsyncWithHrefAndAttributeOwner()
        {
            var str               = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><D:multistatus xmlns:D='DAV:'><D:response><D:href>http://www.example.com/container/</D:href><D:propstat><D:prop><D:lockdiscovery><D:activelock><D:locktype><D:write/></D:locktype><D:lockscope><D:exclusive/></D:lockscope><D:depth>0</D:depth><D:owner><D:href>http://example.org/~ejw/contact.html</D:href><x:author xmlns:x='http://example.com/ns'><x:name>Jane Doe</x:name></x:author></D:owner><D:timeout>Infinite</D:timeout><D:locktoken><D:href>urn:uuid:f81de2ad-7f3d-a1b2-4f3c-00a0c91a9d76</D:href></D:locktoken><D:lockroot><D:href>http://www.example.com/container/</D:href></D:lockroot></D:activelock></D:lockdiscovery></D:prop><D:status>HTTP/1.1 200 OK</D:status></D:propstat></D:response></D:multistatus>";
            var content           = new StringContent(str);
            var multistatus       = WebDavResponseContentParser.ParseMultistatusResponseContentAsync(content).Result;
            var ownerRaw          = ((Propstat)multistatus.Response[0].Items[0]).Prop.LockDiscovery.ActiveLock[0].OwnerRaw;
            var ownerHref         = ((Propstat)multistatus.Response[0].Items[0]).Prop.LockDiscovery.ActiveLock[0].OwnerHref;
            var ownerString       = ownerRaw.ToString(SaveOptions.DisableFormatting);
            var expectedOwnerRaw  = "<owner xmlns=\"DAV:\"><href>http://example.org/~ejw/contact.html</href><x:author xmlns:x=\"http://example.com/ns\"><x:name>Jane Doe</x:name></x:author></owner>";
            var expectedOwnerHref = @"http://example.org/~ejw/contact.html";

            Assert.IsNotNull(multistatus);
            Assert.AreEqual(expectedOwnerRaw, ownerString);
            Assert.AreEqual(expectedOwnerHref, ownerHref);
        }
        public async Task UIT_WebDavClient_Mkcol()
        {
            using (var client = CreateWebDavClientWithDebugHttpMessageHandler())
            {
                var testCollection = UriHelper.CombineUrl(webDavRootFolder, TestCollection, true);

                // Create collection.
                var response = await client.MkcolAsync(testCollection);

                var mkColResponseSuccess = response.IsSuccessStatusCode;

                // PropFind.
                PropFind pf = PropFind.CreatePropFindAllProp();
                response = await client.PropFindAsync(webDavRootFolder, WebDavDepthHeaderValue.Infinity, pf);

                var propFindResponseSuccess = response.IsSuccessStatusCode;

                var multistatus = await WebDavResponseContentParser.ParseMultistatusResponseContentAsync(response.Content);

                bool collectionFound = false;

                foreach (var item in multistatus.Response)
                {
                    if (item.Href.EndsWith(TestCollection + "/"))
                    {
                        collectionFound = true;
                        break;
                    }
                }

                // Delete collection.
                response = await client.DeleteAsync(testCollection);

                var deleteResponseSuccess = response.IsSuccessStatusCode;

                Assert.IsTrue(mkColResponseSuccess);
                Assert.IsTrue(propFindResponseSuccess);
                Assert.IsTrue(collectionFound);
                Assert.IsTrue(deleteResponseSuccess);
            }
        }
Esempio n. 17
0
        public async Task UT_WebDavClient_PropFind_AllPropWithXmlContentString()
        {
            var mockHandler    = new MockHttpMessageHandler();
            var requestContent = "<?xml version=\"1.0\" encoding=\"utf-8\"?><D:propfind xmlns:D=\"DAV:\"><D:allprop /></D:propfind>";

            var requestHeaders = new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>(WebDavConstants.Depth, WebDavDepthHeaderValue.Infinity.ToString())
            };

            var responseContent = "<?xml version=\"1.0\" encoding=\"utf-8\"?><D:multistatus xmlns:D=\"DAV:\"><D:response><D:href>http://127.0.0.1/webdav</D:href><D:propstat><D:status>HTTP/1.1 200 OK</D:status><D:prop><D:getcontenttype/><D:getlastmodified>Sat, 08 Apr 2017 10:07:38 GMT</D:getlastmodified><D:lockdiscovery/><D:ishidden>0</D:ishidden><D:supportedlock><D:lockentry><D:lockscope><D:exclusive/></D:lockscope><D:locktype><D:write/></D:locktype></D:lockentry><D:lockentry><D:lockscope><D:shared/></D:lockscope><D:locktype><D:write/></D:locktype></D:lockentry></D:supportedlock><D:getetag/><D:displayname>/</D:displayname><D:getcontentlanguage/><D:getcontentlength>0</D:getcontentlength><D:iscollection>1</D:iscollection><D:creationdate>2017-04-06T09:32:20.983Z</D:creationdate><D:resourcetype><D:collection/></D:resourcetype></D:prop></D:propstat></D:response><D:response><D:href>http://127.0.0.1/webdav/test1/</D:href><D:propstat><D:status>HTTP/1.1 200 OK</D:status><D:prop><D:getcontenttype/><D:getlastmodified>Sat, 08 Apr 2017 10:07:54 GMT</D:getlastmodified><D:lockdiscovery/><D:ishidden>0</D:ishidden><D:supportedlock><D:lockentry><D:lockscope><D:exclusive/></D:lockscope><D:locktype><D:write/></D:locktype></D:lockentry><D:lockentry><D:lockscope><D:shared/></D:lockscope><D:locktype><D:write/></D:locktype></D:lockentry></D:supportedlock><D:getetag/><D:displayname>test1</D:displayname><D:getcontentlanguage/><D:getcontentlength>0</D:getcontentlength><D:iscollection>1</D:iscollection><D:creationdate>2017-04-08T10:07:32.205Z</D:creationdate><D:resourcetype><D:collection/></D:resourcetype></D:prop></D:propstat></D:response><D:response><D:href>http://127.0.0.1/webdav/test2/</D:href><D:propstat><D:status>HTTP/1.1 200 OK</D:status><D:prop><D:getcontenttype/><D:getlastmodified>Sat, 08 Apr 2017 10:07:35 GMT</D:getlastmodified><D:lockdiscovery/><D:ishidden>0</D:ishidden><D:supportedlock><D:lockentry><D:lockscope><D:exclusive/></D:lockscope><D:locktype><D:write/></D:locktype></D:lockentry><D:lockentry><D:lockscope><D:shared/></D:lockscope><D:locktype><D:write/></D:locktype></D:lockentry></D:supportedlock><D:getetag/><D:displayname>test2</D:displayname><D:getcontentlanguage/><D:getcontentlength>0</D:getcontentlength><D:iscollection>1</D:iscollection><D:creationdate>2017-04-08T10:07:35.866Z</D:creationdate><D:resourcetype><D:collection/></D:resourcetype></D:prop></D:propstat></D:response><D:response><D:href>http://127.0.0.1/webdav/test1/file1.txt</D:href><D:propstat><D:status>HTTP/1.1 200 OK</D:status><D:prop><D:getcontenttype>text/plain</D:getcontenttype><D:getlastmodified>Sat, 08 Apr 2017 10:08:15 GMT</D:getlastmodified><D:lockdiscovery/><D:ishidden>0</D:ishidden><D:supportedlock><D:lockentry><D:lockscope><D:exclusive/></D:lockscope><D:locktype><D:write/></D:locktype></D:lockentry><D:lockentry><D:lockscope><D:shared/></D:lockscope><D:locktype><D:write/></D:locktype></D:lockentry></D:supportedlock><D:getetag>\"4840af950b0d21:0\"</D:getetag><D:displayname>file1.txt</D:displayname><D:getcontentlanguage/><D:getcontentlength>6</D:getcontentlength><D:iscollection>0</D:iscollection><D:creationdate>2017-04-08T10:07:48.579Z</D:creationdate><D:resourcetype/></D:prop></D:propstat></D:response><D:response><D:href>http://127.0.0.1/webdav/test1/test1_1/</D:href><D:propstat><D:status>HTTP/1.1 200 OK</D:status><D:prop><D:getcontenttype/><D:getlastmodified>Sat, 08 Apr 2017 10:08:00 GMT</D:getlastmodified><D:lockdiscovery/><D:ishidden>0</D:ishidden><D:supportedlock><D:lockentry><D:lockscope><D:exclusive/></D:lockscope><D:locktype><D:write/></D:locktype></D:lockentry><D:lockentry><D:lockscope><D:shared/></D:lockscope><D:locktype><D:write/></D:locktype></D:lockentry></D:supportedlock><D:getetag/><D:displayname>test1_1</D:displayname><D:getcontentlanguage/><D:getcontentlength>0</D:getcontentlength><D:iscollection>1</D:iscollection><D:creationdate>2017-04-08T10:07:42.302Z</D:creationdate><D:resourcetype><D:collection/></D:resourcetype></D:prop></D:propstat></D:response><D:response><D:href>http://127.0.0.1/webdav/test1/test1_1/file2.txt</D:href><D:propstat><D:status>HTTP/1.1 200 OK</D:status><D:prop><D:getcontenttype>text/plain</D:getcontenttype><D:getlastmodified>Sat, 08 Apr 2017 10:08:09 GMT</D:getlastmodified><D:lockdiscovery/><D:ishidden>0</D:ishidden><D:supportedlock><D:lockentry><D:lockscope><D:exclusive/></D:lockscope><D:locktype><D:write/></D:locktype></D:lockentry><D:lockentry><D:lockscope><D:shared/></D:lockscope><D:locktype><D:write/></D:locktype></D:lockentry></D:supportedlock><D:getetag>\"25f646650b0d21:0\"</D:getetag><D:displayname>file2.txt</D:displayname><D:getcontentlanguage/><D:getcontentlength>6</D:getcontentlength><D:iscollection>0</D:iscollection><D:creationdate>2017-04-08T10:07:58.137Z</D:creationdate><D:resourcetype/></D:prop></D:propstat></D:response></D:multistatus>";

            mockHandler.When(WebDavMethod.PropFind, WebDavRootFolder).WithContent(requestContent).WithHeaders(requestHeaders).Respond(HttpStatusCode.OK, new StringContent(responseContent));

            using (var client = CreateWebDavClient(mockHandler))
            {
                var response = await client.PropFindAsync(WebDavRootFolder, WebDavDepthHeaderValue.Infinity, requestContent);

                var multistatus = await WebDavResponseContentParser.ParseMultistatusResponseContentAsync(response.Content);

                Assert.IsTrue(response.IsSuccessStatusCode);
                Assert.IsNotNull(multistatus);
            }
        }
        public async Task UIT_WebDavClient_Move_Rename()
        {
            using (var client = CreateWebDavClientWithDebugHttpMessageHandler())
            {
                var testCollectionSource = UriHelper.CombineUrl(webDavRootFolder, TestCollection, true);
                var testFileToRename     = UriHelper.CombineUrl(testCollectionSource, TestFile, true);
                var testFileRenamed      = UriHelper.CombineUrl(testCollectionSource, "RenamedFile", true);

                // Create source collection.
                var response = await client.MkcolAsync(testCollectionSource);

                var mkColResponseSuccess = response.IsSuccessStatusCode;

                // Put file.
                using (var fileStream = File.OpenRead(TestFile))
                {
                    var content = new StreamContent(fileStream);
                    response = await client.PutAsync(testFileToRename, content);
                }

                var putResponseSuccess = response.IsSuccessStatusCode;

                // Move.
                response = await client.MoveAsync(testFileToRename, testFileRenamed);

                var moveResponseSuccess = response.IsSuccessStatusCode;

                // PropFind.
                PropFind pf = PropFind.CreatePropFindAllProp();
                response = await client.PropFindAsync(webDavRootFolder, WebDavDepthHeaderValue.Infinity, pf);

                var propFindResponseSuccess = response.IsSuccessStatusCode;

                var multistatus = await WebDavResponseContentParser.ParseMultistatusResponseContentAsync(response.Content);

                bool foundRenamedFile = false;
                bool foundFile        = false;

                foreach (var item in multistatus.Response)
                {
                    if (item.Href.EndsWith("RenamedFile"))
                    {
                        foundRenamedFile = true;
                    }

                    if (item.Href.EndsWith(TestFile))
                    {
                        foundFile = true;
                    }
                }

                // Delete source and destination.
                // Delete file.
                response = await client.DeleteAsync(testCollectionSource);

                var deleteResponseSuccess = response.IsSuccessStatusCode;

                Assert.IsTrue(mkColResponseSuccess);
                Assert.IsTrue(putResponseSuccess);
                Assert.IsTrue(moveResponseSuccess);
                Assert.IsTrue(propFindResponseSuccess);
                Assert.IsTrue(foundRenamedFile);
                Assert.IsFalse(foundFile);
                Assert.IsTrue(deleteResponseSuccess);
            }
        }
        public async Task UIT_WebDavClient_PropPatch()
        {
            using (var client = CreateWebDavClientWithDebugHttpMessageHandler())
            {
                var testFile           = UriHelper.CombineUrl(webDavRootFolder, TestFile, true);
                var putResponseSuccess = false;

                // Put file.
                using (var fileStream = File.OpenRead(TestFile))
                {
                    var content     = new StreamContent(fileStream);
                    var responsePut = await client.PutAsync(testFile, content);

                    putResponseSuccess = responsePut.IsSuccessStatusCode;
                }

                // PropPatch (set).
                var propertyUpdate = new PropertyUpdate();
                var set            = new Set();

                var prop = new Prop()
                {
                    DisplayName = "TestFileDisplayName"
                };

                set.Prop             = prop;
                propertyUpdate.Items = new object[] { set };
                var response = await client.PropPatchAsync(testFile, propertyUpdate);

                var multistatusPropPatchSet = await WebDavResponseContentParser.ParseMultistatusResponseContentAsync(response.Content);

                var propPatchResponseSuccess = response.IsSuccessStatusCode;

                // PropFind.
                PropFind pf = PropFind.CreatePropFindWithEmptyProperties(PropNameConstants.DisplayName);
                response = await client.PropFindAsync(testFile, WebDavDepthHeaderValue.Zero, pf);

                var propFindResponseSuccess = response.IsSuccessStatusCode;
                var multistatusPropFind     = await WebDavResponseContentParser.ParseMultistatusResponseContentAsync(response.Content);

                var displayName = ((Propstat)multistatusPropFind.Response[0].Items[0]).Prop.DisplayName;
                // IIS ignores display name and always puts the file name as display name.
                var displayNameResult = "TestFileDisplayName" == displayName || TestFile == displayName;

                // PropPatch (remove).
                propertyUpdate = new PropertyUpdate();
                var remove = new Remove();
                prop                 = Prop.CreatePropWithEmptyProperties(PropNameConstants.DisplayName);
                remove.Prop          = prop;
                propertyUpdate.Items = new object[] { remove };
                response             = await client.PropPatchAsync(testFile, propertyUpdate);

                var propPatchRemoveResponseSuccess = response.IsSuccessStatusCode;
                multistatusPropFind = await WebDavResponseContentParser.ParseMultistatusResponseContentAsync(response.Content);

                var multistatusPropFindResult = string.IsNullOrEmpty(((Propstat)multistatusPropFind.Response[0].Items[0]).Prop.DisplayName);

                // Delete file.
                response = await client.DeleteAsync(testFile);

                var deleteResponseSuccess = response.IsSuccessStatusCode;

                Assert.IsTrue(putResponseSuccess);
                Assert.IsNotNull(multistatusPropPatchSet);
                Assert.IsTrue(propPatchResponseSuccess);
                Assert.IsTrue(propFindResponseSuccess);
                Assert.IsTrue(displayNameResult);
                Assert.IsTrue(propPatchRemoveResponseSuccess);
                Assert.IsTrue(multistatusPropFindResult);
                Assert.IsTrue(deleteResponseSuccess);
            }
        }
Esempio n. 20
0
        /// <summary>
        /// Extracts the property names (known and unknown) from a <see cref="HttpContent"/>.
        /// </summary>
        /// <param name="content">The <see cref="HttpContent"/> containing the <see cref="Multistatus"/> as XML.</param>
        /// <returns>The <see cref="Task"/>t representing the asynchronous operation.</returns>
        public static async Task <string[]> GetPropertyNamesKnownAndUnknownFromMultiStatusContentAsync(HttpContent content)
        {
            if (content == null)
            {
                return(null);
            }

            try
            {
                var propertyList = new HashSet <string>();
                var multistatus  = await WebDavResponseContentParser.ParseMultistatusResponseContentAsync(content);

                foreach (var response in multistatus.Response)
                {
                    foreach (var item in response.Items)
                    {
                        var propStat = item as Propstat;

                        if (propStat == null)
                        {
                            continue;
                        }

                        var prop = propStat.Prop;

                        // Add known properties.
                        if (prop.ChildCountString != null)
                        {
                            propertyList.Add(PropNameConstants.ChildCount);
                        }

                        if (prop.ContentClass != null)
                        {
                            propertyList.Add(PropNameConstants.ContentClass);
                        }

                        if (prop.CreationDateString != null)
                        {
                            propertyList.Add(PropNameConstants.CreationDate);
                        }

                        if (prop.DefaultDocument != null)
                        {
                            propertyList.Add(PropNameConstants.DefaultDocument);
                        }

                        if (prop.DisplayName != null)
                        {
                            propertyList.Add(PropNameConstants.DisplayName);
                        }

                        if (prop.GetContentLanguage != null)
                        {
                            propertyList.Add(PropNameConstants.GetContentLanguage);
                        }

                        if (prop.GetContentLengthString != null)
                        {
                            propertyList.Add(PropNameConstants.GetContentLength);
                        }

                        if (prop.GetContentType != null)
                        {
                            propertyList.Add(PropNameConstants.GetContentType);
                        }

                        if (prop.GetEtag != null)
                        {
                            propertyList.Add(PropNameConstants.GetEtag);
                        }

                        if (prop.GetLastModifiedString != null)
                        {
                            propertyList.Add(PropNameConstants.GetLastModified);
                        }

                        if (prop.HasSubsString != null)
                        {
                            propertyList.Add(PropNameConstants.HasSubs);
                        }

                        if (prop.HrefString != null)
                        {
                            propertyList.Add(PropNameConstants.Href);
                        }

                        if (prop.Id != null)
                        {
                            propertyList.Add(PropNameConstants.Id);
                        }

                        if (prop.IsFolderString != null)
                        {
                            propertyList.Add(PropNameConstants.IsFolder);
                        }

                        if (prop.IsHiddenString != null)
                        {
                            propertyList.Add(PropNameConstants.IsHidden);
                        }

                        if (prop.IsReadonlyString != null)
                        {
                            propertyList.Add(PropNameConstants.IsReadonly);
                        }

                        if (prop.IsRootString != null)
                        {
                            propertyList.Add(PropNameConstants.IsRoot);
                        }

                        if (prop.IsStructuredDocumentString != null)
                        {
                            propertyList.Add(PropNameConstants.IsStructuredDocument);
                        }

                        if (prop.LastAccessedString != null)
                        {
                            propertyList.Add(PropNameConstants.LastAccessed);
                        }

                        if (prop.LockDiscovery != null)
                        {
                            propertyList.Add(PropNameConstants.LockDiscovery);
                        }

                        if (prop.Name != null)
                        {
                            propertyList.Add(PropNameConstants.Name);
                        }

                        if (prop.NoSubsString != null)
                        {
                            propertyList.Add(PropNameConstants.NoSubs);
                        }

                        if (prop.ObjectCountString != null)
                        {
                            propertyList.Add(PropNameConstants.ObjectCount);
                        }

                        if (prop.ParentName != null)
                        {
                            propertyList.Add(PropNameConstants.ParentName);
                        }

                        if (prop.QuotaAvailableBytesString != null)
                        {
                            propertyList.Add(PropNameConstants.QuotaAvailableBytes);
                        }

                        if (prop.QuotaUsedBytesString != null)
                        {
                            propertyList.Add(PropNameConstants.QuotaUsedBytes);
                        }

                        if (prop.ReservedString != null)
                        {
                            propertyList.Add(PropNameConstants.Reserved);
                        }

                        if (prop.ResourceType != null)
                        {
                            propertyList.Add(PropNameConstants.ResourceType);
                        }

                        if (prop.SupportedLock != null)
                        {
                            propertyList.Add(PropNameConstants.SupportedLock);
                        }

                        if (prop.VisibleCountString != null)
                        {
                            propertyList.Add(PropNameConstants.VisibleCount);
                        }

                        // Add unknown properties.
                        if (prop.AdditionalProperties != null)
                        {
                            foreach (var unknownElement in prop.AdditionalProperties)
                            {
                                propertyList.Add(unknownElement.Name.LocalName);
                            }
                        }
                    }
                }

                return(propertyList.ToArray());
            }
            catch (Exception ex)
            {
                throw new WebDavException("Failed to retrieve property names from the HttpContent.", ex);
            }
        }