Skip to main content

RestSharp Components

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

RestClient:

            RestClient is basically a bridge between the request and server to get the response using HTTP methods from the server.

·         public static RestClient client;

·         Client=new RestClient(“http://baseurl.com/”);

RestRequest:

            RestRequest where you can create a request which consist header, parameter etc.

·         public static RestRequest restRequest;

·         restRequest=new RestRequest (Method. GET);

·         restRequest.AddHeader(“Accept”,” application/json”);

Handling Request:

  • Using RestRequest creates a new request to specified URL.
  • AddParameter will add a new parameter to the request.
  • HTTP header can easily be added to the request you have generated, using request, AddHeader.
  • You can replace a taken 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.

Execute:

            var content=client.Execute(restRequest).content;

            We here execute by using client.Execute by passing request reference and get response in the form of content.

Execute Method:

            In order to execute the API request from RestSharp client, we need to use simple execute method, but it’s not the only method available. There are lot of overloaded Execute methods available such as.

  • Execute
  • Execute<>(generic)
  • ExecuteAsGet
  • ExecuteAsGet<>
  • ExecuteAsPost
  • ExecuteAsPost<>
  • ExecuteAsync
  • ExecuteAsync<.>
  • ExecuteAsynGet

RestResponse:

            RestResponse basically a response which coming from the server is different format JSON, XML etc.

            var response=client.Execute(restRequest);

Features of RestSharp:

  • The API returns XML, which is automatically detected and de-serialized to the call object using the default xmlDeserializer.
  • By default, a RestRequest is made via GET HTTP request. You can change this by setting the method property of RestRequest or specifying the method in the constructor when creating an instance (covered below).
  • Parameters of type UrlSegment have their value injected into the URL based on a matching token name existing in the Resource property value.

 

Nunit Framework:

Nunit is an open-source unit testing framework for Microsoft .Net. It serves the same purpose as Junit does in the java world and is one of many programs in the Xunit family.

Features:

  • Tests can be run from a console runner, within Visual Studio through a Test Adapter or through 3rd party runners.
  • Tests can be run in parallel.
  • Strong support for data driven tests.
  • Supports multiple platforms including .Net core, Xamarin Mobile, Compact Framework and Silverlight.
  • Every test case can be added to one or more categories to allow for selective running.

Comments

Popular posts from this blog

Specflow Tables – Convert Table to Dictionary

            Tables can be used both vertically and horizontally. In the first table examples, we'll use the Data Vertically as a Key-Value pair, which the Dictionary object allows. 1) Add a New Step The first step is to create a new Step that will accept data in the form of a table. It is once again simple to specify data for the step in Vertical format. Let's look at how to pass Vertical Data in the same Users scenario that was used in the previous SpecFlow :   Feature : Users        Operations about user   Background :        Given I perform api operation on petstore server " https://petstore.swagger.io/v2/ "   Scenario : POST operation with table        When I perform POST operation for User using endpoint " /user " using table data               | Key ...

What is BDD?

  Behavior Driven Framework (BDD) :           In software engineering, behavior-driven development (BDD) is a software development process that evolved from test-driven development (TDD). These tools are frequently designed for use in BDD projects, they can be viewed as specialized forms of test-driven development . The grammar used in the tests is simple descriptive English. Tests are more user-focused and are explained as application behavior. Examples are used to clarify requirements. This distinction necessitates the use of a language that can define in an understandable manner. Features of BDD : Shifting from thinking in terms of "tests" to thinking in terms of "behavior." Collaboration among business stakeholders, business analysts, the QA team, and developers. It's simple to describe a language that's everywhere. Business value is the driving force. Test Driven Development (TDD) is extended by using natural language that non-technical stakeholders ca...

Json Response Deserialization

  How Do I Deserialize a JSON Response to a Class Using Rest Sharp? Let's continue from the earlier section, Creating a POST request with Rest-Sharp. In the earlier section, we made a successful post request and received the Response body shown below. To validate parts of the Response body, we used JObject.Parse. We will make this Response body into a Class. Deserialization of JSON refers to the process of converting a JSON or any structured data, including XML, into a programming class. Deserialization refers to the process of converting JSON from its String form to its Class form. This is also known as structured data object representation. JSON is used to represent structured data in this case. To integrate the JSON to a Class representation, we will first create a Class that contains all of the nodes in the JSON. There are three nodes in the Success response above. ·          Success code ·      ...