Skip to content

Commit 14324f6

Browse files
Merge pull request #8 from Sabermotamedi/change/add-fluentassertion-to-json-unit-test
Change/add fluentassertion to json unit test
2 parents 5bf51f0 + f4281b5 commit 14324f6

File tree

3 files changed

+70
-114
lines changed

3 files changed

+70
-114
lines changed

src/HttpClientToCurlGeneratorTest/UnitTest/MediaTypes/Json/FailedCurlGeneratorTests.cs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
using System.Net.Mime;
2-
using System.Text;
1+
using FluentAssertions;
32
using HttpClientToCurl;
43
using NUnit.Framework;
4+
using System.Net.Mime;
5+
using System.Text;
56

67
namespace HttpClientToCurlGeneratorTest.UnitTest.MediaTypes.Json;
78

89
public class FailedCurlGeneratorTests
910
{
10-
#region :: GenerateCurl For Post Method ::
11-
1211
[Theory]
1312
public void GenerateCurl_When_Invalid_HttpMethod()
1413
{
@@ -30,9 +29,9 @@ public void GenerateCurl_When_Invalid_HttpMethod()
3029
true);
3130

3231
// Assert
33-
Assert.That(!string.IsNullOrWhiteSpace(script?.Trim()), Is.True);
34-
Assert.That(script, Does.StartWith("GenerateCurlError"));
35-
Assert.That(script?.Trim(), Is.EqualTo($"GenerateCurlError => invalid HttpMethod: {httpRequestMessage.Method.Method}!"));
32+
script.Should().NotBeNullOrEmpty();
33+
script.Should().StartWith("GenerateCurlError");
34+
script.Trim().Should().BeEquivalentTo($"GenerateCurlError => invalid HttpMethod: {httpRequestMessage.Method.Method}!");
3635
}
3736

3837
[Theory]
@@ -56,11 +55,11 @@ public void GenerateCurl_When_Invalid_JsonBody()
5655
true);
5756

5857
// Assert
59-
Assert.That(!string.IsNullOrWhiteSpace(script?.Trim()), Is.True);
60-
Assert.That(script, Does.StartWith("GenerateCurlError"));
61-
Assert.That(script?.Trim(), Is.EqualTo(@"GenerateCurlError => exception in parsing request body application/json!
58+
script.Should().NotBeNullOrEmpty();
59+
script.Should().StartWith("GenerateCurlError");
60+
script.Trim().Should().BeEquivalentTo(@"GenerateCurlError => exception in parsing request body application/json!
6261
request body:
63-
""name"":""steven"",""requestId"":10001005,""amount"":60000"));
62+
""name"":""steven"",""requestId"":10001005,""amount"":60000");
6463
}
6564

6665
[Theory]
@@ -84,10 +83,10 @@ public void GenerateCurl_When_Invalid_BaseUrl()
8483
true);
8584

8685
// Assert
87-
Assert.That(!string.IsNullOrWhiteSpace(script?.Trim()), Is.True);
88-
Assert.That(script, Does.StartWith("GenerateCurlError"));
89-
Assert.That(script?.Trim(), Is.EqualTo("GenerateCurlError => baseUrl argument is null or empty!"));
86+
script.Should().NotBeNullOrEmpty();
87+
script.Should().StartWith("GenerateCurlError");
88+
script.Trim().Should().BeEquivalentTo("GenerateCurlError => baseUrl argument is null or empty!");
9089
}
9190

92-
#endregion
91+
9392
}

src/HttpClientToCurlGeneratorTest/UnitTest/MediaTypes/Json/SuccessCurlGeneratorTests.cs

Lines changed: 55 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
using System.Net.Mime;
2-
using System.Text;
1+
using FluentAssertions;
32
using HttpClientToCurl;
43
using Microsoft.AspNetCore.WebUtilities;
54
using NUnit.Framework;
5+
using System.Net.Mime;
6+
using System.Text;
67

78
namespace HttpClientToCurlGeneratorTest.UnitTest.MediaTypes.Json;
89

910
public class SuccessCurlGeneratorTests
1011
{
11-
#region :: GenerateCurl For Post Method ::
12+
1213

1314
[Theory]
1415
public void GenerateCurl_For_PostMethod()
@@ -31,11 +32,9 @@ public void GenerateCurl_For_PostMethod()
3132
true);
3233

3334
// Assert
34-
Assert.That(!string.IsNullOrWhiteSpace(script?.Trim()), Is.True);
35-
Assert.That(script, Does.StartWith("curl -X POST"));
36-
Assert.That(script?.Trim(),
37-
Is.EqualTo(
38-
@"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}'"));
35+
script.Should().NotBeNullOrEmpty();
36+
script.Should().StartWith("curl -X POST");
37+
script.Trim().Should().BeEquivalentTo(@"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}'");
3938
}
4039

4140
[Theory]
@@ -60,11 +59,9 @@ public void GenerateCurl_With_ContentLength_For_PostMethod()
6059
true);
6160

6261
// Assert
63-
Assert.That(!string.IsNullOrWhiteSpace(script?.Trim()), Is.True);
64-
Assert.That(script, Does.StartWith("curl -X POST"));
65-
Assert.That(script?.Trim(),
66-
Is.EqualTo(
67-
@"curl -X POST http://localhost:1213/v1/api/test -H 'Authorization: Bearer f69406a4-6b62-4734-a8dc-158f0fc308ab' -H 'Content-Type: application/json; charset=utf-8' -d '{""name"":""sara"",""requestId"":10001001,""amount"":20000}'"));
62+
script.Should().NotBeNullOrEmpty();
63+
script.Should().StartWith("curl -X POST");
64+
script.Trim().Should().BeEquivalentTo(@"curl -X POST http://localhost:1213/v1/api/test -H 'Authorization: Bearer f69406a4-6b62-4734-a8dc-158f0fc308ab' -H 'Content-Type: application/json; charset=utf-8' -d '{""name"":""sara"",""requestId"":10001001,""amount"":20000}'");
6865
}
6966

7067
[Theory]
@@ -92,11 +89,10 @@ public void GenerateCurl_With_QueryString_For_PostMethod()
9289
true);
9390

9491
// Assert
95-
Assert.That(!string.IsNullOrWhiteSpace(script?.Trim()), Is.True);
96-
Assert.That(script, Does.StartWith("curl -X POST"));
97-
Assert.That(script?.Trim(),
98-
Is.EqualTo(
99-
@"curl -X POST http://localhost:1213/v1/api/test?id=12 -H 'Authorization: Bearer 4797c126-3f8a-454a-aff1-96c0220dae61' -H 'Content-Type: application/json; charset=utf-8' -d '{""name"":""amin"",""requestId"":10001000,""amount"":10000}'"));
92+
93+
script.Should().NotBeNullOrEmpty();
94+
script.Should().StartWith("curl -X POST");
95+
script.Trim().Should().BeEquivalentTo(@"curl -X POST http://localhost:1213/v1/api/test?id=12 -H 'Authorization: Bearer 4797c126-3f8a-454a-aff1-96c0220dae61' -H 'Content-Type: application/json; charset=utf-8' -d '{""name"":""amin"",""requestId"":10001000,""amount"":10000}'");
10096
}
10197

10298
[Theory]
@@ -125,10 +121,9 @@ public void GenerateCurl_UrlEncoded_For_PostMethod()
125121
true);
126122

127123
// Assert
128-
Assert.That(!string.IsNullOrWhiteSpace(script?.Trim()), Is.True);
129-
Assert.That(script?.Trim(),
130-
Is.EqualTo(
131-
@"curl -X POST http://localhost:1213/v1/api/test -H 'Authorization: Bearer 4797c126-3f8a-454a-aff1-96c0220dae61' -H 'Content-Type: application/x-www-form-urlencoded' -d 'session=703438f3-16ad-4ba5-b923-8f72cd0f2db9' -d 'payload={""name"":""justin"",""requestId"":10001026,""amount"":26000}'"));
124+
script.Should().NotBeNullOrEmpty();
125+
script.Should().StartWith("curl -X POST");
126+
script.Trim().Should().BeEquivalentTo(@"curl -X POST http://localhost:1213/v1/api/test -H 'Authorization: Bearer 4797c126-3f8a-454a-aff1-96c0220dae61' -H 'Content-Type: application/x-www-form-urlencoded' -d 'session=703438f3-16ad-4ba5-b923-8f72cd0f2db9' -d 'payload={""name"":""justin"",""requestId"":10001026,""amount"":26000}'");
132127
}
133128

134129
[Theory]
@@ -150,11 +145,9 @@ public void GenerateCurl_Without_RequestBody_For_PostMethod()
150145
true);
151146

152147
// Assert
153-
Assert.That(!string.IsNullOrWhiteSpace(script?.Trim()), Is.True);
154-
Assert.That(script, Does.StartWith("curl -X POST"));
155-
Assert.That(script?.Trim(),
156-
Is.EqualTo(
157-
@"curl -X POST http://localhost:1213/v1/api/test -H 'Authorization: Bearer c332e9a1-1e0e-44c2-b819-b0e7e8ff7d45' -H 'Content-Type: application/json; charset=utf-8' -d ''"));
148+
script.Should().NotBeNullOrEmpty();
149+
script.Should().StartWith("curl -X POST");
150+
script.Trim().Should().BeEquivalentTo(@"curl -X POST http://localhost:1213/v1/api/test -H 'Authorization: Bearer c332e9a1-1e0e-44c2-b819-b0e7e8ff7d45' -H 'Content-Type: application/json; charset=utf-8' -d ''");
158151
}
159152

160153
[Theory]
@@ -176,11 +169,9 @@ public void GenerateCurl_Without_Content_For_PostMethod()
176169
true);
177170

178171
// Assert
179-
Assert.That(!string.IsNullOrWhiteSpace(script?.Trim()), Is.True);
180-
Assert.That(script, Does.StartWith("curl -X POST"));
181-
Assert.That(script?.Trim(),
182-
Is.EqualTo(
183-
@"curl -X POST http://localhost:1213/v1/api/test -H 'Authorization: Bearer 56bfa7a0-0541-4d71-9efc-8b28219ac31a' -d ''"));
172+
script.Should().NotBeNullOrEmpty();
173+
script.Should().StartWith("curl -X POST");
174+
script.Trim().Should().BeEquivalentTo(@"curl -X POST http://localhost:1213/v1/api/test -H 'Authorization: Bearer 56bfa7a0-0541-4d71-9efc-8b28219ac31a' -d ''");
184175
}
185176

186177
[Theory]
@@ -204,12 +195,10 @@ public void GenerateCurl_Along_With_Warning_When_Has_Additional_Slash_For_PostMe
204195
true);
205196

206197
// Assert
207-
Assert.That(!string.IsNullOrWhiteSpace(script?.Trim()), Is.True);
208-
Assert.That(script, Does.StartWith("# Warning"));
209-
Assert.That(script?.Trim(),
210-
Is.EqualTo(
211-
@"# Warning: you must remove the Slash at the end of base url or at the first of the requestUri.
212-
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}'"));
198+
script.Should().NotBeNullOrEmpty();
199+
script.Should().StartWith("# Warning");
200+
script.Trim().Should().BeEquivalentTo(@"# Warning: you must remove the Slash at the end of base url or at the first of the requestUri.
201+
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}'");
213202
}
214203

215204
[Theory]
@@ -233,18 +222,12 @@ public void GenerateCurl_Along_With_Warning_When_Has_Fewer_Slash_For_PostMethod(
233222
true);
234223

235224
// Assert
236-
Assert.That(!string.IsNullOrWhiteSpace(script?.Trim()), Is.True);
237-
Assert.That(script, Does.StartWith("# Warning"));
238-
Assert.That(script?.Trim(),
239-
Is.EqualTo(
240-
@"# Warning: you must add the Slash at the end of base url or at the first of the requestUri.
241-
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}'"));
225+
script.Should().NotBeNullOrEmpty();
226+
script.Should().StartWith("# Warning");
227+
script.Trim().Should().BeEquivalentTo(@"# Warning: you must add the Slash at the end of base url or at the first of the requestUri.
228+
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}'");
242229
}
243230

244-
#endregion
245-
246-
#region :: GenerateCurl For Get Method ::
247-
248231
[Theory]
249232
public void GenerateCurl_For_GetMethod()
250233
{
@@ -264,11 +247,9 @@ public void GenerateCurl_For_GetMethod()
264247
true);
265248

266249
// Assert
267-
Assert.That(!string.IsNullOrWhiteSpace(script?.Trim()), Is.True);
268-
Assert.That(script, Does.StartWith("curl"));
269-
Assert.That(script?.Trim(),
270-
Is.EqualTo(
271-
@"curl http://localhost:1213/v1/api/test -H 'Authorization: Bearer 703438f3-16ad-4ba5-b923-8f72cd0f2db9' -H 'Content-Type: application/json; charset=utf-8'"));
250+
script.Should().NotBeNullOrEmpty();
251+
script.Should().StartWith("curl");
252+
script.Trim().Should().BeEquivalentTo(@"curl http://localhost:1213/v1/api/test -H 'Authorization: Bearer 703438f3-16ad-4ba5-b923-8f72cd0f2db9' -H 'Content-Type: application/json; charset=utf-8'");
272253
}
273254

274255
[Theory]
@@ -294,11 +275,9 @@ public void GenerateCurl_With_QueryString_For_GetMethod()
294275
true);
295276

296277
// Assert
297-
Assert.That(!string.IsNullOrWhiteSpace(script?.Trim()), Is.True);
298-
Assert.That(script, Does.StartWith("curl"));
299-
Assert.That(script?.Trim(),
300-
Is.EqualTo(
301-
@"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'"));
278+
script.Should().NotBeNullOrEmpty();
279+
script.Should().StartWith("curl");
280+
script.Trim().Should().BeEquivalentTo(@"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'");
302281
}
303282

304283
[Theory]
@@ -320,11 +299,10 @@ public void GenerateCurl_Along_With_Warning_When_Has_Additional_Slash_For_GetMet
320299
true);
321300

322301
// Assert
323-
Assert.That(!string.IsNullOrWhiteSpace(script?.Trim()), Is.True);
324-
Assert.That(script, Does.StartWith("# Warning"));
325-
Assert.That(script?.Trim(),
326-
Is.EqualTo(@"# Warning: you must remove the Slash at the end of base url or at the first of the requestUri.
327-
curl http://localhost:1213/v1/api/test -H 'Authorization: Bearer 703438f3-16ad-4ba5-b923-8f72cd0f2db9' -H 'Content-Type: application/json; charset=utf-8'"));
302+
script.Should().NotBeNullOrEmpty();
303+
script.Should().StartWith("# Warning");
304+
script.Trim().Should().BeEquivalentTo(@"# Warning: you must remove the Slash at the end of base url or at the first of the requestUri.
305+
curl http://localhost:1213/v1/api/test -H 'Authorization: Bearer 703438f3-16ad-4ba5-b923-8f72cd0f2db9' -H 'Content-Type: application/json; charset=utf-8'");
328306
}
329307

330308
[Theory]
@@ -346,17 +324,12 @@ public void GenerateCurl_Along_With_Warning_When_Has_Fewer_Slash_For_GetMethod()
346324
true);
347325

348326
// Assert
349-
Assert.That(!string.IsNullOrWhiteSpace(script?.Trim()), Is.True);
350-
Assert.That(script, Does.StartWith("# Warning"));
351-
Assert.That(script?.Trim(),
352-
Is.EqualTo(@"# Warning: you must add the Slash at the end of base url or at the first of the requestUri.
353-
curl http://localhost:1213/v1/api/test -H 'Authorization: Bearer 703438f3-16ad-4ba5-b923-8f72cd0f2db9' -H 'Content-Type: application/json; charset=utf-8'"));
327+
script.Should().NotBeNullOrEmpty();
328+
script.Should().StartWith("# Warning");
329+
script.Trim().Should().BeEquivalentTo(@"# Warning: you must add the Slash at the end of base url or at the first of the requestUri.
330+
curl http://localhost:1213/v1/api/test -H 'Authorization: Bearer 703438f3-16ad-4ba5-b923-8f72cd0f2db9' -H 'Content-Type: application/json; charset=utf-8'");
354331
}
355332

356-
#endregion
357-
358-
#region :: GenerateCurl For Put Method ::
359-
360333
[Theory]
361334
public void GenerateCurl_For_PutMethod()
362335
{
@@ -378,17 +351,11 @@ public void GenerateCurl_For_PutMethod()
378351
true);
379352

380353
// Assert
381-
Assert.That(!string.IsNullOrWhiteSpace(script?.Trim()), Is.True);
382-
Assert.That(script, Does.StartWith("curl -X PUT"));
383-
Assert.That(script?.Trim(),
384-
Is.EqualTo(
385-
@"curl -X PUT http://localhost:1213/v1/api/test -H 'Authorization: Bearer 4797c126-3f8a-454a-aff1-96c0220dae61' -H 'Content-Type: application/json; charset=utf-8' -d '{""name"":""reza"",""requestId"":10001002,""amount"":30000}'"));
354+
script.Should().NotBeNullOrEmpty();
355+
script.Should().StartWith("curl -X PUT");
356+
script.Trim().Should().BeEquivalentTo(@"curl -X PUT http://localhost:1213/v1/api/test -H 'Authorization: Bearer 4797c126-3f8a-454a-aff1-96c0220dae61' -H 'Content-Type: application/json; charset=utf-8' -d '{""name"":""reza"",""requestId"":10001002,""amount"":30000}'");
386357
}
387358

388-
#endregion
389-
390-
#region :: GenerateCurl For Patch Method ::
391-
392359
[Theory]
393360
public void GenerateCurl_For_PatchMethod()
394361
{
@@ -410,17 +377,11 @@ public void GenerateCurl_For_PatchMethod()
410377
true);
411378

412379
// Assert
413-
Assert.That(!string.IsNullOrWhiteSpace(script?.Trim()), Is.True);
414-
Assert.That(script, Does.StartWith("curl -X PATCH"));
415-
Assert.That(script?.Trim(),
416-
Is.EqualTo(
417-
@"curl -X PATCH http://localhost:1213/v1/api/test -H 'Authorization: Bearer 4797c126-3f8a-454a-aff1-96c0220dae61' -H 'Content-Type: application/json; charset=utf-8' -d '{""name"":""hamed"",""requestId"":10001003,""amount"":40000}'"));
380+
script.Should().NotBeNullOrEmpty();
381+
script.Should().StartWith("curl -X PATCH");
382+
script.Trim().Should().BeEquivalentTo(@"curl -X PATCH http://localhost:1213/v1/api/test -H 'Authorization: Bearer 4797c126-3f8a-454a-aff1-96c0220dae61' -H 'Content-Type: application/json; charset=utf-8' -d '{""name"":""hamed"",""requestId"":10001003,""amount"":40000}'");
418383
}
419384

420-
#endregion
421-
422-
#region :: GenerateCurl For DeleteMethod ::
423-
424385
[Theory]
425386
public void GenerateCurl_For_DeleteMethod()
426387
{
@@ -441,12 +402,9 @@ public void GenerateCurl_For_DeleteMethod()
441402
true);
442403

443404
// Assert
444-
Assert.That(!string.IsNullOrWhiteSpace(script?.Trim()), Is.True);
445-
Assert.That(script, Does.StartWith("curl -X DELETE"));
446-
Assert.That(script?.Trim(),
447-
Is.EqualTo(
448-
@"curl -X DELETE http://localhost:1213/v1/api/test/12 -H 'Authorization: Bearer 703438f3-16ad-4ba5-b923-8f72cd0f2db9' -H 'Content-Type: application/json; charset=utf-8'"));
405+
script.Should().NotBeNullOrEmpty();
406+
script.Should().StartWith("curl -X DELETE");
407+
script.Trim().Should().BeEquivalentTo(@"curl -X DELETE http://localhost:1213/v1/api/test/12 -H 'Authorization: Bearer 703438f3-16ad-4ba5-b923-8f72cd0f2db9' -H 'Content-Type: application/json; charset=utf-8'");
449408
}
450409

451-
#endregion
452-
}
410+
}

src/HttpClientToCurlGeneratorTest/UnitTest/MediaTypes/Xml/SuccessCurlGeneratorTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ public void Get_Curl_Script_When_Input_XML_Is_Valid()
3535
true);
3636

3737
// Assert
38-
script.Should().NotBeNull();
39-
script.Should().NotBeEmpty();
38+
script.Should().NotBeNullOrEmpty();
4039
script.Should().StartWith("curl -X POST");
4140
script.Should().BeEquivalentTo(@"curl -X POST http://localhost:1213/v1/api/test -H 'Authorization: Bearer 4797c126-3f8a-454a-aff1-96c0220dae61' -H 'Content-Type: text/xml; charset=utf-8' -d '<?xml version = ""1.0"" encoding = ""UTF-8""?>
4241
<Order>

0 commit comments

Comments
 (0)