Software Development

RESTful API testing in C# with RestSharp

What is RestSharp? An Introduction to RestSharp’s Features and Functionality

RestSharp is a comprehensive, open-source HTTP client library that works with all kinds of DotNet technologies.  It can be used to build robust applications by making it easy to interface with public APIs and quickly access data without the complexity of dealing with raw HTTP requests.

With its simple API and powerful library, REST architecture is the tool of choice for programmers looking to build detailed programs and applications. RESTful architecture provides an information-driven, resource-oriented approach to creating Web applications.

Benefits of RestSharp

RestSharp is one of the best libraries to use if you frequently use REST to consume HTTP APIs in DotNet. The main purpose of RestSharp is to make synchronous and asynchronous calls to remote resources over HTTP.

However, RestSharp can call any API over HTTP, as long as you have the resource URI and request parameters that you want to send comply with W3C HTTP standards.

One of the main challenges of using HTTP APIs for .NET developers is to work with requests and responses of different kinds and translate them to complex C# types. RestSharp can take care of serializing the request body to JSON or XML and deserialize the response. It can also form a valid request URI based on different parameter kinds – path, query, form or body.

In short, RestSharp works best as the foundation for a proxy class for your API. Each API would most probably require different settings for RestClient. Hence, a dedicated API class (and its interface) gives you sound isolation between different RestClient instances and make them testable.

Essentially, RestSharp is a wrapper around HttpClient that allows you to do the following:

  • Add default parameters of any kind (not just headers) to the client, once
  • Add parameters of any kind to each request (query, URL segment, form, attachment, serialized body, header) in a straightforward way
  • Serialize the payload to JSON or XML if necessary
  • Set the correct content headers (content type, disposition, length, etc.)
  • Handle the remote endpoint response
  • Deserialize the response from JSON or XML if necessary

How RestSharp Works

RestSharp works best as the foundation for a proxy class for an API. The most basic features of RestSharp include creating a request, adding parameters to the request, execution, and handling of said request, deserialization, and authentication. Here’s a look at some RestSharp basics:

  • Handling Requests
  • Using RestRequest creates a new request to a specified URL.
  • AddParameter will add a new parameter to the request.
  • HTTP headers can easily be added to the request you have generated, using request.AddHeader.
  • You can replace a token in the request, by using request.AddUrlSegment. This will replace the matching token in the request.
  • To execute the request, the command client.Execute(request) is used. The response object can also be used to parse your data.

The examples I show you in this blog post use the https://reqres.in/ API, a publicly accessible API that tests your front-end against a real API. For example, when you send an HTTP GET call to https://reqres.in/api/users?page=2 (where api is the resource, users is the path Parameter and page=2 is the Query parameter), you’ll receive this JSON document as a response:

 

Some really basic checks

RestSharp is available as a NuGet package, which makes it really easy to add to your C# project. So, what does an API test written using RestSharp look like? Let’s say that I want to check whether the previously mentioned HTTP GET call checks if page 2 is reflecting correct and First Name reflects “Michael”, this is what that looks like:

Working with response bodies

It’s highly recommended deserializing JSON responses to actual objects or POCOs (Plain Old C# Objects) in this case. The JSON response you’ve seen earlier in this blog post can be represented by the following ListOfUserDTO class:

And the Datum class inside looks like this:

 

Now that we have modelled our API response as a C# class, we can convert an actual response to an instance of that class using the deserializer that’s built into RestSharp. After doing so, we can refer to the contents of the response by accessing the fields of the object, which makes for far easier test creation and maintenance:

 

All the code that I’ve included in this blog post is available on my Github page. Feel free to clone this project and run it on your own machine to see how automation testing and assertion happen.

 

References:

https://restsharp.dev/intro.html

https://www.ontestautomation.com/restful-api-testing-in-csharp-with-restsharp/

https://reqres.in/ (For API Automation testing)

https://app.quicktype.io/ (for converting JSON responses to objects)

https://www.youtube.com/c/LearnTestAutomation (Rest API Testing using C# Playlists)

Leave a Reply