Previous two tutorials covers How to make a simple GET request and How to validate Response Status Code from Weather Web Service. It is suggested to go through the above tutorials first before moving with this tutorial which covers How to Validate Response Header using Rest Sharp.
Validate Response Header using Rest Sharp
Every response that is received from a server contains zero or more headers. Headers are the part of Response that is sent by the server. Each header entry is basically a Key-Value pair. Headers are used to send across extra information by the server. This extra information is also referred to as Meta data of the Response.
Using the Headers a client can take intelligent decisions for e.g.
One of the Headers called Content-Type which tells how to interpret the data present in the Body of the Response. If the Body contains data in the form of JSON, then the value of Content-Type header will be application/json. Similarly if the data in the body is XML the Content-Type header will be application/xml.
How to read different Header Types from HTTP Response?
Get different Header Types received in a Response
Let’s just see how to read a Header using RestSharp. To do that lets do a simple exercise in which the test would record the following Header Types from the Response:
Content-Type
Server
Content-Encoding
The Response interface provides direct parameter property collection “Headers” to access individual header or all the Headers. Below image shows the available list parameters.
Reading Individual Header
As we get all the headers in a list. To get individual header and its value we can create a dictionary<key T, Value T> and store all headers and retrieve individual header by key as header name and store their value in key-value pair. By each individual header name we can retrieve its value. Below is the sample code for it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
[TestMethod] public void GetContentTypeHeader() { RestClient restClient = new RestClient("http://restapi.demoqa.com/utilities/weather/city/"); RestRequest restRequest = new RestRequest("Guntur", Method.GET); IRestResponse restResponse = restClient.Execute(restRequest); // Creating a dictionary and adding all headers and their values Dictionary<string, string> HeadersList = new Dictionary<string, string>(); foreach (var item in restResponse.Headers) { string[] KeyPairs=item.ToString().Split('='); HeadersList.Add(KeyPairs[0],KeyPairs[1]); } Assert.AreEqual("application/json”,HeadersList["Content-Type"],"Content-type not matching"); /*Expected*/, /*Actual*/, /*Failure message*/ } |
To create a dictionary which store string key(Header Name) and its string value(Header Value)
We use following code:
1 2 |
Dictionary<string, string> HeadersList = new Dictionary<string, string>(); /*Key*//*Value*/ |
As we get headers list parameters which is a collection. We iterate through each list item, which contains header name and value, so we split the parameters and add each into the dictionary created.
1 2 3 4 5 6 7 8 9 |
// For each list item from headers list foreach (var item in restResponse.Headers) { // split each item into header name and its value string[] KeyPairs=item.ToString().Split('='); // Add the extracted details into created dictionary HeadersList.Add(KeyPairs[0],KeyPairs[1]); } |
Now to read any header from the dictionary we can use as shown below:
1 2 |
HeadersList["Content-Type"] /*Dictionalry[“Header Name”]*/ |
How to Validate Response Header using RestSharp?
Now that we have a mechanism to read a Header. Let’s write a test to validate the values of the header by putting an Assert. The code is simple and its mostly same as the above code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
[TestMethod] public void GetContentTypeHeader() { RestClient restClient = new RestClient("http://restapi.demoqa.com/utilities/weather/city/"); RestRequest restRequest = new RestRequest("Guntur", Method.GET); IRestResponse restResponse = restClient.Execute(restRequest); // Creating a dictionary and adding all headers and their values Dictionary<string, string> HeadersList = new Dictionary<string, string>(); foreach (var item in restResponse.Headers) { string[] KeyPairs=item.ToString().Split('='); HeadersList.Add(KeyPairs[0],KeyPairs[1]); } //Asserting content type from header Assert.AreEqual("application/json”, HeadersList["Content-Type"],"Content-type not matching"); //Asserting Server name from header Assert.AreEqual("nginx/1.14.0”, HeadersList["Server"]," Server not matching"); } |