public void TestAppendUrlPathBasePathDoesNotEndInForwardSlashAppendedPathDoesNotBeginWithForwardSlash()
        {
            var sb = new StringBuilder("/some");

            Assert.AreEqual(sb, sb.AppendUrlPath("path"));

            Assert.AreEqual("/some/path", sb.ToString());
        }
        public void TestAppendUrlPathBasePathIsEmpty()
        {
            var sb = new StringBuilder();

            Assert.AreEqual(sb, sb.AppendUrlPath("some/path"));

            Assert.AreEqual("some/path", sb.ToString());
        }
        public void TestAppendUrlPathNull()
        {
            var sb = new StringBuilder("/some/path");

            Assert.AreEqual(sb, sb.AppendUrlPath(null));

            Assert.AreEqual("/some/path", sb.ToString());
        }
        public void TestAppendUrlPathStringEmpty()
        {
            var sb = new StringBuilder("/some/path");

            Assert.AreEqual(sb, sb.AppendUrlPath(string.Empty));

            Assert.AreEqual("/some/path/", sb.ToString());
        }
        internal Uri GetResourceUri(string path, ApiMethod method)
        {
            try
            {
                if ((path.StartsWith("https://", StringComparison.OrdinalIgnoreCase) &&
                   !path.StartsWith(this.ApiEndpoint, StringComparison.OrdinalIgnoreCase)) ||
                    path.StartsWith("http://", StringComparison.OrdinalIgnoreCase))
                {
                    return new Uri(path, UriKind.Absolute);
                }

                StringBuilder sb;
                if (path.StartsWith(this.ApiEndpoint, StringComparison.OrdinalIgnoreCase))
                {
                    sb = new StringBuilder(path);
                }
                else
                {
                    sb = new StringBuilder(this.ApiEndpoint);
                    sb = sb.AppendUrlPath(path);
                }

                var resourceUrl = new Uri(sb.ToString(), UriKind.Absolute);
                sb.Append(string.IsNullOrEmpty(resourceUrl.Query) ? "?" : "&");

                if (method != ApiMethod.Download)
                {
                    sb.AppendQueryParam(QueryParameters.SuppressResponseCodes, "true");
                    sb.Append("&").AppendQueryParam(QueryParameters.SuppressRedirects, "true");
                }

                return new Uri(sb.ToString(), UriKind.Absolute);
            }
            catch (FormatException)
            {
                throw new ArgumentException(
                    String.Format(CultureInfo.CurrentUICulture, ResourceHelper.GetString("UrlInvalid"), path),
                    "path");
            }
        }
Beispiel #6
0
        private Uri ConstructUploadUri(string uploadLocation)
        {
            Uri resourceUrl;

            try
            {
                // Do not forget to append the query string that was originally sent.
                // this.Url contains the original query string plus suppress_response_codes and suppress_redirects.
                // We will just reuse that.
                string originalQueryString = this.Url.Query;

                if (!string.IsNullOrWhiteSpace(originalQueryString))
                {
                    // Uri.Query return contains "?" (e.g., ?key=value)
                    int indexOfQuestion = originalQueryString.IndexOf('?');
                    if (indexOfQuestion != -1)
                    {
                        originalQueryString = originalQueryString.Substring(indexOfQuestion + 1);
                    }

                    uploadLocation = uploadLocation.Contains("?")
                                         ? uploadLocation + "&" + originalQueryString
                                         : uploadLocation + "?" + originalQueryString;
                }

                resourceUrl = new Uri(uploadLocation, UriKind.Absolute);
            }
            catch (FormatException exp)
            {
                throw new LiveConnectException(
                          ApiOperation.ApiClientErrorCode,
                          ResourceHelper.GetString("NoUploadLinkFound"),
                          exp);
            }

            string query = resourceUrl.Query;
            string url   = resourceUrl.GetComponents(
                UriComponents.SchemeAndServer | UriComponents.Path | UriComponents.KeepDelimiter,
                UriFormat.SafeUnescaped);

            var sb = new StringBuilder(url);

            if (!this.IsFilePath)
            {
                sb.AppendUrlPath(Uri.EscapeDataString(this.FileName));
            }

            bool hasQuery = !string.IsNullOrEmpty(query);

            if (hasQuery)
            {
                sb.Append(query);
            }

            // Only add the overwrite parameter for folder uploads,
            // since overwrite does not make sense on file path uploads (because they are always overwriting).
            if (!this.IsFilePath)
            {
                sb.Append(hasQuery ? '&' : '?');
                sb.AppendQueryParam(QueryParameters.Overwrite, QueryParameters.GetOverwriteValue(this.OverwriteOption));
            }

            try
            {
                return(new Uri(sb.ToString(), UriKind.Absolute));
            }
            catch (FormatException exp)
            {
                throw new LiveConnectException(
                          ApiOperation.ApiClientErrorCode,
                          String.Format(CultureInfo.CurrentUICulture, ResourceHelper.GetString("FileNameInvalid"), "fileName"),
                          exp);
            }
        }
        private Uri ConstructUploadUri(string uploadLocation)
        {
            Uri resourceUrl;
            try
            {
                // Do not forget to append the query string that was originally sent.
                // this.Url contains the original query string plus suppress_response_codes and suppress_redirects.
                // We will just reuse that.
                string originalQueryString = this.Url.Query;

                if (!string.IsNullOrWhiteSpace(originalQueryString))
                {
                    // Uri.Query return contains "?" (e.g., ?key=value)
                    int indexOfQuestion = originalQueryString.IndexOf('?');
                    if (indexOfQuestion != -1)
                    {
                        originalQueryString = originalQueryString.Substring(indexOfQuestion + 1);
                    }

                    uploadLocation = uploadLocation.Contains("?")
                                         ? uploadLocation + "&" + originalQueryString
                                         : uploadLocation + "?" + originalQueryString;
                }

                resourceUrl = new Uri(uploadLocation, UriKind.Absolute);
            }
            catch (FormatException exp)
            {
                throw new LiveConnectException(
                    ApiOperation.ApiClientErrorCode,
                    ResourceHelper.GetString("NoUploadLinkFound"),
                    exp);
            }

            string query = resourceUrl.Query;
            string url = resourceUrl.GetComponents(
                UriComponents.SchemeAndServer | UriComponents.Path | UriComponents.KeepDelimiter, 
                UriFormat.SafeUnescaped);

            var sb = new StringBuilder(url);
            if (!this.IsFilePath)
            {
                sb.AppendUrlPath(Uri.EscapeDataString(this.FileName));
            }

            bool hasQuery = !string.IsNullOrEmpty(query);
            if (hasQuery)
            {
                sb.Append(query);
            }

            // Only add the overwrite parameter for folder uploads,
            // since overwrite does not make sense on file path uploads (because they are always overwriting).
            if (!this.IsFilePath)
            {
                sb.Append(hasQuery ? '&' : '?');
                sb.AppendQueryParam(QueryParameters.Overwrite, QueryParameters.GetOverwriteValue(this.OverwriteOption));
            }

            try
            {
                return new Uri(sb.ToString(), UriKind.Absolute);
            }
            catch (FormatException exp)
            {
                throw new LiveConnectException(
                    ApiOperation.ApiClientErrorCode,
                    String.Format(CultureInfo.CurrentUICulture, ResourceHelper.GetString("FileNameInvalid"), "fileName"),
                    exp);
            }
        }