0
votes

Please consider below code,

import org.junit.Test;
import org.omg.CORBA.Request;

import com.jayway.restassured.RestAssured;
import com.jayway.restassured.response.Response;

import static com.jayway.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;

public class App 
{

    @Test
    public void loginAuthentication()
    {

        RestAssured.authentication = basic("username", "password");

        Response resp = given().
            contentType("application/x-www-form-urlencoded").
        when().
            post("https://unitrends.atlassian.net/login?username=Gayatri.londhe&password=GayatriA4");

        System.out.println(resp.prettyPrint());
    }
}

I am getting below response,

<div class="aui-message error">
     <span class="aui-icon icon-error"/>
     <span class="error" id="error-authentication_failure_invalid_credentials">
        Sorry, we didn't recognize that username and password combination. Please double-check and try again.
     </span>
</div>

How to use authentication in rest-assured, i am able to do rest post request using REST-console (chrome plugin). What am i doing wrong?

I hope to get some help at this. I am new at rest-assured testing.

1

1 Answers

0
votes

Try this...

Response resp = given().
            contentType("application/x-www-form-urlencoded").expect().response().statusCode(200).
            when().
            post("https://unitrends.atlassian.net/login?username=Gayatri.londhe&password=GayatriA4");

It will fetch response if the response status is 200.Change it,if you service endpoint returns other than 200 on success.

Try this

@BeforeClass
public void setup() throws org.json.JSONException, IOException {
  //Assigning base url
    RestAssured.baseURI = "https://unitrends.atlassian.net/";
    //Assigning default port
    RestAssured.port = <port>
    RestAssured.basePath = "login";
    RestAssured.config = config().redirect(RedirectConfig.redirectConfig().followRedirects(true).and().allowCircularRedirects(true).maxRedirects(100));
}
@Test
public void testRestServiceAuthentication() {
    //for a get method with basic authorization,its expecting response code 200
    Response res = given().expect().response().statusCode(200).when().post("?username=Gayatri.londhe&password=GayatriA4");
    System.out.println(res.getBody().asString());
}