Developer Docs

What is RestSharp?

RestSharp is a C# library used to build and send API requests, and interpret the responses. It is used as part of the C#Bot API testing framework to build the requests, send them to the server, and interpret the responses so assertions can be made. The ‘WebApi’ class in the C#Bot testing framework acts as a convenience wrapper around RestSharp. This class adds several defaults to the header of requests to avoid repetition, but it is flexible enough to extend upon further for your own manual tests.

/// <summary>
/// Sets the most common headers shared by endpoints of the application.
/// </summary>
public void SetDefaultHeaders()
{
    CommonHeaders["Content-Type"] = "application/json";
    CommonHeaders["Accept"] = "application/json, text/html, */*";
}

/// <summary>
/// Configures authentication for the specified user, if none is provided the super account
/// is used.
/// </summary>
/// <param name="userName">The username of the user to configure authentication for</param>
/// <param name="password">The password of the user to configure authentication for</param>
public void ConfigureAuthenticationHeaders(string userName = null, string password = null)
{
    userName ??= _configure.SuperUsername;
    password ??= _configure.SuperPassword;

    var loginToken = new LoginToken(_configure.BaseUrl, userName, password);

    CommonHeaders["Authorization"] = $"{loginToken.TokenType} {loginToken.AccessToken}";
}

The WebApi class also contains methods which will send the API request to the server, and will return the response. To write new API tests, you need to find the path for the endpoint you want to test (in the example below it is ‘/api/graphql’, but this will vary). API endpoints are found in Serverside controllers, and use the syntax ‘Route[“api/entity/exampleEntity”]’ for the controller, and then another route on the individual endpoint which is appended to the controller endpoint to get the complete route.

The query which you pass in with the route, will be the information which you want to pass to the request. The information required will depend on the individual endpoint, but query information is added into a RestSharp JsonObject to it can be added to the request. An example of this can be seen in the example below.

var query = new RestSharp.JsonObject();
query.Add("query", "{ " + entityName + "{id}}");

api.ConfigureAuthenticationHeaders();
var response = api.Post($"/api/graphql", query);

Once you have received a response, you can check the response code returns as expected:

Assert.Equal(HttpStatusCode.OK, response.StatusCode);

On this page