Sending an HTTP request

Course Home | Code Example

We are now ready to create our first Rest-Assured API test so we will begin by making a request to the Web API restful-booker specifically the ping endpoint.

Create a GET request

Start by creating a new Test in your class and add the following code into the Test:

Response response = given().get("https://restful-booker.herokuapp.com/ping/");

The given() method is where Rest-Assured starts. The method itself doesn’t do anything but allows you to use a design pattern known as method chaining (I.E. having a method return a different method that can be called in the same expression) to call the method get().

The get() method takes one parameter, the URI of the Web API we want to send a request to. In this case it’s the health check endpoint of the Auth API. We are then saving the results of the API request in a Rest-Assured Response object for future use.

Basic assertions for a request

At the moment our test doesn’t have anything to assert on, so let’s create a simple assertion based on the status code of the response we get back.

Extend the test to assert for a status code of 200:

Response response = given().get("https://restful-booker.herokuapp.com/ping/");
int statusCode = response.getStatusCode();

assertThat(statusCode, is(201));

The response we get back from the Web API is stored in the response object, which we can then call additional methods on to get specific details from the Response. In this case we are calling getStatusCode() to retrieve the HTTP response status code and then asserting it equals 200.

Next Lesson ➔

Mark Winteringham's photo
Author

Mark Winteringham

@2bittester

Mark Winteringham is a tester, toolsmith and the Ministry of Testing DojoBoss with over 10 years experience providing testing expertise on award-winning projects across a wide range of technology sectors including BBC, Barclays, UK Government and Thomson Reuters. He is an advocate for modern risk-based testing practices and trains teams in Automation in Testing, Behaviour Driven Development and Exploratory testing techniques. He is also the co-founder of Software Testing Clinic a community raising awareness of careers in testing and improving testing education. You can find him on Twitter @2bittester.

Comments