Skip to content

Commit cfafcbe

Browse files
committed
fix #270
1 parent 78fbaf7 commit cfafcbe

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

WebApiClientCore.Test/Implementations/HttpApiRequestMessageTest.cs

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using System;
2-
using System.Text;
32
using System.Threading.Tasks;
43
using WebApiClientCore.Exceptions;
54
using WebApiClientCore.Implementations;
6-
using WebApiClientCore.Internals;
75
using Xunit;
86

97
namespace WebApiClientCore.Test.Implementations
@@ -112,9 +110,9 @@ public async Task AddFormFiledAsyncTest()
112110
[Fact]
113111
public async Task AddFormDataTextTest()
114112
{
115-
string get(string name, string value)
113+
static string get(string name, string value)
116114
{
117-
return $@"Content-Disposition: form-data; name=""{name}""{"\r\n\r\n"}{HttpUtil.UrlEncode(value, Encoding.UTF8)}";
115+
return $@"Content-Disposition: form-data; name=""{name}""{"\r\n\r\n"}{value}";
118116
}
119117

120118
var reqeust = new HttpApiRequestMessageImpl();
@@ -130,5 +128,19 @@ string get(string name, string value)
130128
Assert.Contains(get("age", "18"), body);
131129
Assert.Equal("multipart/form-data", reqeust.Content.Headers.ContentType!.MediaType);
132130
}
131+
132+
133+
[Fact]
134+
public void AddFormDataTextEmptyCollectionTest()
135+
{
136+
var reqeust = new HttpApiRequestMessageImpl
137+
{
138+
Method = System.Net.Http.HttpMethod.Post,
139+
RequestUri = new Uri("http://webapiclient.com")
140+
};
141+
142+
reqeust.AddFormDataText([]);
143+
Assert.Null(reqeust.Content);
144+
}
133145
}
134146
}

WebApiClientCore/Implementations/HttpApiRequestMessageImpl.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,13 @@ public override void AddFormDataText(IEnumerable<KeyValue> keyValues)
180180
if (this.Content is not MultipartContent httpContent)
181181
{
182182
httpContent = new FormDataContent();
183-
this.Content = httpContent;
184183
}
185184

186185
foreach (var keyValue in keyValues)
187186
{
188187
var textContent = new FormDataTextContent(keyValue);
189188
httpContent.Add(textContent);
189+
this.Content = httpContent;
190190
}
191191
}
192192

0 commit comments

Comments
 (0)