簡體   English   中英

如何使用放心測試需要身份驗證的 Rest API

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

我想在獲得 Json 響應之前測試需要身份驗證的 Rest API。 對於前。 如果我想訪問rest API: http://192.168.xx.xx:9000/dashboards/all/list/m1/p1/sch1

那么

如果我還沒有登錄,那么這會將我重定向到登錄 HTML 頁面,登錄后,這將顯示 Json 輸出。

現在我想用 java 編寫一個同樣的放心代碼:我不知道,這是否可以使用它進行登錄。

所以我寫了一個簡單的代碼:

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);
    }

因此,這里不是獲取 Json 響應,而是獲取 HTML 源頁面響應。

所以,我的問題是,如果可能的話,如何登錄並獲得 Json 響應。

提前致謝。

如果您使用 JIRA 的默認身份驗證,那么您需要搶占式身份驗證。 下面是示例代碼。

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

示例代碼將幫助您在發送每個請求之前進行身份驗證。

這個方法效果很好。 除了提交登錄憑證,還同時驗證200狀態碼

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

你可以試試這個:

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

您可能正在尋找表單身份驗證:

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

(可選)您需要提供 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);
        
    }
}

下面的代碼對我有用:

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

參考 - 示例 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