Serializing Objects to URL Encoded Form Data
I was writing integration tests and I wanted to re-use some model classes instead of dictionaries. Here's how I did it.
Join the DZone community and get the full member experience.
Join For FreeWhile messing with dictionaries to create form data for FormUrlEncodedContent to see I could send data to a server using an HTTP client, I started thinking about easier and cleaner ways to do it. I was writing integration tests and I wanted to re-use some model classes instead of dictionaries. Here's how to do it. Sample integration test is incluced.
Those who are currently writing integration tests for some ASP.NET Core web applications may also find the following writings interesting:
- Using custom appsettings.json with ASP.NET Core integration tests
- Using custom startup class with ASP.NET Core integration tests
- Using ASP.NET Core Identity user accounts in integration tests
- File uploads in ASP.NET Core integration tests
Problem
I write integration tests for ASP.NET Core web application. There are tests that use HTTP POST to send some data to a server. I can create a POST body as shown here.
var formDictionary = new Dictionary<string, string>();
formDictionary.Add("Id", "1");
formDictionary.Add("Title", "Item title");
var formContent = new FormUrlEncodedContent(formDictionary);
var response = await client.PostAsync(url, formContent);
I don't like this code and I would use it only if other options turn out to be less optimal. Instead, I want something like this:
var folder = new MediaFolder { Id = 1, Title = "Folder title" };
var formBody = GetFormBodyOfObject(folder);
// Act
var response = await client.PostAsync(url, fromBody);
Or why not something more elegant like the code here:
var folder = new MediaFolder { Id = 1, Title = "Folder title" };
// Act
var response = await client.PostAsync(url, folder.ToFormBody());
This way I can use model classes from web applications because these classes are used on forms anyway to move data between a browser and a server. If otherwise breaking changes are introduced then the compiler will tell me about it.
Serializing Objects to Dictionary<string,string>
Thanks to Arnaud Auroux from Geek Learning, we have a ready-made solution. The ToKeyValue()
method by Arnaud turns objects to JSON and then uses the Newtonsoft.JSON library to convert the object toa dictionary like the FormUrlEncodedContent class wants.
The ToKeyValue()
method is the main work horse for my solution. I turned it into an extension method and made it a serializer to avoid cyclic references.
public static IDictionary<string, string> ToKeyValue(this object metaToken)
{
if (metaToken == null)
{
return null;
}
// Added by me: avoid cyclic references
var serializer = new JsonSerializer { ReferenceLoopHandling = ReferenceLoopHandling.Ignore };
var token = metaToken as JToken;
if (token == null)
{
// Modified by me: use serializer defined above
return ToKeyValue(JObject.FromObject(metaToken, serializer));
}
if (token.HasValues)
{
var contentData = new Dictionary<string, string>();
foreach (var child in token.Children().ToList())
{
var childContent = child.ToKeyValue();
if (childContent != null)
{
contentData = contentData.Concat(childContent)
.ToDictionary(k => k.Key, v => v.Value);
}
}
return contentData;
}
var jValue = token as JValue;
if (jValue?.Value == null)
{
return null;
}
var value = jValue?.Type == JTokenType.Date ?
jValue?.ToString("o", CultureInfo.InvariantCulture) :
jValue?.ToString(CultureInfo.InvariantCulture);
return new Dictionary<string, string> { { token.Path, value } };
}
Now we can serialize our objects to Dictionary<string,string> and it also works with object hierarchies.
Serializing Objects to Form Data
But we are not there yet. The ToKeyValue()
extension method is useful if we want to modify a dictionary before it goes to FormUrlEncodedContent
. For cases when we don't need any modifications to the dictionary, I wrote the ToFormData()
extension method.
Using these extension methods, I can write integration tests as shown here.
[Fact]
public async Task CreateFolder_CreatesFolderWithValidData()
{
// Arrange
var factory = GetFactory(hasUser: true);
var client = factory.CreateClient();
var url = "Home/CreateFolder";
var folder = new MediaFolder { Id = 1, Title = "Folder title" };
// Act
var response = await client.PostAsync(url, folder.ToFormData());
// Assert
response.EnsureSuccessStatusCode(); // Status Code 200-299
Assert.Equal("text/html; charset=utf-8", response.Content.Headers.ContentType.ToString());
}
Wrapping Up
We started with an annoying problem and ended up with a pretty nice solution. Instead of manually filling dictionaries used for HTTP POST requests in integration tests, we found nice a JSON-based solution by Arnaud Auroux and we built the ToKeyValue()
and ToFormData()
extension methods to simplify object serialization to .NET Core HttpClient form data. These extension methods are general enough to have them in some common library for our projects.
Published at DZone with permission of Gunnar Peipman, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments