Skip to content

Commit 8adcd93

Browse files
aminamin
authored andcommitted
show request body value when an error in parsing occurs.
1 parent 9fd65cb commit 8adcd93

File tree

7 files changed

+96
-76
lines changed

7 files changed

+96
-76
lines changed

src/HttpClientToCurl/Builder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ string CheckAndAddWarningMessageForIncorrectSlash(bool hasOnBaseUrl, bool hasOnR
8181
internal static StringBuilder AddHeaders(this StringBuilder stringBuilder, HttpClient httpClient, HttpRequestMessage httpRequestMessage, bool needAddDefaultHeaders = true)
8282
{
8383
bool hasHeader = false;
84-
84+
8585
if (needAddDefaultHeaders && httpClient.DefaultRequestHeaders.Any())
8686
{
8787
var defaultHeaders = httpClient.DefaultRequestHeaders.Where(dh => dh.Key != HttpRequestHeader.ContentLength.ToString());
@@ -138,7 +138,7 @@ internal static StringBuilder AddBody(this StringBuilder stringBuilder, HttpCont
138138
string body = content?.ReadAsStringAsync().GetAwaiter().GetResult();
139139

140140
if (content is not null && !string.IsNullOrWhiteSpace(body) && !Helpers.IsValidBody(body, contentType))
141-
throw new JsonException($"exception in parsing body {contentType}!");
141+
throw new JsonException($"exception in parsing request body {contentType}!{Environment.NewLine}request body:{Environment.NewLine}{body}");
142142

143143
if (contentType == "application/x-www-form-urlencoded")
144144
_AddFormUrlEncodedContentBody(stringBuilder, body);

src/HttpClientToCurl/Generator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ public static string GenerateCurl(HttpClient httpClient, HttpRequestMessage http
2626
}
2727
catch (Exception exception)
2828
{
29-
script = $"GenerateCurlError => {exception.Message}.{exception.InnerException}";
29+
script = $"GenerateCurlError => {exception.Message} {exception.InnerException}";
30+
script = $"GenerateCurlError => {exception.Message} {exception.InnerException}";
3031
}
3132

3233
return script;

src/HttpClientToCurlGeneratorTest/FunctionalTest/SuccessScenariosTests.cs

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ public void Success_GenerateCurlInString_For_PostMethod()
3030
string curlResult = httpClient.GenerateCurlInString(httpRequestMessage);
3131

3232
// Assert
33-
Assert.That(curlResult, Is.Not.Null);
34-
Assert.That(curlResult, Is.Not.Empty);
33+
Assert.That(!string.IsNullOrWhiteSpace(curlResult?.Trim()), Is.True);
3534
Assert.That(curlResult, Does.StartWith("curl -X POST"));
3635
Assert.That(curlResult?.Trim(),
3736
Is.EqualTo(
@@ -55,8 +54,7 @@ public void GenerateCurl_When_Set_RequestUri_Inside_HttpRequestMessage_For_PostM
5554
string curlResult = httpClient.GenerateCurlInString(httpRequestMessage);
5655

5756
// Assert
58-
Assert.That(curlResult, Is.Not.Null);
59-
Assert.That(curlResult, Is.Not.Empty);
57+
Assert.That(!string.IsNullOrWhiteSpace(curlResult?.Trim()), Is.True);
6058
Assert.That(curlResult, Does.StartWith("curl -X POST"));
6159
Assert.That(curlResult?.Trim(),
6260
Is.EqualTo(
@@ -80,8 +78,7 @@ public void Success_GenerateCurlInString_When_RequestUri_Is_Null_For_PostMethod(
8078
string curlResult = httpClient.GenerateCurlInString(httpRequestMessage);
8179

8280
// Assert
83-
Assert.That(curlResult, Is.Not.Null);
84-
Assert.That(curlResult, Is.Not.Empty);
81+
Assert.That(!string.IsNullOrWhiteSpace(curlResult?.Trim()), Is.True);
8582
Assert.That(curlResult, Does.StartWith("curl -X POST"));
8683
Assert.That(curlResult?.Trim(),
8784
Is.EqualTo(
@@ -104,8 +101,7 @@ public void Success_GenerateCurlInString_When_RequestBody_Is_Null_For_PostMethod
104101
string curlResult = httpClient.GenerateCurlInString(httpRequestMessage);
105102

106103
// Assert
107-
Assert.That(curlResult, Is.Not.Null);
108-
Assert.That(curlResult, Is.Not.Empty);
104+
Assert.That(!string.IsNullOrWhiteSpace(curlResult?.Trim()), Is.True);
109105
Assert.That(curlResult, Does.StartWith("curl -X POST"));
110106
Assert.That(curlResult?.Trim(),
111107
Is.EqualTo(
@@ -127,8 +123,7 @@ public void Success_GenerateCurlInString_When_HttpContent_Is_Null_For_PostMethod
127123
string curlResult = httpClient.GenerateCurlInString(httpRequestMessage);
128124

129125
// Assert
130-
Assert.That(curlResult, Is.Not.Null);
131-
Assert.That(curlResult, Is.Not.Empty);
126+
Assert.That(!string.IsNullOrWhiteSpace(curlResult?.Trim()), Is.True);
132127
Assert.That(curlResult, Does.StartWith("curl -X POST"));
133128
Assert.That(curlResult?.Trim(),
134129
Is.EqualTo(
@@ -160,8 +155,7 @@ public void Success_GenerateCurlInString_Without_HttpRequestMessage_For_PostMeth
160155
HttpMethod.Post, requestUri, httpRequestHeaders, jsonContent);
161156

162157
// Assert
163-
Assert.That(curlResult, Is.Not.Null);
164-
Assert.That(curlResult, Is.Not.Empty);
158+
Assert.That(!string.IsNullOrWhiteSpace(curlResult?.Trim()), Is.True);
165159
Assert.That(curlResult, Does.StartWith("curl -X POST"));
166160
Assert.That(curlResult?.Trim(),
167161
Is.EqualTo(
@@ -184,8 +178,7 @@ public void Success_GenerateCurlInString_Without_HttpRequestMessage_And_Body_Is_
184178
HttpMethod.Post, requestUri, httpRequestHeaders);
185179

186180
// Assert
187-
Assert.That(curlResult, Is.Not.Null);
188-
Assert.That(curlResult, Is.Not.Empty);
181+
Assert.That(!string.IsNullOrWhiteSpace(curlResult?.Trim()), Is.True);
189182
Assert.That(curlResult, Does.StartWith("curl -X POST"));
190183
Assert.That(curlResult?.Trim(),
191184
Is.EqualTo(
@@ -216,8 +209,7 @@ public void Success_GenerateCurlInString_Without_HttpRequestMessage_And_RequestU
216209
httpMethod: HttpMethod.Post, requestHeaders: httpRequestHeaders, requestBody: jsonContent);
217210

218211
// Assert
219-
Assert.That(curlResult, Is.Not.Null);
220-
Assert.That(curlResult, Is.Not.Empty);
212+
Assert.That(!string.IsNullOrWhiteSpace(curlResult?.Trim()), Is.True);
221213
Assert.That(curlResult, Does.StartWith("curl -X POST"));
222214
Assert.That(curlResult?.Trim(),
223215
Is.EqualTo(
@@ -247,8 +239,7 @@ public void Success_GenerateCurlInString_Without_HttpRequestMessage_And_HttpRequ
247239
httpMethod: HttpMethod.Post, requestUri: requestUri, requestBody: jsonContent);
248240

249241
// Assert
250-
Assert.That(curlResult, Is.Not.Null);
251-
Assert.That(curlResult, Is.Not.Empty);
242+
Assert.That(!string.IsNullOrWhiteSpace(curlResult?.Trim()), Is.True);
252243
Assert.That(curlResult, Does.StartWith("curl -X POST"));
253244
Assert.That(curlResult?.Trim(),
254245
Is.EqualTo(
@@ -275,11 +266,11 @@ public void Success_GenerateCurlInString_For_GetMethod()
275266
string curlResult = httpClient.GenerateCurlInString(httpRequestMessage);
276267

277268
// Assert
278-
Assert.That(curlResult, Is.Not.Null);
279-
Assert.That(curlResult, Is.Not.Empty);
269+
Assert.That(!string.IsNullOrWhiteSpace(curlResult?.Trim()), Is.True);
280270
Assert.That(curlResult, Does.StartWith("curl"));
281271
Assert.That(curlResult?.Trim(),
282-
Is.EqualTo(@"curl http://localhost:1213/v1/api/test -H 'Authorization: Bearer 703438f3-16ad-4ba5-b923-8f72cd0f2db9' -H 'Content-Type: application/json; charset=utf-8'"));
272+
Is.EqualTo(
273+
@"curl http://localhost:1213/v1/api/test -H 'Authorization: Bearer 703438f3-16ad-4ba5-b923-8f72cd0f2db9' -H 'Content-Type: application/json; charset=utf-8'"));
283274
}
284275

285276
[Theory]
@@ -302,11 +293,11 @@ public void Success_GenerateCurlInString_With_QueryString_For_GetMethod()
302293
string curlResult = httpClient.GenerateCurlInString(httpRequestMessage);
303294

304295
// Assert
305-
Assert.That(curlResult, Is.Not.Null);
306-
Assert.That(curlResult, Is.Not.Empty);
296+
Assert.That(!string.IsNullOrWhiteSpace(curlResult?.Trim()), Is.True);
307297
Assert.That(curlResult, Does.StartWith("curl"));
308298
Assert.That(curlResult?.Trim(),
309-
Is.EqualTo(@"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'"));
299+
Is.EqualTo(
300+
@"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'"));
310301
}
311302

312303
[Theory]
@@ -324,8 +315,7 @@ public void Success_GenerateCurlInString_When_RequestUri_Is_Null_For_GetMethod()
324315
string curlResult = httpClient.GenerateCurlInString(httpRequestMessage);
325316

326317
// Assert
327-
Assert.That(curlResult, Is.Not.Null);
328-
Assert.That(curlResult, Is.Not.Empty);
318+
Assert.That(!string.IsNullOrWhiteSpace(curlResult?.Trim()), Is.True);
329319
Assert.That(curlResult, Does.StartWith("curl"));
330320
Assert.That(curlResult?.Trim(),
331321
Is.EqualTo(@"curl http://localhost:1213/v1 -H 'Authorization: Bearer 703438f3-16ad-4ba5-b923-8f72cd0f2db9' -H 'Content-Type: application/json; charset=utf-8'"));
@@ -347,8 +337,7 @@ public void Success_GenerateCurlInString_Without_HttpRequestMessage_For_GetMetho
347337
HttpMethod.Get, requestUri, httpRequestHeaders);
348338

349339
// Assert
350-
Assert.That(curlResult, Is.Not.Null);
351-
Assert.That(curlResult, Is.Not.Empty);
340+
Assert.That(!string.IsNullOrWhiteSpace(curlResult?.Trim()), Is.True);
352341
Assert.That(curlResult, Does.StartWith("curl"));
353342
Assert.That(curlResult?.Trim(),
354343
Is.EqualTo(
@@ -370,8 +359,7 @@ public void Success_GenerateCurlInString_Without_HttpRequestMessage_And_RequestU
370359
httpMethod: HttpMethod.Get, requestHeaders: httpRequestHeaders);
371360

372361
// Assert
373-
Assert.That(curlResult, Is.Not.Null);
374-
Assert.That(curlResult, Is.Not.Empty);
362+
Assert.That(!string.IsNullOrWhiteSpace(curlResult?.Trim()), Is.True);
375363
Assert.That(curlResult, Does.StartWith("curl"));
376364
Assert.That(curlResult?.Trim(),
377365
Is.EqualTo(
@@ -392,8 +380,7 @@ public void Success_GenerateCurlInString_Without_HttpRequestMessage_And_HttpRequ
392380
HttpMethod.Get, requestUri);
393381

394382
// Assert
395-
Assert.That(curlResult, Is.Not.Null);
396-
Assert.That(curlResult, Is.Not.Empty);
383+
Assert.That(!string.IsNullOrWhiteSpace(curlResult?.Trim()), Is.True);
397384
Assert.That(curlResult, Does.StartWith("curl"));
398385
Assert.That(curlResult?.Trim(),
399386
Is.EqualTo(

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@ public void GenerateCurl_When_Invalid_HttpMethod()
3030
true);
3131

3232
// Assert
33-
Assert.That(script, Is.Not.Null);
34-
Assert.That(script, Is.Not.Empty);
33+
Assert.That(!string.IsNullOrWhiteSpace(script?.Trim()), Is.True);
3534
Assert.That(script, Does.StartWith("GenerateCurlError"));
36-
Assert.That(script?.Trim(), Is.EqualTo($"GenerateCurlError => invalid HttpMethod: {httpRequestMessage.Method.Method}!."));
35+
Assert.That(script?.Trim(), Is.EqualTo($"GenerateCurlError => invalid HttpMethod: {httpRequestMessage.Method.Method}!"));
3736
}
3837

3938
[Theory]
@@ -57,10 +56,11 @@ public void GenerateCurl_When_Invalid_JsonBody()
5756
true);
5857

5958
// Assert
60-
Assert.That(script, Is.Not.Null);
61-
Assert.That(script, Is.Not.Empty);
59+
Assert.That(!string.IsNullOrWhiteSpace(script?.Trim()), Is.True);
6260
Assert.That(script, Does.StartWith("GenerateCurlError"));
63-
Assert.That(script?.Trim(), Is.EqualTo("GenerateCurlError => exception in parsing body application/json!."));
61+
Assert.That(script?.Trim(), Is.EqualTo(@"GenerateCurlError => exception in parsing request body application/json!
62+
request body:
63+
""name"":""steven"",""requestId"":10001005,""amount"":60000"));
6464
}
6565

6666
[Theory]
@@ -84,10 +84,9 @@ public void GenerateCurl_When_Invalid_BaseUrl()
8484
true);
8585

8686
// Assert
87-
Assert.That(script, Is.Not.Null);
88-
Assert.That(script, Is.Not.Empty);
87+
Assert.That(!string.IsNullOrWhiteSpace(script?.Trim()), Is.True);
8988
Assert.That(script, Does.StartWith("GenerateCurlError"));
90-
Assert.That(script?.Trim(), Is.EqualTo("GenerateCurlError => baseUrl argument is null or empty!."));
89+
Assert.That(script?.Trim(), Is.EqualTo("GenerateCurlError => baseUrl argument is null or empty!"));
9190
}
9291

9392
#endregion

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

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ public void GenerateCurl_For_PostMethod()
3131
true);
3232

3333
// Assert
34-
Assert.That(script, Is.Not.Null);
35-
Assert.That(script, Is.Not.Empty);
34+
Assert.That(!string.IsNullOrWhiteSpace(script?.Trim()), Is.True);
3635
Assert.That(script, Does.StartWith("curl -X POST"));
3736
Assert.That(script?.Trim(),
3837
Is.EqualTo(
@@ -61,8 +60,7 @@ public void GenerateCurl_With_ContentLength_For_PostMethod()
6160
true);
6261

6362
// Assert
64-
Assert.That(script, Is.Not.Null);
65-
Assert.That(script, Is.Not.Empty);
63+
Assert.That(!string.IsNullOrWhiteSpace(script?.Trim()), Is.True);
6664
Assert.That(script, Does.StartWith("curl -X POST"));
6765
Assert.That(script?.Trim(),
6866
Is.EqualTo(
@@ -94,8 +92,7 @@ public void GenerateCurl_With_QueryString_For_PostMethod()
9492
true);
9593

9694
// Assert
97-
Assert.That(script, Is.Not.Null);
98-
Assert.That(script, Is.Not.Empty);
95+
Assert.That(!string.IsNullOrWhiteSpace(script?.Trim()), Is.True);
9996
Assert.That(script, Does.StartWith("curl -X POST"));
10097
Assert.That(script?.Trim(),
10198
Is.EqualTo(
@@ -128,8 +125,7 @@ public void GenerateCurl_UrlEncoded_For_PostMethod()
128125
true);
129126

130127
// Assert
131-
Assert.That(script, Is.Not.Null);
132-
Assert.That(script, Is.Not.Empty);
128+
Assert.That(!string.IsNullOrWhiteSpace(script?.Trim()), Is.True);
133129
Assert.That(script?.Trim(),
134130
Is.EqualTo(
135131
@"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}'"));
@@ -154,8 +150,7 @@ public void GenerateCurl_Without_RequestBody_For_PostMethod()
154150
true);
155151

156152
// Assert
157-
Assert.That(script, Is.Not.Null);
158-
Assert.That(script, Is.Not.Empty);
153+
Assert.That(!string.IsNullOrWhiteSpace(script?.Trim()), Is.True);
159154
Assert.That(script, Does.StartWith("curl -X POST"));
160155
Assert.That(script?.Trim(),
161156
Is.EqualTo(
@@ -181,8 +176,7 @@ public void GenerateCurl_Without_Content_For_PostMethod()
181176
true);
182177

183178
// Assert
184-
Assert.That(script, Is.Not.Null);
185-
Assert.That(script, Is.Not.Empty);
179+
Assert.That(!string.IsNullOrWhiteSpace(script?.Trim()), Is.True);
186180
Assert.That(script, Does.StartWith("curl -X POST"));
187181
Assert.That(script?.Trim(),
188182
Is.EqualTo(
@@ -210,8 +204,7 @@ public void GenerateCurl_Along_With_Warning_When_Has_Additional_Slash_For_PostMe
210204
true);
211205

212206
// Assert
213-
Assert.That(script, Is.Not.Null);
214-
Assert.That(script, Is.Not.Empty);
207+
Assert.That(!string.IsNullOrWhiteSpace(script?.Trim()), Is.True);
215208
Assert.That(script, Does.StartWith("# Warning"));
216209
Assert.That(script?.Trim(),
217210
Is.EqualTo(
@@ -240,8 +233,7 @@ public void GenerateCurl_Along_With_Warning_When_Has_Fewer_Slash_For_PostMethod(
240233
true);
241234

242235
// Assert
243-
Assert.That(script, Is.Not.Null);
244-
Assert.That(script, Is.Not.Empty);
236+
Assert.That(!string.IsNullOrWhiteSpace(script?.Trim()), Is.True);
245237
Assert.That(script, Does.StartWith("# Warning"));
246238
Assert.That(script?.Trim(),
247239
Is.EqualTo(
@@ -272,8 +264,7 @@ public void GenerateCurl_For_GetMethod()
272264
true);
273265

274266
// Assert
275-
Assert.That(script, Is.Not.Null);
276-
Assert.That(script, Is.Not.Empty);
267+
Assert.That(!string.IsNullOrWhiteSpace(script?.Trim()), Is.True);
277268
Assert.That(script, Does.StartWith("curl"));
278269
Assert.That(script?.Trim(),
279270
Is.EqualTo(
@@ -303,8 +294,7 @@ public void GenerateCurl_With_QueryString_For_GetMethod()
303294
true);
304295

305296
// Assert
306-
Assert.That(script, Is.Not.Null);
307-
Assert.That(script, Is.Not.Empty);
297+
Assert.That(!string.IsNullOrWhiteSpace(script?.Trim()), Is.True);
308298
Assert.That(script, Does.StartWith("curl"));
309299
Assert.That(script?.Trim(),
310300
Is.EqualTo(
@@ -330,8 +320,7 @@ public void GenerateCurl_Along_With_Warning_When_Has_Additional_Slash_For_GetMet
330320
true);
331321

332322
// Assert
333-
Assert.That(script, Is.Not.Null);
334-
Assert.That(script, Is.Not.Empty);
323+
Assert.That(!string.IsNullOrWhiteSpace(script?.Trim()), Is.True);
335324
Assert.That(script, Does.StartWith("# Warning"));
336325
Assert.That(script?.Trim(),
337326
Is.EqualTo(@"# Warning: you must remove the Slash at the end of base url or at the first of the requestUri.
@@ -357,8 +346,7 @@ public void GenerateCurl_Along_With_Warning_When_Has_Fewer_Slash_For_GetMethod()
357346
true);
358347

359348
// Assert
360-
Assert.That(script, Is.Not.Null);
361-
Assert.That(script, Is.Not.Empty);
349+
Assert.That(!string.IsNullOrWhiteSpace(script?.Trim()), Is.True);
362350
Assert.That(script, Does.StartWith("# Warning"));
363351
Assert.That(script?.Trim(),
364352
Is.EqualTo(@"# Warning: you must add the Slash at the end of base url or at the first of the requestUri.
@@ -390,8 +378,7 @@ public void GenerateCurl_For_PutMethod()
390378
true);
391379

392380
// Assert
393-
Assert.That(script, Is.Not.Null);
394-
Assert.That(script, Is.Not.Empty);
381+
Assert.That(!string.IsNullOrWhiteSpace(script?.Trim()), Is.True);
395382
Assert.That(script, Does.StartWith("curl -X PUT"));
396383
Assert.That(script?.Trim(),
397384
Is.EqualTo(
@@ -423,8 +410,7 @@ public void GenerateCurl_For_PatchMethod()
423410
true);
424411

425412
// Assert
426-
Assert.That(script, Is.Not.Null);
427-
Assert.That(script, Is.Not.Empty);
413+
Assert.That(!string.IsNullOrWhiteSpace(script?.Trim()), Is.True);
428414
Assert.That(script, Does.StartWith("curl -X PATCH"));
429415
Assert.That(script?.Trim(),
430416
Is.EqualTo(
@@ -455,8 +441,7 @@ public void GenerateCurl_For_DeleteMethod()
455441
true);
456442

457443
// Assert
458-
Assert.That(script, Is.Not.Null);
459-
Assert.That(script, Is.Not.Empty);
444+
Assert.That(!string.IsNullOrWhiteSpace(script?.Trim()), Is.True);
460445
Assert.That(script, Does.StartWith("curl -X DELETE"));
461446
Assert.That(script?.Trim(),
462447
Is.EqualTo(

0 commit comments

Comments
 (0)