Skip to content

Commit 51ab526

Browse files
committed
fix: post request body
1 parent 68916bd commit 51ab526

34 files changed

+73
-48
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,8 @@ samples/Nexar.samples/.idea/.idea.Nexar.dir/.idea
33
samples/Nexar.samples/bin/Debug/net8.0
44
Nexar /ozel.png
55
.idea/.idea.Nexar /.idea
6+
Nexar /.idea/.idea.Nexar .dir/.idea
7+
Nexar /bin/Debug
8+
Nexar /bin/Debug
9+
Nexar /bin/Debug
10+
Nexar.Test/Nexar.Test/bin/Debug/net8.0
981 Bytes
Binary file not shown.

Nexar /bin/Debug/net8.0/Nexar.dll

1.5 KB
Binary file not shown.

Nexar /bin/Debug/net8.0/Nexar.pdb

76 Bytes
Binary file not shown.

Nexar /obj/Debug/BuzzSpire.Nexar.1.0.0.nuspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<releaseNotes>Alfa</releaseNotes>
1515
<copyright>MIT</copyright>
1616
<tags>C#, HttpClient, HTTP, Requests, REST, API, GET, POST, PUT, DELETE, PATCH, Head, Async, Asynchronous, .NET, Nexar, Web, API, Client, Library</tags>
17-
<repository type="git" url="https://github.com/BuzzSpire/Nexar" commit="b829d5a58ca8049bb3983a484787a1bc0e434f1a" />
17+
<repository type="git" url="https://github.com/BuzzSpire/Nexar" commit="68916bd81183d69c1bb5b35fb1fe1b4e6487c52f" />
1818
<dependencies>
1919
<group targetFramework="net8.0" />
2020
</dependencies>

Nexar /obj/Debug/net8.0/Nexar.AssemblyInfo.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
[assembly: System.Reflection.AssemblyCopyrightAttribute("MIT")]
1616
[assembly: System.Reflection.AssemblyDescriptionAttribute(@"Nexar is a powerful and user-friendly C# class designed for seamless communication with web APIs. With Nexar, sending and receiving HTTP requests has never been easier. Whether you need to perform simple GET requests or more complex POST operations, Nexar provides a clean and efficient interface to interact with web services.")]
1717
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
18-
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+c3bcb34d361587c7f40e9509270494f592eac477")]
18+
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+68916bd81183d69c1bb5b35fb1fe1b4e6487c52f")]
1919
[assembly: System.Reflection.AssemblyProductAttribute("Nexar")]
2020
[assembly: System.Reflection.AssemblyTitleAttribute("Nexar")]
2121
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9af63ef825557b7067cdefbdd9d7ee803400b20a5b547e5bedf69d90e6c15de0
1+
3cf4a65764fa893624ca5829e505e095eefec7f40e770cc36a2308e0db95868a

Nexar /obj/Debug/net8.0/Nexar.dll

1.5 KB
Binary file not shown.

Nexar /obj/Debug/net8.0/Nexar.pdb

76 Bytes
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"documents":{"/Users/mert/github/Nexar/*":"https://raw.githubusercontent.com/BuzzSpire/Nexar/b829d5a58ca8049bb3983a484787a1bc0e434f1a/*"}}
1+
{"documents":{"/Users/mert/github/Nexar/*":"https://raw.githubusercontent.com/BuzzSpire/Nexar/68916bd81183d69c1bb5b35fb1fe1b4e6487c52f/*"}}

Nexar /obj/Debug/net8.0/ref/Nexar.dll

1 KB
Binary file not shown.
1 KB
Binary file not shown.
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
17284035680895798
1+
17287157342940800

Nexar /src/Http/Abstract/HttpClient.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public interface INexar
2121
/// <param name="headers">The headers to be added to the request.</param>
2222
/// <param name="body">The body of the request.</param>
2323
/// <returns>The HTTP response message as a string.</returns>
24-
Task<string> PostAsync(string url, Dictionary<string, string> headers, string body);
24+
Task<string> PostAsync<T>(string url, Dictionary<string, string> headers, T body);
2525

2626
/// <summary>
2727
/// Sends a PUT request to the specified Uri as an asynchronous operation.

Nexar /src/Http/Concrate/Nexar.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Text;
2+
using System.Text.Json;
13
using Nexar.Http.Abstract;
24

35

@@ -21,17 +23,19 @@ public async Task<string> GetAsync(string url, Dictionary<string, string> header
2123
return await response.Content.ReadAsStringAsync();
2224
}
2325

24-
public async Task<string> PostAsync(string url, Dictionary<string, string> headers, string body)
26+
public async Task<string> PostAsync<T>(string url, Dictionary<string, string> headers, T body)
2527
{
2628
var request = new HttpRequestMessage(HttpMethod.Post, url)
2729
{
28-
Content = new StringContent(body)
30+
Content = new StringContent(JsonSerializer.Serialize(body))
2931
};
3032

3133
foreach (var header in headers)
3234
{
3335
request.Headers.Add(header.Key, header.Value);
3436
}
37+
38+
request.Content = new StringContent(JsonSerializer.Serialize(body), Encoding.UTF8, "application/json");
3539

3640
var response = await _client.SendAsync(request);
3741
response.EnsureSuccessStatusCode();
+2-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
22
<s:Boolean x:Key="/Default/AddReferences/RecentPaths/=_002FUsers_002Fmert_002Fgithub_002FNexar_002FNexar_0020_002Fbin_002FDebug_002Fnet8_002E0_002FNexar_002Edll/@EntryIndexedValue">True</s:Boolean>
3-
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=38738679_002D1c03_002D40a1_002Db089_002Ddd8d221456aa/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="GetAsync_ReturnsSuccessfulResponse" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
3+
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=38738679_002D1c03_002D40a1_002Db089_002Ddd8d221456aa/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="GetAsync_ReturnsSuccessfulResponse" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
44
&lt;TestAncestor&gt;
55
&lt;TestId&gt;xUnit::31A13890-A264-4A09-9AD3-ACE82D69E396::net8.0::NexarTests&lt;/TestId&gt;
66
&lt;/TestAncestor&gt;
77
&lt;/SessionState&gt;</s:String>
8-
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=6a015090_002Dd404_002D4a18_002D9a5e_002De74767d304b5/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="NexarTests" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
9-
&lt;TestAncestor&gt;
10-
&lt;TestId&gt;xUnit::31A13890-A264-4A09-9AD3-ACE82D69E396::net8.0::NexarTests&lt;/TestId&gt;
11-
&lt;/TestAncestor&gt;
12-
&lt;/SessionState&gt;</s:String></wpf:ResourceDictionary>
8+
</wpf:ResourceDictionary>

Nexar.Test/Nexar.Test/NexarTest.cs

+38-28
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
using Moq;
22
using System.Net;
33
using Moq.Protected;
4+
using Newtonsoft.Json;
5+
using Xunit.Abstractions;
6+
using JsonSerializer = System.Text.Json.JsonSerializer;
47

58
/// <summary>
69
/// This class contains unit tests for the Nexar class.
710
/// </summary>
811
public class NexarTests
912
{
13+
private readonly ITestOutputHelper _testOutputHelper;
14+
1015
/// <summary>
1116
/// Mock object for HttpMessageHandler to simulate HTTP requests.
1217
/// </summary>
@@ -26,11 +31,21 @@ public class NexarTests
2631
/// Constructor for the NexarTests class.
2732
/// Initializes the mock HttpMessageHandler, HttpClient, and Nexar objects.
2833
/// </summary>
29-
public NexarTests()
34+
public NexarTests(ITestOutputHelper testOutputHelper)
3035
{
36+
_testOutputHelper = testOutputHelper;
3137
_mockHttpMessageHandler = new Mock<HttpMessageHandler>();
3238
_client = new HttpClient(_mockHttpMessageHandler.Object);
3339
_nexar = new Nexar.Nexar();
40+
41+
// Setup the mock HttpMessageHandler to return a successful HTTP response.
42+
var httpResponse = new HttpResponseMessage(HttpStatusCode.OK)
43+
{
44+
Content = new StringContent("Successful response")
45+
};
46+
_mockHttpMessageHandler.Protected().Setup<Task<HttpResponseMessage>>("SendAsync",
47+
ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>()).ReturnsAsync(httpResponse);
48+
3449
}
3550

3651
/// <summary>
@@ -40,21 +55,6 @@ public NexarTests()
4055
[Fact]
4156
public async Task GetAsync_ReturnsSuccessfulResponse()
4257
{
43-
// Setup the mock HttpMessageHandler to return a successful HTTP response.
44-
var httpResponse = new HttpResponseMessage(HttpStatusCode.OK)
45-
{
46-
Content = new StringContent("Successful response")
47-
};
48-
49-
_mockHttpMessageHandler
50-
.Protected()
51-
.Setup<Task<HttpResponseMessage>>(
52-
"SendAsync",
53-
ItExpr.IsAny<HttpRequestMessage>(),
54-
ItExpr.IsAny<CancellationToken>()
55-
)
56-
.ReturnsAsync(httpResponse);
57-
5858
// Call the GetAsync method and verify the response.
5959
var headers = new Dictionary<string, string> { { "Accept", "application/json" } };
6060
var result = await _nexar.GetAsync("https://fakestoreapi.com/products/1", headers);
@@ -69,20 +69,30 @@ public async Task GetAsync_ReturnsSuccessfulResponse()
6969
[Fact]
7070
public async Task GetAsync_ThrowsExceptionForUnsuccessfulResponse()
7171
{
72-
// Setup the mock HttpMessageHandler to return a bad request HTTP response.
73-
var httpResponse = new HttpResponseMessage(HttpStatusCode.BadRequest);
74-
75-
_mockHttpMessageHandler
76-
.Protected()
77-
.Setup<Task<HttpResponseMessage>>(
78-
"SendAsync",
79-
ItExpr.IsAny<HttpRequestMessage>(),
80-
ItExpr.IsAny<CancellationToken>()
81-
)
82-
.ReturnsAsync(httpResponse);
83-
8472
// Call the GetAsync method and verify that it throws an exception.
8573
var headers = new Dictionary<string, string> { { "Accept", "application/json" } };
8674
await Assert.ThrowsAsync<HttpRequestException>(() => _nexar.GetAsync("https://api.example.com", headers));
8775
}
76+
77+
[Fact]
78+
public async Task PostAsync_ReturnsSuccessfulResponse()
79+
{
80+
var headers = new Dictionary<string, string> { { "Accept", "application/json" } };
81+
82+
var body = new
83+
{
84+
id = 21,
85+
title = "test product",
86+
price = 13.5,
87+
description = "lorem ipsum set",
88+
image = "https://i.pravatar.cc",
89+
category = "electronic"
90+
};
91+
92+
var result =
93+
await _nexar.PostAsync("https://fakestoreapi.com/products", headers, body);
94+
95+
Assert.Equal(JsonSerializer.Serialize(body), result);
96+
}
97+
8898
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
76 Bytes
Binary file not shown.

Nexar.Test/Nexar.Test/obj/Debug/net8.0/Nexar.Test.AssemblyInfo.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
[assembly: System.Reflection.AssemblyCompanyAttribute("Nexar.Test")]
1414
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
1515
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
16-
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+bdf99157b4e0ec6b9a4562c0ac8efb079fa0ddd4")]
16+
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+68916bd81183d69c1bb5b35fb1fe1b4e6487c52f")]
1717
[assembly: System.Reflection.AssemblyProductAttribute("Nexar.Test")]
1818
[assembly: System.Reflection.AssemblyTitleAttribute("Nexar.Test")]
1919
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
53e2894abe7beea2c27f71523429d37ad136060057e1fe001bf98c49120d2682
1+
1e3c24e1f88cc5c95cb96763867c2660c6a3d4c93956bfd79fbb7f631411d68e
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"documents":{"/Users/mert/github/Nexar/*":"https://raw.githubusercontent.com/BuzzSpire/Nexar/bdf99157b4e0ec6b9a4562c0ac8efb079fa0ddd4/*"}}
1+
{"documents":{"/Users/mert/github/Nexar/*":"https://raw.githubusercontent.com/BuzzSpire/Nexar/68916bd81183d69c1bb5b35fb1fe1b4e6487c52f/*"}}
Binary file not shown.
Binary file not shown.

Nexar.Test/Nexar.Test/obj/Nexar.Test.csproj.nuget.dgspec.json

+5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
"warnAsError": [
3333
"NU1605"
3434
]
35+
},
36+
"restoreAuditProperties": {
37+
"enableAudit": "true",
38+
"auditLevel": "low",
39+
"auditMode": "direct"
3540
}
3641
},
3742
"frameworks": {

Nexar.Test/Nexar.Test/obj/Nexar.Test.csproj.nuget.g.props

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">/Users/mert/.nuget/packages/</NuGetPackageRoot>
88
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">/Users/mert/.nuget/packages/</NuGetPackageFolders>
99
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
10-
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.9.1</NuGetToolVersion>
10+
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.10.0</NuGetToolVersion>
1111
</PropertyGroup>
1212
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
1313
<SourceRoot Include="/Users/mert/.nuget/packages/" />

Nexar.Test/Nexar.Test/obj/project.assets.json

+5
Original file line numberDiff line numberDiff line change
@@ -5665,6 +5665,11 @@
56655665
"warnAsError": [
56665666
"NU1605"
56675667
]
5668+
},
5669+
"restoreAuditProperties": {
5670+
"enableAudit": "true",
5671+
"auditLevel": "low",
5672+
"auditMode": "direct"
56685673
}
56695674
},
56705675
"frameworks": {

Nexar.Test/Nexar.Test/obj/project.nuget.cache

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"version": 2,
3-
"dgSpecHash": "mxxepLdYn0HAiiFbFQQuBXCGDi1UDKEZ19Ctul5LF6VWrZEE5wSbXblmYV+uzf4BSw6Sp4neiWQTHn3DBzwuVw==",
3+
"dgSpecHash": "7REIgiHrKLQ=",
44
"success": true,
55
"projectFilePath": "/Users/mert/github/Nexar/Nexar.Test/Nexar.Test/Nexar.Test.csproj",
66
"expectedPackageFiles": [
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
17283296055357338
1+
17287195675578369

0 commit comments

Comments
 (0)