コード例 #1
0
        /// <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);
        }
コード例 #2
0
        public void UT_CodedUrl_TryParse_InvalidCodedUrlFormatParentheses_ShouldReturnFalse()
        {
            var parseResult = CodedUrl.TryParse("(urn:uuid:my-lock-token)", out var codedUrl);

            Assert.IsFalse(parseResult);
            Assert.IsNull(codedUrl);
        }
コード例 #3
0
        public void UT_CodedUrl_TryParse_InvalidCodedUrlFormatAbsoluteUri_ShouldReturnFalse()
        {
            var parseResult = CodedUrl.TryParse("urn:uuid:my-lock-token", out var codedUrl);

            Assert.IsFalse(parseResult);
            Assert.IsNull(codedUrl);
        }
コード例 #4
0
        public void UT_CodedUrl_TryParse_ValidCodedUrlFormat()
        {
            var expectedString = "<urn:uuid:my-lock-token>";
            var parseResult    = CodedUrl.TryParse(expectedString, out var codedUrl);

            Assert.IsTrue(parseResult);
            Assert.IsNotNull(codedUrl);
            Assert.AreEqual(expectedString, codedUrl.ToString());
        }
コード例 #5
0
        public void UT_LockToken_IfHeaderNoTagListFormat_IfHeaderWithBrackets()
        {
            var parseResult = CodedUrl.TryParse("<urn:uuid:my-lock-token>", out var codedUrl);

            var lockToken             = new LockToken(codedUrl.AbsoluteUri);
            var absoluteUri           = lockToken.AbsoluteUri;
            var lockTokenHeaderFormat = lockToken.LockTokenHeaderFormat;
            var noTagList             = lockToken.IfHeaderNoTagListFormat;

            Assert.AreEqual("urn:uuid:my-lock-token", absoluteUri.ToString());
            Assert.AreEqual("<urn:uuid:my-lock-token>", lockTokenHeaderFormat.ToString());
            Assert.AreEqual("(<urn:uuid:my-lock-token>)", noTagList.ToString());
            Assert.IsTrue(parseResult);
        }
コード例 #6
0
        /// <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);
        }
コード例 #7
0
        public async Task UT_WebDavClient_Unlock()
        {
            var testFileToUnlock = UriHelper.CombineUrl(WebDavRootFolder, TestFile, true);
            var lockTokenString  = "<opaquelocktoken:cb2b7d6d-98ea-47cf-b569-5b98126b8f13.6df801d2b41b3b6e>";

            CodedUrl.TryParse(lockTokenString, out var codedUrl);

            var mockHandler = new MockHttpMessageHandler();

            var requestHeaders = new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>(WebDavRequestHeader.LockToken, lockTokenString)
            };

            mockHandler.When(WebDavMethod.Unlock, testFileToUnlock).WithHeaders(requestHeaders).Respond(HttpStatusCode.NoContent);

            using (var client = CreateWebDavClient(mockHandler))
            {
                var lockToken = new LockToken(codedUrl.AbsoluteUri);
                var response  = await client.UnlockAsync(testFileToUnlock, lockToken);

                Assert.IsTrue(response.IsSuccessStatusCode);
            }
        }