【问题标题】:How to test Rest API which require authentication using Rest assured如何使用放心测试需要身份验证的 Rest API
【发布时间】:2014-01-05 08:49:45
【问题描述】:

我想在获得 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 响应。

提前致谢。

【问题讨论】:

  • 如果您需要抢占式身份验证,请参阅this link here,它显示了如何进行基本或 OAut 身份验证或this link here。
  • 其实我也试过这个链接,但问题是它重定向的登录页面,一个html页面..使用你的链接,这只是显示HTML页面。
  • 您使用什么类型的身份验证?只是一个登录页面,其中包含一个检查输入的用户名和密码的支持数据库调用 - 所以没有基本身份验证或 OAuth?这似乎很奇怪,但特别是对于主要用于由应用程序而不是人类访问的基于 REST 的 Web 服务(因此在表单字段中没有输入并单击提交按钮)。你使用什么服务器框架? CXF,Restlet,...?您是否已经尝试使用 SoapUI 连接到您的 REST 服务?
  • 我会尽快更新它,因为我不确定您要求的答案。每当我发出像 192.168.xx.xx:9000/dashboards/all/list/m1/p1/sch1 这样的 http 请求时,它都会重定向到 HTML 登录页面,在该页面中要求输入用户名和密码。明天我会得到更多细节,并会更新你。谢谢。
  • @RomanVottner 与我们的开发团队讨论.. 他们设计了 Rest API,使得任何 Rest API 请求都将重定向到 HTML 登录页面。他们同意在 QA 测试环境中更改此身份验证行为,但这是正确的方法吗??

标签: java json rest testng rest-assured


【解决方案1】:

如果您使用 JIRA 的默认身份验证,则需要抢先式身份验证。下面是示例代码。

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

示例代码将帮助您在发送每个请求之前进行身份验证。

【讨论】:

  • 非常适合基本身份验证。
【解决方案2】:

这种方法效果很好。除了提交登录凭证外,还同时验证200状态码

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

【讨论】:

    【解决方案3】:

    你可以试试这个:

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

    【讨论】:

      【解决方案4】:

      您可能正在寻找表单身份验证:

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

      您还需要提供一个 FormAuthConfig 实例作为第三个参数来描述您的 HTML 登录页面的外观。

      【讨论】:

        【解决方案5】:
        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);
                
            }
        }
        

        【讨论】:

          【解决方案6】:

          以下代码对我有用:

          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

          【讨论】:

            【解决方案7】:
            public void LoginRequest() {
                RestAssured.baseURI = BASE_URL;
                RequestSpecification request  = RestAssured.given().auth().preemptive().basic(USERNAME, PASSWORD);
                response = request.get("​/Account​/v1​/User");
            

            【讨论】:

            • 虽然此代码可以解决问题,including an explanation 说明如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提出问题的人。请edit您的回答添加解释并说明适用的限制和假设。
            猜你喜欢
            • 2018-05-10
            • 2017-08-22
            • 1970-01-01
            • 2018-07-04
            • 2017-07-28
            • 1970-01-01
            • 2016-01-09
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多