For this lesson we are going to look at how to add HTTP headers to a request and see how they affect the response we get back.
Start off by creating a new test and adding in the following code and running it:
Header acceptHeader = new Header("accept","text/plain"); Response response = given() .header(acceptHeader) .get("https://restful-booker.herokuapp.com/booking/1"); int statusCode = response.getStatusCode(); assertThat(statusCode, is(200));
Hang on! When it runs it fails!!! Whats up with that???
What we have done is called another method before
get(), specifically the
header() method which will add an HTTP header to your request before it’s sent. The header we passed to the request was create before hand by using the Rest-Assured
Header object where we added the name of the header (accept) and the value (text/plain).
And this is what is causing the test to fail. We have set the wrong value for the header. So let’s update the
acceptHeader line to the correct value and then run the test again:
Header acceptHeader = new Header("accept","application/json");
Hey presto, the test passes!