Rest Assured – Test Secure web services

I have recently faced an issue while integrating the Rest assured Test cases with Maven build.

All my web services are protected by SAML implemented tool. So if we access any web service via Rest Assured test case then it will return an error page and asking to provide the user name and password. Hence all those test cases are getting failed.

There are two ways we can resolve the issue.

1. Expose the Restful web services via an another port on which the authentication is not enabled. So we can access all the services through that port.


import com.jayway.restassured.RestAssured;
import static org.hamcrest.Matchers.containsString;
import org.junit.Test;

public class HelloServiceTest  {

    @Test
    public void testPing() {
        RestAssured.baseURI = "http://localhost";
        //Different port number
        RestAssured.port = 8080;
        RestAssured.basePath = "/ctx/";
        RestAssured.when().expect().statusCode(200).body(containsString("Hello World")).get("/hello");
        }
    }

2. Create an unexpired Test cookie and pass that cookie for each Rest Service call like below. This cookie needs to be processed and validated at service provider end.



import com.jayway.restassured.RestAssured;
import static org.hamcrest.Matchers.containsString;
import org.junit.Test;

public class HelloServiceTest  {

    @Test
    public void testPing() {
        RestAssured.baseURI = "http://localhost";
        RestAssured.port = 80;
        RestAssured.basePath = "/ctx/";
        RestAssured.given().cookies("TEST_USER_COOKIE", "Bala-UID").when().expect().statusCode(200).body(containsString("Hello World")).get("/hello");
        }
    }
Advertisements

One thought on “Rest Assured – Test Secure web services

  1. Pingback: Rest Assured API for Rest Service Integration testing | Bala's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s