Validate Response Status
In the previous tutorial of REST API Test with RestSharp, a test made a call to Weather REST Service. This tutorial will also continue the same example.
Functional testing of REST Services involves verifying the Responses returned from various End-Points. Some of the important test verification are
- Response Status
- Response Header
- Response Body
Validate Response Status using RestSharp
This tutorial will cover the following verification for Response Status :
- Validating Response Status Code
- Validating Error Status Code
- Validating Response Status Line
From the previous First RestSharp Test example, a simple test which calls the Weather web service using a Get Method looks like this
1 2 3 4 5 6 7 8 9 |
[TestMethod] public void GetWheatherInfo() { RestClient restClient = new RestClient("http://restapi.demoqa.com/utilities/weather/city/"); RestRequest restRequest = new RestRequest("Guntur",Method.GET); IRestResponse restResponse = restClient.Execute(restRequest); string response = restResponse.Content; if(!response.contains("Guntur”) Assert.Fail("Whether information is not displayed"); } |
IResponse object which represents the HTTP Response packet received from the Web service Server. HTTP Response contains Status, collection of Headers and a Body. Hence, it becomes obvious that the Response object should provide mechanisms to read Status, Headers and Body.
How to Validate Response Status Code?
IResponse is an interface which has lots of properties, mostly which can help to get parts of the received response. Look at some of the important properties. A simple response followed by a dot (restResponse) would show the available properties on the response object. As shown in the image below.
IResponse Interface Properties
As noticed above, StatusCode can be used to get the status code of the REST Response. This property returns an HttpStatusCode which is an Enum.
1 2 3 4 5 6 7 8 9 10 11 12 |
[TestMethod] public void GetWheatherInfo() { RestClient restClient = new RestClient("http://restapi.demoqa.com/utilities/weather/city/"); RestRequest restRequest = new RestRequest("Guntur", Method.GET); IRestResponse restResponse = restClient.Execute(restRequest); //Extract status code from received response and store in an Interger int StatusCode = (int)restResponse.StatusCode; //Assert that correct Status is returned Assert.AreEqual (200, StatusCode, "Status code is not 200"); } |
Code Explanation
Below line of code, convert the Enum Status Code to Integer from the IResponse object:
1 |
int StatusCode = (int)restResponse.StatusCode; |
Extracted status code is compared with the expected value of 200.
1 2 |
// Assert that correct status code is returned. Assert.AreEqual (200, StatusCode , "Status code is not 200"); |
Try running the test and notice that the test will pass. This is because the web service indeed returns a status code of 200.
How to Validate an Error Status Code?
Status Codes returned by the Server depends on whether the HTTP Request was successful or not. If the Request is successful, Status Code 200 is returned otherwise Status Code other than 200 will be returned.
For ToolsQA Weather web service, let’s create a Negative Test. The scenario will verify the Status Code returned by Weather Web Service on providing invalid City name.
City name that this test will use here is an invalid string, like ‘abcdef”. Just copy the code from previous test and simple replace the city name with “abcdef”. At this moment do not change the status code. The code looks as shown below
1 2 3 4 5 6 7 8 9 10 11 12 |
[TestMethod] public void GetWeatherDetailsInvalidCity() { RestClient restClient = new RestClient("http://restapi.demoqa.com/utilities/weather/city/"); /*Invalid city*/ RestRequest restRequest = new RestRequest(“abcdef", Method.GET); IRestResponse restResponse = restClient.Execute(restRequest); int StatusCode = (int)restResponse.StatusCode; Assert.AreEqual (200, StatusCode , "Status code is not 200"); } |
Now run your test class and check the result. Our negative test GetWeatherDetailsInvalidCity will fail. The reason is that the web service returns an error code of 400 when invalid city name is sent to it. However, the test is validating for expected value of 200 as shown in the image below.
A quick change in the expected result in the code will make sure that this test passes. Update respective line as in the code below:
Assert.AreEqual (400, StatusCode, “Status code is not 200”);
How to Validate Response Status Description, Protocol version?
Just as we get the Status Code as mentioned above, we can get the Status Description & Protocol Version as well. To get these properties we will simply call the Response.PropertyName. Here is the code to do that.
1 2 |
string statusDescription = restResponse.StatusDescription; string protocolVersion = restResponse.ProtocolVersion.ToString(); |