Skip to content

Commit 5e0b738

Browse files
authored
Invalid SendForm tests (#10)
* adding invalid sendform tests * number 3 it is...
1 parent 3fd41f1 commit 5e0b738

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/StackExchange.Utils.Http/Extensions.Send.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static IRequestBuilder SendForm(this IRequestBuilder builder, NameValueCo
3434
var content = new MultipartFormDataContent();
3535
foreach(var formKey in form.AllKeys)
3636
{
37-
content.Add(new StringContent(form[formKey]), formKey);
37+
content.Add(new StringContent(form[formKey] ?? string.Empty), formKey);
3838
}
3939
return SendContent(builder, content);
4040
}

tests/StackExchange.Utils.Tests/HttpTests.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,5 +243,44 @@ public async Task LargePost()
243243
Assert.Equal("https://httpbin.org/post", result.Data.Url);
244244
Assert.Equal(Http.DefaultSettings.UserAgent, result.Data.Headers["User-Agent"]);
245245
}
246+
247+
[Fact]
248+
public async Task NullSendFormValue()
249+
{
250+
var settings = new HttpSettings();
251+
252+
var form = new System.Collections.Specialized.NameValueCollection
253+
{
254+
["nullValue"] = null,
255+
};
256+
257+
var result = await Http.Request("https://httpbin.org/post", settings)
258+
.SendForm(form)
259+
.ExpectJson<HttpBinResponse>()
260+
.PostAsync();
261+
262+
Assert.True(result.Success);
263+
Assert.Null(result.Error);
264+
Assert.Same("", result.Data.Form["nullValue"]);
265+
}
266+
267+
[Theory]
268+
[InlineData(null)]
269+
[InlineData("")]
270+
public void NullSendFormKey(string key)
271+
{
272+
var settings = new HttpSettings();
273+
274+
var form = new System.Collections.Specialized.NameValueCollection
275+
{
276+
[key] = "nullKey",
277+
// if not handled, throws System.ArgumentException
278+
// at System.Net.Http.MultipartFormDataContent.Add(HttpContent content, String name)
279+
// at StackExchange.Utils.ExtensionsForHttp.SendForm(IRequestBuilder builder, NameValueCollection form)
280+
};
281+
282+
Assert.ThrowsAny<Exception>(() => Http.Request("https://httpbin.org/post", settings).SendForm(form));
283+
// TODO would it be more appropriate to return just log the error and return it in result.Error after .PostAsync?
284+
}
246285
}
247286
}

0 commit comments

Comments
 (0)