Skip to content

Commit 94c9429

Browse files
authored
Merge pull request #53 from mozhganEtaati/feature/HCG-49
Closes: #49
2 parents 751e606 + 38c6ea0 commit 94c9429

17 files changed

+940
-0
lines changed

HttpClientToCurlGenerator.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "solutionItems", "solutionIt
2525
pre-push = pre-push
2626
EndProjectSection
2727
EndProject
28+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HttpRequestMessageToCurlGeneratorTest", "HttpRequestMessageToCurlTest\HttpRequestMessageToCurlGeneratorTest.csproj", "{007CA9E0-CDF0-4375-8E8C-A24C9A7BF531}"
29+
EndProject
2830
Global
2931
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3032
Debug|Any CPU = Debug|Any CPU
@@ -51,6 +53,10 @@ Global
5153
{403B236B-D7D8-43FB-B47E-DCCAF8BA96C4}.Debug|Any CPU.Build.0 = Debug|Any CPU
5254
{403B236B-D7D8-43FB-B47E-DCCAF8BA96C4}.Release|Any CPU.ActiveCfg = Release|Any CPU
5355
{403B236B-D7D8-43FB-B47E-DCCAF8BA96C4}.Release|Any CPU.Build.0 = Release|Any CPU
56+
{007CA9E0-CDF0-4375-8E8C-A24C9A7BF531}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
57+
{007CA9E0-CDF0-4375-8E8C-A24C9A7BF531}.Debug|Any CPU.Build.0 = Debug|Any CPU
58+
{007CA9E0-CDF0-4375-8E8C-A24C9A7BF531}.Release|Any CPU.ActiveCfg = Release|Any CPU
59+
{007CA9E0-CDF0-4375-8E8C-A24C9A7BF531}.Release|Any CPU.Build.0 = Release|Any CPU
5460
EndGlobalSection
5561
GlobalSection(SolutionProperties) = preSolution
5662
HideSolutionNode = FALSE
@@ -61,6 +67,7 @@ Global
6167
{323022D2-AAA7-443B-895C-77F5B1634D68} = {A8574DB9-8411-4F81-A82E-F97AD00EF8AF}
6268
{F7B35144-A00C-45BE-BC41-36C1B21FCD18} = {A8574DB9-8411-4F81-A82E-F97AD00EF8AF}
6369
{403B236B-D7D8-43FB-B47E-DCCAF8BA96C4} = {A8574DB9-8411-4F81-A82E-F97AD00EF8AF}
70+
{007CA9E0-CDF0-4375-8E8C-A24C9A7BF531} = {E36BF269-7F5D-4DE7-99B0-14567F9CD6B3}
6471
EndGlobalSection
6572
GlobalSection(ExtensibilityGlobals) = postSolution
6673
SolutionGuid = {E5E0FFF6-54C3-4BA1-91F3-EF3513A18D5D}
Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
using System.Net.Mime;
2+
using System.Text;
3+
using FluentAssertions;
4+
using HttpClientToCurl;
5+
using HttpClientToCurl.Utility;
6+
using Microsoft.AspNetCore.WebUtilities;
7+
8+
namespace HttpRequestMessageToCurlGeneratorTest.FunctionalTest;
9+
10+
public class SuccessScenariosTests
11+
{
12+
#region :: GenerateCurlInString For Post Method ::
13+
14+
[Fact]
15+
public void Success_GenerateCurlInString_For_PostMethod()
16+
{
17+
// Arrange
18+
string requestBody = /*lang=json,strict*/ @"{""name"":""sara"",""requestId"":10001001,""amount"":20000}";
19+
20+
var requestUri = "api/test";
21+
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri) { Content = new StringContent(requestBody, Encoding.UTF8, MediaTypeNames.Application.Json) };
22+
httpRequestMessage.Headers.Add("Authorization", "Bearer 4797c126-3f8a-454a-aff1-96c0220dae61");
23+
var baseAddress = new Uri("http://localhost:1213/v1/");
24+
// Act
25+
string curlResult = httpRequestMessage.GenerateCurlInString(baseAddress);
26+
27+
// Assert
28+
curlResult.Should().NotBeNullOrEmpty();
29+
curlResult.Should().StartWith("curl -X POST");
30+
curlResult.Trim().Should()
31+
.BeEquivalentTo(
32+
@"curl -X POST 'http://localhost:1213/v1/api/test' -H 'Authorization: Bearer 4797c126-3f8a-454a-aff1-96c0220dae61' -H 'Content-Type: application/json; charset=utf-8' -d '{""name"":""sara"",""requestId"":10001001,""amount"":20000}'");
33+
}
34+
35+
[Fact]
36+
public void Success_GenerateCurlInString_With_RequestUri_TypeOf_Uri_For_PostMethod()
37+
{
38+
// Arrange
39+
string requestBody = /*lang=json,strict*/ @"{""name"":""sara"",""requestId"":10001001,""amount"":20000}";
40+
41+
var requestUri = Helpers.CreateUri("api/test");
42+
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri) { Content = new StringContent(requestBody, Encoding.UTF8, MediaTypeNames.Application.Json) };
43+
httpRequestMessage.Headers.Add("Authorization", "Bearer 4797c126-3f8a-454a-aff1-96c0220dae61");
44+
45+
var baseAddress = new Uri("http://localhost:1213/v1/");
46+
47+
// Act
48+
string curlResult = httpRequestMessage.GenerateCurlInString(baseAddress);
49+
50+
// Assert
51+
curlResult.Should().NotBeNullOrEmpty();
52+
curlResult.Should().StartWith("curl -X POST");
53+
curlResult.Trim().Should()
54+
.BeEquivalentTo(
55+
@"curl -X POST 'http://localhost:1213/v1/api/test' -H 'Authorization: Bearer 4797c126-3f8a-454a-aff1-96c0220dae61' -H 'Content-Type: application/json; charset=utf-8' -d '{""name"":""sara"",""requestId"":10001001,""amount"":20000}'");
56+
}
57+
58+
[Fact]
59+
public void GenerateCurl_When_Set_RequestUri_Inside_HttpRequestMessage_For_PostMethod()
60+
{
61+
// Arrange
62+
string requestBody = /*lang=json,strict*/ @"{""name"":""sara"",""requestId"":10001001,""amount"":20000}";
63+
64+
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "api/test") { Content = new StringContent(requestBody, Encoding.UTF8, MediaTypeNames.Application.Json) };
65+
httpRequestMessage.Headers.Add("Authorization", "Bearer 4797c126-3f8a-454a-aff1-96c0220dae61");
66+
67+
using var httpClient = new HttpClient();
68+
var baseAddress = new Uri("http://localhost:1213/v1/");
69+
70+
// Act
71+
string curlResult = httpRequestMessage.GenerateCurlInString(baseAddress);
72+
73+
// Assert
74+
curlResult.Should().NotBeNullOrEmpty();
75+
curlResult.Should().StartWith("curl -X POST");
76+
curlResult.Trim().Should()
77+
.BeEquivalentTo(
78+
@"curl -X POST 'http://localhost:1213/v1/api/test' -H 'Authorization: Bearer 4797c126-3f8a-454a-aff1-96c0220dae61' -H 'Content-Type: application/json; charset=utf-8' -d '{""name"":""sara"",""requestId"":10001001,""amount"":20000}'");
79+
}
80+
81+
[Fact]
82+
public void Success_GenerateCurlInString_When_RequestUri_Is_Null_For_PostMethod()
83+
{
84+
// Arrange
85+
string requestBody = /*lang=json,strict*/ @"{""name"":""sara"",""requestId"":10001001,""amount"":20000}";
86+
87+
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, string.Empty) { Content = new StringContent(requestBody, Encoding.UTF8, MediaTypeNames.Application.Json) };
88+
httpRequestMessage.Headers.Add("Authorization", "Bearer 4797c126-3f8a-454a-aff1-96c0220dae61");
89+
90+
var baseAddress = new Uri("http://localhost:1213/v1/");
91+
92+
// Act
93+
string curlResult = httpRequestMessage.GenerateCurlInString(baseAddress);
94+
95+
// Assert
96+
curlResult.Should().NotBeNullOrEmpty();
97+
curlResult.Should().StartWith("curl -X POST");
98+
curlResult.Trim().Should()
99+
.BeEquivalentTo(
100+
@"curl -X POST 'http://localhost:1213/v1/' -H 'Authorization: Bearer 4797c126-3f8a-454a-aff1-96c0220dae61' -H 'Content-Type: application/json; charset=utf-8' -d '{""name"":""sara"",""requestId"":10001001,""amount"":20000}'");
101+
}
102+
103+
[Fact]
104+
public void Success_GenerateCurlInString_When_RequestBody_Is_Null_For_PostMethod()
105+
{
106+
// Arrange
107+
var requestUri = "api/test";
108+
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri) { Content = new StringContent(string.Empty, Encoding.UTF8, MediaTypeNames.Application.Json) };
109+
httpRequestMessage.Headers.Add("Authorization", "Bearer 4797c126-3f8a-454a-aff1-96c0220dae61");
110+
111+
var baseAddress = new Uri("http://localhost:1213/v1/");
112+
113+
// Act
114+
string curlResult = httpRequestMessage.GenerateCurlInString(baseAddress);
115+
116+
// Assert
117+
curlResult.Should().NotBeNullOrEmpty();
118+
curlResult.Should().StartWith("curl -X POST");
119+
curlResult.Trim().Should()
120+
.BeEquivalentTo(
121+
@"curl -X POST 'http://localhost:1213/v1/api/test' -H 'Authorization: Bearer 4797c126-3f8a-454a-aff1-96c0220dae61' -H 'Content-Type: application/json; charset=utf-8' -d ''");
122+
}
123+
124+
[Fact]
125+
public void Success_GenerateCurlInString_When_HttpContent_Is_Null_For_PostMethod()
126+
{
127+
// Arrange
128+
var requestUri = "api/test";
129+
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri);
130+
httpRequestMessage.Headers.Add("Authorization", "Bearer 4797c126-3f8a-454a-aff1-96c0220dae61");
131+
132+
using var httpClient = new HttpClient();
133+
var baseAddress = new Uri("http://localhost:1213/v1/");
134+
135+
// Act
136+
string curlResult = httpRequestMessage.GenerateCurlInString(baseAddress);
137+
138+
// Assert
139+
curlResult.Should().NotBeNullOrEmpty();
140+
curlResult.Should().StartWith("curl -X POST");
141+
curlResult.Trim().Should().BeEquivalentTo(@"curl -X POST 'http://localhost:1213/v1/api/test' -H 'Authorization: Bearer 4797c126-3f8a-454a-aff1-96c0220dae61' -d ''");
142+
}
143+
144+
[Fact]
145+
public void Success_GenerateCurlInString_With_Multiple_Value_For_A_Header_PostMethod()
146+
{
147+
// Arrange
148+
string requestBody = /*lang=json,strict*/ @"{""name"":""sara"",""requestId"":10001001,""amount"":20000}";
149+
150+
var requestUri = "api/test";
151+
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri) { Content = new StringContent(requestBody, Encoding.UTF8, MediaTypeNames.Application.Json) };
152+
httpRequestMessage.Headers.Add("Authorization", "Bearer 4797c126-3f8a-454a-aff1-96c0220dae61");
153+
154+
List<string?> headerValues = ["_ga=GA1.1.41226618.1701506283", "mywebsite-sp=cbf42587-7ec5-4179-aac5-cbc9ae6fbf05", "sp_ses.13cb=*"];
155+
httpRequestMessage.Headers.Add("cookie", headerValues);
156+
157+
var baseAddress = new Uri("http://localhost:1213/v1/");
158+
159+
// Act
160+
string curlResult = httpRequestMessage.GenerateCurlInString(baseAddress);
161+
162+
// Assert
163+
curlResult.Should().NotBeNullOrEmpty();
164+
curlResult.Should().StartWith("curl -X POST");
165+
curlResult.Trim().Should()
166+
.BeEquivalentTo(
167+
@"curl -X POST 'http://localhost:1213/v1/api/test' -H 'Authorization: Bearer 4797c126-3f8a-454a-aff1-96c0220dae61' -H 'Cookie: _ga=GA1.1.41226618.1701506283; mywebsite-sp=cbf42587-7ec5-4179-aac5-cbc9ae6fbf05; sp_ses.13cb=*' -H 'Content-Type: application/json; charset=utf-8' -d '{""name"":""sara"",""requestId"":10001001,""amount"":20000}'");
168+
}
169+
170+
#endregion
171+
172+
#region :: GenerateCurlInString For Get Method ::
173+
174+
[Fact]
175+
public void Success_GenerateCurlInString_For_GetMethod()
176+
{
177+
// Arrange
178+
var requestUri = "api/test";
179+
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, requestUri) { Content = new StringContent(string.Empty, Encoding.UTF8, MediaTypeNames.Application.Json) };
180+
httpRequestMessage.Headers.Add("Authorization", "Bearer 703438f3-16ad-4ba5-b923-8f72cd0f2db9");
181+
182+
var baseAddress = new Uri("http://localhost:1213/v1/");
183+
184+
// Act
185+
string curlResult = httpRequestMessage.GenerateCurlInString(baseAddress);
186+
187+
// Assert
188+
curlResult.Should().NotBeNullOrEmpty();
189+
curlResult.Should().StartWith("curl");
190+
curlResult.Trim().Should()
191+
.BeEquivalentTo(
192+
@"curl 'http://localhost:1213/v1/api/test' -H 'Authorization: Bearer 703438f3-16ad-4ba5-b923-8f72cd0f2db9' -H 'Content-Type: application/json; charset=utf-8'");
193+
}
194+
195+
[Fact]
196+
public void Success_GenerateCurlInString_With_QueryString_For_GetMethod()
197+
{
198+
// Arrange
199+
var queryString = new Dictionary<string, string> { { "id", "12" } };
200+
var requestUri = QueryHelpers.AddQueryString("api/test", queryString!);
201+
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, requestUri) { Content = new StringContent(string.Empty, Encoding.UTF8, MediaTypeNames.Application.Json) };
202+
httpRequestMessage.Headers.Add("Authorization", "Bearer 703438f3-16ad-4ba5-b923-8f72cd0f2db9");
203+
204+
var baseAddress = new Uri("http://localhost:1213/v1/");
205+
206+
// Act
207+
string curlResult = httpRequestMessage.GenerateCurlInString(baseAddress);
208+
209+
// Assert
210+
curlResult.Should().NotBeNullOrEmpty();
211+
curlResult.Should().StartWith("curl");
212+
curlResult.Trim().Should()
213+
.BeEquivalentTo(
214+
@"curl 'http://localhost:1213/v1/api/test?id=12' -H 'Authorization: Bearer 703438f3-16ad-4ba5-b923-8f72cd0f2db9' -H 'Content-Type: application/json; charset=utf-8'");
215+
}
216+
217+
[Fact]
218+
public void Success_GenerateCurlInString_When_RequestUri_Is_Null_For_GetMethod()
219+
{
220+
// Arrange
221+
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, string.Empty) { Content = new StringContent(string.Empty, Encoding.UTF8, MediaTypeNames.Application.Json) };
222+
httpRequestMessage.Headers.Add("Authorization", "Bearer 703438f3-16ad-4ba5-b923-8f72cd0f2db9");
223+
224+
var baseAddress = new Uri("http://localhost:1213/v1/");
225+
226+
// Act
227+
string curlResult = httpRequestMessage.GenerateCurlInString(baseAddress);
228+
229+
// Assert
230+
curlResult.Should().NotBeNullOrEmpty();
231+
curlResult.Should().StartWith("curl");
232+
curlResult.Trim().Should()
233+
.BeEquivalentTo(@"curl 'http://localhost:1213/v1/' -H 'Authorization: Bearer 703438f3-16ad-4ba5-b923-8f72cd0f2db9' -H 'Content-Type: application/json; charset=utf-8'");
234+
}
235+
236+
[Fact]
237+
public void Success_GenerateCurlInString_With_Multiple_Value_For_A_Header_GetMethod()
238+
{
239+
// Arrange
240+
var requestUri = "api/test";
241+
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, requestUri) { Content = new StringContent(string.Empty, Encoding.UTF8, MediaTypeNames.Application.Json) };
242+
httpRequestMessage.Headers.Add("Authorization", "Bearer 703438f3-16ad-4ba5-b923-8f72cd0f2db9");
243+
244+
List<string?> headerValues = ["_ga=GA1.1.41226618.1701506283", "mywebsite-sp=cbf42587-7ec5-4179-aac5-cbc9ae6fbf05", "sp_ses.13cb=*"];
245+
httpRequestMessage.Headers.Add("cookie", headerValues);
246+
247+
var baseAddress = new Uri("http://localhost:1213/v1/");
248+
249+
// Act
250+
string curlResult = httpRequestMessage.GenerateCurlInString(baseAddress);
251+
252+
// Assert
253+
curlResult.Should().NotBeNullOrEmpty();
254+
curlResult.Should().StartWith("curl");
255+
curlResult.Trim().Should()
256+
.BeEquivalentTo(
257+
@"curl 'http://localhost:1213/v1/api/test' -H 'Authorization: Bearer 703438f3-16ad-4ba5-b923-8f72cd0f2db9' -H 'Cookie: _ga=GA1.1.41226618.1701506283; mywebsite-sp=cbf42587-7ec5-4179-aac5-cbc9ae6fbf05; sp_ses.13cb=*' -H 'Content-Type: application/json; charset=utf-8'");
258+
}
259+
260+
#endregion
261+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
8+
<IsPackable>false</IsPackable>
9+
<IsTestProject>true</IsTestProject>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="FluentAssertions.Json" Version="6.1.0" />
14+
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="8.0.0" />
15+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
16+
<PackageReference Include="xunit" Version="2.6.3" />
17+
<PackageReference Include="xunit.core" Version="2.6.3" />
18+
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.5">
19+
<PrivateAssets>all</PrivateAssets>
20+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
21+
</PackageReference>
22+
</ItemGroup>
23+
24+
<ItemGroup>
25+
<ProjectReference Include="..\src\HttpClientToCurl\HttpClientToCurl.csproj" />
26+
</ItemGroup>
27+
28+
<ItemGroup>
29+
<Using Include="Xunit" />
30+
</ItemGroup>
31+
32+
</Project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Net.Mime;
2+
using System.Text;
3+
using HttpClientToCurl.Builder;
4+
5+
namespace HttpRequestMessageToCurlGeneratorTest.UnitTest.MediaTypes.Json;
6+
7+
public class FailedCurlGeneratorTests
8+
{
9+
[Fact]
10+
public void GenerateCurl_When_HttpMethod_Is_Invalid()
11+
{
12+
// Arrange
13+
string requestBody = /*lang=json,strict*/ @"{""name"":""russel"",""requestId"":10001004,""amount"":50000}";
14+
15+
var requestUri = "api/test";
16+
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Trace, requestUri)
17+
{
18+
Content = new StringContent(requestBody, Encoding.UTF8, MediaTypeNames.Application.Json)
19+
};
20+
httpRequestMessage.Headers.Add("Authorization", "Bearer 4797c126-3f8a-454a-aff1-96c0220dae61");
21+
22+
var baseAddress = new Uri("http://localhost:1213/v1/");
23+
24+
// Act - Assert
25+
Assert.Throws<NotSupportedException>(() => Generator.GenerateCurl(
26+
httpRequestMessage,
27+
baseAddress,
28+
null));
29+
}
30+
}

0 commit comments

Comments
 (0)