public async Task GetSucceedsAccessToLockedDocumentTest() { var response = await Client.LockAsync( "/test1.txt", WebDavTimeoutHeaderValue.CreateInfiniteWebDavTimeout(), WebDavDepthHeaderValue.Zero, new LockInfo() { LockScope = LockScope.CreateExclusiveLockScope(), LockType = LockType.CreateWriteLockType(), }).ConfigureAwait(false); var prop = await WebDavResponseContentParser.ParsePropResponseContentAsync(response.EnsureSuccessStatusCode().Content).ConfigureAwait(false); Assert.NotNull(prop.LockDiscovery); Assert.Collection( prop.LockDiscovery.ActiveLock, activeLock => { Assert.Equal("/test1.txt", activeLock.LockRoot.Href); Assert.Equal(WebDavDepthHeaderValue.Zero.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)); }); var ct = CancellationToken.None; var getResponse = await Client.GetAsync("/test1.txt", ct).ConfigureAwait(false); getResponse.EnsureSuccessStatusCode(); }
/// <summary> /// Gets a <see cref="LockToken"/> from a <see cref="WebDavResponseMessage"/>. /// </summary> /// <param name="responseMessage">The <see cref="WebDavResponseMessage"/> whose <see cref="LockToken"/> should be retrieved.</param> /// <returns>The <see cref="LockToken"/> of the <see cref="WebDavResponseMessage"/> or null if the WebDavResponseMessage does not contain a lock token.</returns> public static async Task <LockToken> GetLockTokenFromWebDavResponseMessage(WebDavResponseMessage responseMessage) { // Try to get lock token from response header. if (responseMessage.Headers.TryGetValues(WebDavRequestHeader.LockToken, out IEnumerable <string> lockTokenHeaderValues)) { // We assume only one Lock-Token header is sent, based on the spec: https://tools.ietf.org/html/rfc4918#section-9.10.1 var lockTokenHeaderValue = lockTokenHeaderValues.FirstOrDefault(); // Make sure the lockTokenHeaderValue is valid according to spec (https://tools.ietf.org/html/rfc4918#section-10.5). if (lockTokenHeaderValue != null && CodedUrl.TryParse(lockTokenHeaderValue, out var codedUrl)) { return(new LockToken(codedUrl.AbsoluteUri)); } } // If lock token was not submitted by response header, it should be found in the response content. try { var prop = await WebDavResponseContentParser.ParsePropResponseContentAsync(responseMessage.Content); var href = prop.LockDiscovery.ActiveLock[0].LockToken.Href; if (AbsoluteUri.TryParse(href, out var absoluteUri)) { return(new LockToken(absoluteUri)); } } catch (Exception) { return(null); } return(null); }
public async Task AddLockToRootRecursiveWithPrincipalOwnerTest() { var response = await Client.LockAsync( "/", WebDavTimeoutHeaderValue.CreateInfiniteWebDavTimeout(), WebDavDepthHeaderValue.Infinity, new LockInfo() { LockScope = LockScope.CreateExclusiveLockScope(), LockType = LockType.CreateWriteLockType(), OwnerRaw = new XElement("{DAV:}owner", "principal"), }) ; var prop = await WebDavResponseContentParser.ParsePropResponseContentAsync(response.EnsureSuccessStatusCode().Content); Assert.Collection( 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.Equal("<owner xmlns=\"DAV:\">principal</owner>", activeLock.OwnerRaw.ToString(SaveOptions.DisableFormatting)); Assert.Equal(WebDavTimeoutHeaderValue.CreateInfiniteWebDavTimeout().ToString(), activeLock.Timeout, StringComparer.OrdinalIgnoreCase); Assert.NotNull(activeLock.LockToken?.Href); Assert.True(Uri.IsWellFormedUriString(activeLock.LockToken.Href, UriKind.RelativeOrAbsolute)); }); }
public async Task AddLockToRootRecursiveWithTimeoutTest() { var response = await Client.LockAsync( "/", WebDavTimeoutHeaderValue.CreateWebDavTimeout(TimeSpan.FromSeconds(1)), WebDavDepthHeaderValue.Infinity, new LockInfo() { LockScope = LockScope.CreateExclusiveLockScope(), LockType = LockType.CreateWriteLockType(), }); var prop = await WebDavResponseContentParser.ParsePropResponseContentAsync(response.EnsureSuccessStatusCode().Content); Assert.Collection( 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.CreateWebDavTimeout(TimeSpan.FromSeconds(1)).ToString(), activeLock.Timeout, StringComparer.OrdinalIgnoreCase); Assert.NotNull(activeLock.LockToken?.Href); Assert.True(Uri.IsWellFormedUriString(activeLock.LockToken.Href, UriKind.RelativeOrAbsolute)); }); }
public async Task AddLockToRootAndTryRefreshTest() { var lockResponse = await Client.LockAsync( "/", WebDavTimeoutHeaderValue.CreateInfiniteWebDavTimeout(), WebDavDepthHeaderValue.Infinity, new LockInfo() { LockScope = LockScope.CreateExclusiveLockScope(), LockType = LockType.CreateWriteLockType(), }) ; lockResponse.EnsureSuccessStatusCode(); var prop = await WebDavResponseContentParser.ParsePropResponseContentAsync(lockResponse.Content); var lockToken = prop.LockDiscovery.ActiveLock.Single().LockToken; Assert.True(AbsoluteUri.TryParse(lockToken.Href, out var lockTokenUri)); var refreshResponse = await Client.RefreshLockAsync( "/", WebDavTimeoutHeaderValue.CreateInfiniteWebDavTimeout(), new LockToken(lockTokenUri)); refreshResponse.EnsureSuccessStatusCode(); var refreshProp = await WebDavResponseContentParser.ParsePropResponseContentAsync(refreshResponse.Content); Assert.NotNull(refreshProp.LockDiscovery); }
public async Task UT_WebDavContentParser_ParsePropResponseContentAsync() { var str = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><D:prop xmlns:D=\"DAV:\"><D:lockdiscovery><D:activelock><D:locktype><D:write/></D:locktype><D:lockscope><D:exclusive/></D:lockscope><D:depth>infinity</D:depth><D:owner><D:href>http://example.org/~ejw/contact.html</D:href></D:owner><D:timeout>Second-604800</D:timeout><D:locktoken><D:href>urn:uuid:e71d4fae-5dec-22d6-fea5-00a0c91e6be4</D:href></D:locktoken><D:lockroot><D:href>http://example.com/workspace/webdav/proposal.doc</D:href></D:lockroot></D:activelock></D:lockdiscovery></D:prop>"; var content = new StringContent(str); var multistatus = await WebDavResponseContentParser.ParsePropResponseContentAsync(content); Assert.IsNotNull(multistatus); }
/// <summary> /// Gets the <see cref="ActiveLock"/> from a <see cref="WebDavResponseMessage"/>. /// </summary> /// <param name="responseMessage">The <see cref="WebDavResponseMessage"/> whose <see cref="ActiveLock"/> should be retrieved.</param> /// <returns>The <see cref="ActiveLock"/> of the <see cref="WebDavResponseMessage"/> or null if the <see cref="WebDavResponseMessage"/> does not contain a lock token.</returns> /// <exception cref="ArgumentNullException">Thrown if the <paramref name="responseMessage"/> is null.</exception> public static async Task <ActiveLock> GetActiveLockFromWebDavResponseMessage(WebDavResponseMessage responseMessage) { if (responseMessage == null) { throw new ArgumentNullException(nameof(responseMessage)); } var prop = await WebDavResponseContentParser.ParsePropResponseContentAsync(responseMessage.Content); var activeLock = prop.LockDiscovery?.ActiveLock.FirstOrDefault(); if (activeLock == null) { return(null); } // If lock token was not be found in the response content, it should be submitted by response header. if (activeLock.LockToken == null) { // Try to get lock token from response header. if (responseMessage.Headers.TryGetValues(WebDavRequestHeader.LockToken, out IEnumerable <string> lockTokenHeaderValues)) { // We assume only one Lock-Token header is sent, based on the spec: https://tools.ietf.org/html/rfc4918#section-9.10.1 var lockTokenHeaderValue = lockTokenHeaderValues.FirstOrDefault(); // Make sure the lockTokenHeaderValue is valid according to spec (https://tools.ietf.org/html/rfc4918#section-10.5). if (lockTokenHeaderValue != null && CodedUrl.TryParse(lockTokenHeaderValue, out var _)) { activeLock.LockToken = new WebDavLockToken { Href = lockTokenHeaderValue } } ; } } return(activeLock); }