(De-)serializing JSON and XML in RestAssured.Net

In a series of (short) blog posts, I would like to share with you some examples of how to use RestAssured.Net for writing tests for RESTful and GraphQL APIs.

All examples that you see in this blog post series can be found on GitHub.

Serialization

Serialization is the process of creating a JSON or XML representation of a C# object. RestAssured.Net supports both JSON and XML, and in this blog post, we’ll look at examples for both.

First, consider this C# object, representing a blog post:

public class Post
{
    [JsonProperty("userId")]
    public int UserId { get; set; }

    [JsonProperty("title")]
    public string Title { get; set; } = string.Empty;

    [JsonProperty("body")]
    public string Body { get; set; } = string.Empty;

    public Post()
    {
    }
}

Now, if we want to create a JSON or an XML representation of an instance of this class, there are several ways to do that. We could, for example, build the JSON or XML ‘by hand’, however, that is error-prone and often clunky.

Therefore, we can also leverage the power of libraries like Json.Net and System.Xml, respectively, to take care of the heavy lifting for us. And that’s exactly what RestAssured.Net does for you, out of the box.

If we want to send a JSON representation of a Post object, all we need to do is pass an instance to the Body() method:

[Test]
public void PostNewPostUsingPoco_CheckStatusCode_ShouldBe201()
{
    Post myNewPost = new Post()
    {
        UserId = 1,
        Title = "My new post title",
        Body = "This is the body of my new post"
    };

    Given()
    .Body(myNewPost)
    .When()
    .Post("http://jsonplaceholder.typicode.com/posts")
    .Then()
    .StatusCode(201);
}

When we run this test and log the request to the standard output, we see that the Post instance is serialized into this JSON payload:

{
    "userId": 1,
    "title": "My new post title",
    "body": "This is the body of my new post"
}

Success! It also works with anonymous objects, by the way:

[Test]
public void PostNewPostUsingAnonymousObject_CheckStatusCode_ShouldBe201()
{
    var myNewPost = new
    {
        userId = 1,
        title = "My new post title",
        body = "This is the body of my new post"
    };

    Given()
    .Body(myNewPost)
    .When()
    .Post("http://jsonplaceholder.typicode.com/posts")
    .Then()
    .StatusCode(201);
}

If we want to serialize our Post object to XML instead of JSON, we need to set the request Content-Type header to application/xml:

[Test]
public void PostNewPostAsXmlUsingAnonymousObject_CheckStatusCode_ShouldBe201()
{
    Given()
    .ContentType("application/xml")
    .Body(myNewPost)
    .When()
    .Post("http://jsonplaceholder.typicode.com/posts")
    .Then()
    .StatusCode(201);
}

This will result in the following XML payload to be POSTed to the endpoint:

<Post xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <UserId>1</UserId>
    <Title>My new post title</Title>
    <Body>This is the body of my new post</Body>
</Post>

Deserialization

We can also transform JSON or XML response payloads back into strongly typed C# objects, a process known as deserialization. In RestAssured.Net, this is done using the As() method:

[Test]
public void GetPost_CheckTitle_ShouldBeExpectedValue()
{
    Post myPost =

    (Post)Given()
    .When()
    .Get("http://jsonplaceholder.typicode.com/posts/1")
    .As(typeof(Post));

    Assert.That(myPost.Title, Is.EqualTo("sunt aut facere repellat provident occaecati excepturi optio reprehenderit"));
}

This, too, works both for JSON and XML. RestAssured.Net inspects the Content-Type header value of the response and tries to deserialize the response according to its value, defaulting to JSON if no Content-Type header value can be found.

That’s it for (de-)serialization and working with JSON and XML request and response payloads in RestAssured.Net. In the next blog post in this series, we’ll look at extracting and reusing request properties and response values, as well as different API authentication mechanisms supported by RestAssured.Net.

"