Esempio n. 1
0
        public static async Task TestLock(WebDavClient webDavClient)
        {
            var response = await webDavClient.Lock("http://mywebdav:88/1.txt",
                                                   new LockParameters { LockScope = LockScope.Shared, Owner = new PrincipalLockOwner("Chuck Norris"), Timeout = TimeSpan.FromSeconds(120) });

            var token = string.Empty;

            foreach (var @lock in response.ActiveLocks)
            {
                token = @lock.LockToken;
                PrintActiveLock(@lock);
            }
            await webDavClient.Unlock("http://mywebdav:88/1.txt", token);

            Console.WriteLine("Unlocked!");

            var response2 = await webDavClient.Lock("http://mywebdav:88/2.txt");

            var token2         = response2.ActiveLocks.First().LockToken;
            var deleteResponse = await webDavClient.Delete("http://mywebdav:88/2.txt");

            if (!deleteResponse.IsSuccessful)
            {
                Console.WriteLine("Can't delete a resource. It's locked!");
            }

            deleteResponse = await webDavClient.Delete("http://mywebdav:88/2.txt", new DeleteParameters { LockToken = token2 });

            if (deleteResponse.IsSuccessful)
            {
                Console.WriteLine("The resource was deleted.");
            }
        }
Esempio n. 2
0
        public async void When_RequestIsSuccessfull_Should_ReturnStatusCode200()
        {
            var client = new WebDavClient().SetWebDavDispatcher(Dispatcher.Mock());
            var response1 = await client.Lock("http://example.com/new");
            var response2 = await client.Lock(new Uri("http://example.com/new"));
            var response3 = await client.Lock("http://example.com/new", new LockParameters());
            var response4 = await client.Lock(new Uri("http://example.com/new"), new LockParameters());

            Assert.Equal(200, response1.StatusCode);
            Assert.Equal(200, response2.StatusCode);
            Assert.Equal(200, response3.StatusCode);
            Assert.Equal(200, response4.StatusCode);
        }
Esempio n. 3
0
        public async void When_RequestIsSuccessfull_Should_ReturnStatusCode200()
        {
            var client    = new WebDavClient().SetWebDavDispatcher(Dispatcher.Mock());
            var response1 = await client.Lock("http://example.com/new");

            var response2 = await client.Lock(new Uri("http://example.com/new"));

            var response3 = await client.Lock("http://example.com/new", new LockParameters());

            var response4 = await client.Lock(new Uri("http://example.com/new"), new LockParameters());

            Assert.Equal(200, response1.StatusCode);
            Assert.Equal(200, response2.StatusCode);
            Assert.Equal(200, response3.StatusCode);
            Assert.Equal(200, response4.StatusCode);
        }
Esempio n. 4
0
        public async void When_RequestIsFailed_Should_ReturnStatusCode500()
        {
            var client   = new WebDavClient().SetWebDavDispatcher(Dispatcher.MockFaulted());
            var response = await client.Lock("http://example.com/new");

            Assert.Equal(500, response.StatusCode);
        }
Esempio n. 5
0
        public async void When_IsAppliedToResourceAndAncestors_Should_SendDepthHeaderEqualsInfinity()
        {
            var dispatcher = Dispatcher.Mock();
            var client = new WebDavClient().SetWebDavDispatcher(dispatcher);

            await client.Lock("http://example.com", new LockParameters { ApplyTo = ApplyTo.Lock.ResourceAndAncestors });
            await dispatcher.Received(1)
                .Send(Arg.Any<Uri>(), WebDavMethod.Lock, Arg.Is(Predicates.CompareHeader("Depth", "infinity")), CancellationToken.None);
        }
Esempio n. 6
0
        public async void When_IsCalledWithCancellationToken_Should_SendRequestWithIt()
        {
            var cts = new CancellationTokenSource();
            var dispatcher = Dispatcher.Mock();
            var client = new WebDavClient().SetWebDavDispatcher(dispatcher);

            await client.Lock("http://example.com/new", new LockParameters { CancellationToken = cts.Token });
            await dispatcher.Received(1)
                .Send(Arg.Any<Uri>(), WebDavMethod.Lock, Arg.Any<RequestParameters>(), cts.Token);
        }
Esempio n. 7
0
        public async void When_IsAppliedToResourceOnly_Should_SendDepthHeaderEqualsZero()
        {
            var dispatcher = Dispatcher.Mock();
            var client     = new WebDavClient().SetWebDavDispatcher(dispatcher);

            await client.Lock("http://example.com", new LockParameters { ApplyTo = ApplyTo.Lock.ResourceOnly });

            await dispatcher.Received(1)
            .Send(Arg.Any <Uri>(), WebDavMethod.Lock, Arg.Is(Predicates.CompareHeader("Depth", "0")), CancellationToken.None);
        }
Esempio n. 8
0
        public async void When_IsCalledWithTimeout_Should_SendTimeoutHeaderInSeconds()
        {
            var dispatcher = Dispatcher.Mock();
            var client     = new WebDavClient().SetWebDavDispatcher(dispatcher);

            await client.Lock("http://example.com", new LockParameters { Timeout = TimeSpan.FromMinutes(2) });

            await dispatcher.Received(1)
            .Send(Arg.Any <Uri>(), WebDavMethod.Lock, Arg.Is(Predicates.CompareHeader("Timeout", "Second-120")), CancellationToken.None);
        }
Esempio n. 9
0
        public async void When_IsCalledWithCancellationToken_Should_SendRequestWithIt()
        {
            var cts        = new CancellationTokenSource();
            var dispatcher = Dispatcher.Mock();
            var client     = new WebDavClient().SetWebDavDispatcher(dispatcher);

            await client.Lock("http://example.com/new", new LockParameters { CancellationToken = cts.Token });

            await dispatcher.Received(1)
            .Send(Arg.Any <Uri>(), WebDavMethod.Lock, Arg.Any <RequestParameters>(), cts.Token);
        }
Esempio n. 10
0
        public async void When_IsCalledWithDefaultArguments_Should_SendNoHeaders()
        {
            var requestUri = new Uri("http://example.com/new");
            var dispatcher = Dispatcher.Mock();
            var client     = new WebDavClient().SetWebDavDispatcher(dispatcher);

            await client.Lock(requestUri);

            await dispatcher.Received(1)
            .Send(requestUri, WebDavMethod.Lock, Arg.Is <RequestParameters>(x => !x.Headers.Any()), CancellationToken.None);
        }
Esempio n. 11
0
        public async void When_IsCalled_Should_SendLockRequest()
        {
            var requestUri = new Uri("http://example.com/new");
            var dispatcher = Dispatcher.Mock();
            var client     = new WebDavClient().SetWebDavDispatcher(dispatcher);

            await client.Lock(requestUri);

            await dispatcher.Received(1)
            .Send(requestUri, WebDavMethod.Lock, Arg.Any <RequestParameters>(), CancellationToken.None);
        }
Esempio n. 12
0
        public async void When_IsCalledWithDefaultArguments_Should_SendNoHeaders()
        {
            var requestUri = new Uri("http://example.com/new");
            var dispatcher = Dispatcher.Mock();
            var client = new WebDavClient().SetWebDavDispatcher(dispatcher);

            await dispatcher.DidNotReceiveWithAnyArgs().Send(requestUri, Arg.Any<HttpMethod>(), new RequestParameters(), CancellationToken.None);
            await client.Lock(requestUri);
            await dispatcher.Received(1)
                .Send(requestUri, WebDavMethod.Lock, Arg.Is<RequestParameters>(x => !x.Headers.Any()), CancellationToken.None);
        }
Esempio n. 13
0
        public async void When_IsCalledWithLockScopeExclusive_Should_AddExclusiveLockScopeToContent()
        {
            const string expectedContent =
                @"<?xml version=""1.0"" encoding=""utf-8""?>
<D:lockinfo xmlns:D=""DAV:"">
  <D:lockscope>
    <D:exclusive />
  </D:lockscope>
  <D:locktype>
    <D:write />
  </D:locktype>
</D:lockinfo>";
            var dispatcher = Dispatcher.Mock();
            var client     = new WebDavClient().SetWebDavDispatcher(dispatcher);

            await client.Lock("http://example.com", new LockParameters { LockScope = LockScope.Exclusive });

            await dispatcher.Received(1)
            .Send(Arg.Any <Uri>(), WebDavMethod.Lock, Arg.Is(Predicates.CompareRequestContent(expectedContent)), CancellationToken.None);
        }
Esempio n. 14
0
        public async void When_IsCalledWithDefaultArguments_Should_SendLockInfo()
        {
            const string expectedContent =
                @"<?xml version=""1.0"" encoding=""utf-8""?>
<D:lockinfo xmlns:D=""DAV:"">
  <D:lockscope>
    <D:shared />
  </D:lockscope>
  <D:locktype>
    <D:write />
  </D:locktype>
</D:lockinfo>";
            var dispatcher = Dispatcher.Mock();
            var client     = new WebDavClient().SetWebDavDispatcher(dispatcher);

            await client.Lock("http://example.com");

            await dispatcher.Received(1)
            .Send(Arg.Any <Uri>(), WebDavMethod.Lock, Arg.Is(Predicates.CompareRequestContent(expectedContent)), CancellationToken.None);
        }
        //Locks the given Resource and returns the LockToken
        //if the locking was successfull
        //If not null will be returned
        public string LockResource(WebDavClient client, string destUri, LockScope lockScope)
        {
            _logger.Debug("Locking remote element " + destUri);
            LockResponse lockResponse = client.Lock(destUri, new LockParameters()
            {
                LockScope = lockScope,
                Owner     = _owner,
            }).Result;

            if (lockResponse.IsSuccessful)
            {
                _logger.Debug("Successfully locked");
                ActiveLock ActiveLock = lockResponse.ActiveLocks.Where(x => x.Owner.Value.Equals(_owner.Value)).FirstOrDefault();
                return(ActiveLock.LockToken);
            }
            else
            {
                _logger.Error("Locking ressource failed! description: " + lockResponse.Description);
            }

            return(null);
        }
Esempio n. 16
0
        public async void When_IsCalledWithUriLockOwner_Should_AddOwnerToContent()
        {
            const string expectedContent =
@"<?xml version=""1.0"" encoding=""utf-8""?>
<D:lockinfo xmlns:D=""DAV:"">
  <D:lockscope>
    <D:shared />
  </D:lockscope>
  <D:locktype>
    <D:write />
  </D:locktype>
  <D:owner>
    <D:href>http://example.com/owner</D:href>
  </D:owner>
</D:lockinfo>";
            var dispatcher = Dispatcher.Mock();
            var client = new WebDavClient().SetWebDavDispatcher(dispatcher);

            await client.Lock("http://example.com", new LockParameters { Owner = new UriLockOwner("http://example.com/owner") });
            await dispatcher.Received(1)
                .Send(Arg.Any<Uri>(), WebDavMethod.Lock, Arg.Is(Predicates.CompareRequestContent(expectedContent)), CancellationToken.None);
        }
Esempio n. 17
0
 public async void When_RequestIsFailed_Should_ReturnStatusCode500()
 {
     var client = new WebDavClient().SetWebDavDispatcher(Dispatcher.MockFaulted());
     var response = await client.Lock("http://example.com/new");
     Assert.Equal(500, response.StatusCode);
 }
Esempio n. 18
0
        public async void When_IsCalledWithDefaultArguments_Should_SendLockInfo()
        {
            const string expectedContent =
@"<?xml version=""1.0"" encoding=""utf-8""?>
<D:lockinfo xmlns:D=""DAV:"">
  <D:lockscope>
    <D:shared />
  </D:lockscope>
  <D:locktype>
    <D:write />
  </D:locktype>
</D:lockinfo>";
            var dispatcher = Dispatcher.Mock();
            var client = new WebDavClient().SetWebDavDispatcher(dispatcher);

            await client.Lock("http://example.com");
            await dispatcher.Received(1)
                .Send(Arg.Any<Uri>(), WebDavMethod.Lock, Arg.Is(Predicates.CompareRequestContent(expectedContent)), CancellationToken.None);
        }
Esempio n. 19
0
        public async void When_IsCalledWithTimeout_Should_SendTimeoutHeaderInSeconds()
        {
            var dispatcher = Dispatcher.Mock();
            var client = new WebDavClient().SetWebDavDispatcher(dispatcher);

            await client.Lock("http://example.com", new LockParameters { Timeout = TimeSpan.FromMinutes(2) });
            await dispatcher.Received(1)
                .Send(Arg.Any<Uri>(), WebDavMethod.Lock, Arg.Is(Predicates.CompareHeader("Timeout", "Second-120")), CancellationToken.None);
        }