Skip to content

Commit c317c83

Browse files
aminamin
authored andcommitted
add warning message when the url has a additional slash or has fewer slash in the end of base url and the first of RequestUri
1 parent 4037602 commit c317c83

File tree

5 files changed

+180
-100
lines changed

5 files changed

+180
-100
lines changed

src/HttpClientToCurl/Builder.cs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,35 +26,56 @@ internal static StringBuilder Initialize(HttpMethod httpMethod)
2626

2727
internal static StringBuilder AddAbsoluteUrl(this StringBuilder stringBuilder, string baseUrl, string requestUri)
2828
{
29-
if (!string.IsNullOrWhiteSpace(baseUrl))
29+
bool hasSlashEndOfBaseUrl = false;
30+
bool hasSlashFirstOfRequestUri = false;
31+
string splitterUrl = string.Empty;
32+
33+
string inputBaseUrl = baseUrl?.Trim();
34+
if (!string.IsNullOrWhiteSpace(inputBaseUrl))
3035
{
31-
string inputBaseUrl = baseUrl.Trim();
36+
if (inputBaseUrl.EndsWith('/'))
37+
hasSlashEndOfBaseUrl = true;
3238

3339
string inputRequestUri = requestUri?.Trim();
3440
if (!string.IsNullOrWhiteSpace(inputRequestUri))
3541
{
3642
if (inputRequestUri.StartsWith('/'))
37-
{
38-
inputRequestUri = inputRequestUri.Remove(0, 1);
39-
var warningMessage = AddWarningMessageForAdditionalSlash();
43+
hasSlashFirstOfRequestUri = true;
4044

45+
string warningMessage = CheckAndAddWarningMessageForIncorrectSlash(hasSlashEndOfBaseUrl, hasSlashFirstOfRequestUri, out splitterUrl);
46+
if (!string.IsNullOrEmpty(warningMessage))
47+
{
4148
stringBuilder
4249
.Insert(0, warningMessage)
4350
.Insert(warningMessage.Length, Environment.NewLine);
4451
}
4552
}
46-
else
53+
54+
if (hasSlashEndOfBaseUrl && (string.IsNullOrEmpty(inputRequestUri) || hasSlashFirstOfRequestUri))
4755
inputBaseUrl = inputBaseUrl.Remove(inputBaseUrl.Length - 1);
48-
56+
4957
return stringBuilder
50-
.Append($"{inputBaseUrl}{inputRequestUri}")
58+
.Append($"{inputBaseUrl}{splitterUrl}{inputRequestUri}")
5159
.Append(' ');
5260
}
5361

5462
throw new InvalidDataException("baseUrl argument is null or empty!");
5563

56-
string AddWarningMessageForAdditionalSlash()
57-
=> "# Warning: you must remove the Slash at the first of the requestUri.";
64+
string CheckAndAddWarningMessageForIncorrectSlash(bool hasOnBaseUrl, bool hasOnRequestUri, out string splitter)
65+
{
66+
string message = string.Empty;
67+
splitter = string.Empty;
68+
69+
if (hasOnBaseUrl && hasOnRequestUri)
70+
message = "# Warning: you must remove the Slash at the end of base url or at the first of the requestUri.";
71+
else if (!hasOnBaseUrl && !hasOnRequestUri)
72+
{
73+
splitter = "/";
74+
message = "# Warning: you must add the Slash at the end of base url or at the first of the requestUri.";
75+
}
76+
77+
return message;
78+
}
5879
}
5980

6081
internal static StringBuilder AddHeaders(this StringBuilder stringBuilder, HttpClient httpClient, HttpRequestMessage httpRequestMessage, bool needAddDefaultHeaders = true)

0 commit comments

Comments
 (0)