コード例 #1
0
        public void ReferenceResolutionNormalExamples(string expectedReferenceUrl, string userInfo, string host, string port, IEnumerable <string> path, string query, string fragment)
        {
            var uri = new RelativeReferenceBuilder().WithHost(host).WithPrefixlessPathSegments(path).WithQuery(query).WithFragment(fragment).Build();

            Assert.Equal(expectedReferenceUrl, uri.ToString());

            var parsed = RelativeReference.Parse(uri.ToString());

            Assert.Equal(userInfo, ((IUniformResourceIdentifierReference)parsed).UserInfo);
            Assert.Equal(host, parsed.Host);
            Assert.Equal(port, parsed.Port);
            Assert.Equal(path, parsed.PathSegments);
            Assert.Equal(query, parsed.Query);
            Assert.Equal(fragment, parsed.Fragment);

            var deconstructed = new RelativeReferenceBuilder(uri).Build();

            Assert.Equal(userInfo, ((IUniformResourceIdentifierReference)deconstructed).UserInfo);
            Assert.Equal(host, deconstructed.Host);
            Assert.Equal(port, deconstructed.Port);
            Assert.Equal(path, deconstructed.PathSegments);
            Assert.Equal(query, deconstructed.Query);
            Assert.Equal(fragment, deconstructed.Fragment);

            var systemUri = uri.ToUri();

            Assert.False(systemUri.IsAbsoluteUri);
            Assert.Equal(uri.ToString(), systemUri.ToString());
        }
コード例 #2
0
        /// <summary>
        /// Resolves a relative URI against a base URI.
        /// </summary>
        /// <typeparam name="T">The type of the base URI; also the type of the result.</typeparam>
        /// <param name="baseUri">The base URI.</param>
        /// <param name="relativeUri">The relative URI.</param>
        /// <param name="factory">The factory method used to create a new URI.</param>
        public static T Resolve <T>(T baseUri, RelativeReference relativeUri, DelegateFactory <T> factory)
            where T : IUniformResourceIdentifier
        {
            _ = relativeUri ?? throw new ArgumentNullException(nameof(relativeUri));
            _ = factory ?? throw new ArgumentNullException(nameof(factory));

            // See 5.2.2, except that referenceUri will always have a null Scheme.
            string?userInfo, host, port, query;
            IEnumerable <string> pathSegments;

            if (relativeUri.AuthorityIsDefined())
            {
                userInfo     = ((IUniformResourceIdentifierReference)relativeUri).UserInfo;
                host         = relativeUri.Host;
                port         = relativeUri.Port;
                pathSegments = relativeUri.PathSegments;
                query        = relativeUri.Query;
            }
            else
            {
                if (relativeUri.PathIsEmpty())
                {
                    pathSegments = baseUri.PathSegments;
                    query        = relativeUri.Query ?? baseUri.Query;
                }
                else
                {
                    if (relativeUri.PathIsAbsolute)
                    {
                        pathSegments = relativeUri.PathSegments;
                    }
                    else
                    {
                        // See 5.2.3
                        if (baseUri.AuthorityIsDefined() && baseUri.PathIsEmpty())
                        {
                            pathSegments = Enumerable.Repeat("", 1).Concat(relativeUri.PathSegments);
                        }
                        else
                        {
                            pathSegments = baseUri.PathSegments.Take(baseUri.PathSegments.Count - 1).Concat(relativeUri.PathSegments);
                        }
                    }
                    query = relativeUri.Query;
                }
                userInfo = baseUri.UserInfo;
                host     = baseUri.Host;
                port     = baseUri.Port;
            }
            return(factory(userInfo, host, port, pathSegments, query, relativeUri.Fragment));
        }
コード例 #3
0
        public void ReferenceResolutionMakesPathRelativeIfNecessary()
        {
            var uri = new RelativeReferenceBuilder().WithPrefixlessPathSegments(new [] { "g:x" }).Build();

            Assert.Equal("./g:x", uri.ToString());

            var parsed = RelativeReference.Parse(uri.ToString());

            Assert.Equal(new [] { ".", "g:x" }, parsed.PathSegments);

            var deconstructed = new RelativeReferenceBuilder(uri).Build();

            Assert.Equal(new[] { ".", "g:x" }, deconstructed.PathSegments);

            var systemUri = uri.ToUri();

            Assert.False(systemUri.IsAbsoluteUri);
            Assert.Equal(uri.ToString(), systemUri.ToString());
        }