Reputation: 15
I'm new to C#, I'm assigned a work to build an API Test application. The Responses are in JSON format(complex), I have to validate(test) the responses for it's correct format, correct type of variables, Check Whether Mandatory datas' are filled etc dynamically(without explicitly creating classes to store deserialized values). Please help me through it.
Upvotes: 1
Views: 1749
Reputation: 307
If you are testing against an integration environment / live API, you could use a framework called RestFluencing.
Rest.GetFromUrl("https://api.github.com/users/defunkt")
.WithHeader("User-Agent", "RestFluencing Sample")
.Response()
.ReturnsDynamic(c => c.login == "defunkt", "Login did not match")
.ReturnsDynamic(c => c.id == 2, "ID did not match")
.Assert();
Also if you have a class model you could use an JsonSchema validator:
public class GitHubUser
{
public int id { get; set; }
public string login { get; set; }
}
Validating:
Rest.GetFromUrl("https://api.github.com/users/defunkt")
.Response()
.HasJsonSchema<GitHubUser>(new JSchemaGenerator())
.Assert();
Check out the GitHub page: RestFluencing
Disclaimer: I am the dev for that framework. Doing an full integration api testing in an easy way in C# has been something that I have been looking for a long time, hence this Fluent-style framework.
Upvotes: 3
Reputation: 610
@Abhishek, System.Json is a .NET Framework namespace which could fulfill your needs. Do explore this namespace in MSDN here - https://msdn.microsoft.com/en-us/library/system.json(v=vs.110).aspx.
In this (How to make sure that string is Valid JSON using JSON.NET) post one more alternative - JSON.NET and the way to use it is discussed.I would still recommend using System.Json until there is a compelling reason to use 3rd party API (JSON.NET).
To validate the data type and the range of values in a property consider JsonType property of JsonValue class. This property returns an enumeration which you could use to validate the data type. For date data type, you will have to parse it from the string using DateTime.Parse method.
Upvotes: 0