简体   繁体   English

如何使用放心测试需要身份验证的 Rest API

[英]How to test Rest API which require authentication using Rest assured

I want to test a Rest API which require authentication, before getting the Json response.我想在获得 Json 响应之前测试需要身份验证的 Rest API。 FOr exa.对于前。 If i want to visit rest API: http://192.168.xx.xx:9000/dashboards/all/list/m1/p1/sch1如果我想访问rest API: http://192.168.xx.xx:9000/dashboards/all/list/m1/p1/sch1

then那么

if I am not already logged in , then this will redirect me to Login HTML page, and after login, this will show me the Json output.如果我还没有登录,那么这会将我重定向到登录 HTML 页面,登录后,这将显示 Json 输出。

Now I want to write a Rest assured code in java for same: I dont know , whether this is possible to do login using this or not.现在我想用 java 编写一个同样的放心代码:我不知道,这是否可以使用它进行登录。

SO I written a simple code for same::所以我写了一个简单的代码:

public class TestNGSimpleTest1 {

    @Test
    public void testAdd() {
            //expect().
            //statusCode(400).
            //body("Status", equalTo("Success")).
            //when().
            //get("http://localhost:9000/dashboards/all/list/m1/p1/sch1");
            //System.out.println("Response received is ::" +res);   
            Response res = get("http://localhost:9000/dashboards/all/list/m1/p1/sch1");
            assertEquals(200,res.getStatusCode());
            String json = res.asString();
            System.out.println("Response received is :: " +json);
    }

So here instead of getting the Json response, I am getting the HTML source page response.因此,这里不是获取 Json 响应,而是获取 HTML 源页面响应。

So, my question is if possible, how to do login and get the Json response.所以,我的问题是,如果可能的话,如何登录并获得 Json 响应。

Thanks in advance.提前致谢。

If you are using default authentication from JIRA, then preemptive authentication is what you need.如果您使用 JIRA 的默认身份验证,那么您需要抢占式身份验证。 Below are the sample code.下面是示例代码。

RestAssured.baseURI = System.getProperty("baseurl");
PreemptiveBasicAuthScheme authScheme = new PreemptiveBasicAuthScheme();
authScheme.setUserName("admin");
authScheme.setPassword("admin");
RestAssured.authentication = authScheme;

The sample code will help you do the authentication before every request you send.示例代码将帮助您在发送每个请求之前进行身份验证。

This method works great.这个方法效果很好。 In addition to submitting the login credentials, it also verifies the 200 status code at the same time除了提交登录凭证,还同时验证200状态码

given().auth().basic(username, password).when().get("/uri/").then().statusCode(200);

你可以试试这个:

given().auth().preemptive().basic(username, password).when().get("{yourApiURL}").then().statusCode(200);

You probably looking for form authentication:您可能正在寻找表单身份验证:

given().auth().form("username", "password"). .. 

Optionally you need to provide an instance of FormAuthConfig as a third parameter to describe how your HTML login page looks like. (可选)您需要提供 FormAuthConfig 的实例作为第三个参数来描述您的 HTML 登录页面的外观。

package jira_APIProject;

import org.testng.annotations.Test;

import io.restassured.RestAssured;
import io.restassured.authentication.PreemptiveBasicAuthScheme;

import static io.restassured.RestAssured.*;

import java.util.Base64;

public class TestBasicAuth {

    @Test
    public void auth() {
        
        RestAssured.baseURI = "https://jjjjjjj-home.atlassian.net/";
        PreemptiveBasicAuthScheme authScheme = new PreemptiveBasicAuthScheme();
        authScheme.setUserName("kkkkkkkkkk.kkkkkkkkkk@gmail.com");
        authScheme.setPassword("lllllllll8U1XLsXZ02927");
        RestAssured.authentication = authScheme;

        given().header("Content-Type","application/json")
        .body("{\r\n" + 
                "    \"body\": \"Updated on 22nd april.\",\r\n" + 
                "    \"visibility\": {\r\n" + 
                "        \"type\": \"role\",\r\n" + 
                "        \"value\": \"Administrator\"\r\n" + 
                "    }\r\n" + 
                "}")
        
        .when().post("rest/api/2/issue/10004/comment")
        .then().log().all().assertThat().statusCode(201);
        
    }
}

Below code worked for me :下面的代码对我有用:

JsonPath response = RestAssured
.given()
    .header("Authorization", "Basic " + encodedString)
    .when()
    .get(GET_PUSH_API)
.then()
    .statusCode(401)
    .extract().jsonPath();

Reference - Example 14 : https://www.programcreek.com/java-api-examples/index.php?api=com.jayway.restassured.RestAssured参考 - 示例 14: https : //www.programcreek.com/java-api-examples/index.php?api=com.jayway.restassured.RestAssured

public void LoginRequest() {
    RestAssured.baseURI = BASE_URL;
    RequestSpecification request  = RestAssured.given().auth().preemptive().basic(USERNAME, PASSWORD);
    response = request.get("​/Account​/v1​/User");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM