【问题标题】:Rest Assured Bearer authentication放心承载认证
【发布时间】:2022-04-13 21:02:29
【问题描述】:

我不熟悉使用 Rest Assured、Java 和 Api 测试,所以请对我温柔一点。当我使用放心测试使用承载身份验证的 api 时,测试失败导致:- java.net.ConnectException:连接被拒绝:连接

我知道问题可能与身份验证有关,但不确定如何使用“承载”。我四处搜索并相信我需要以某种方式使用我的用户名和密码发出初始请求。然后取回一个令牌用于承载身份验证。 请有人可以通过一个非常简单的示例帮助我做到这一点吗?

我的代码是

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

@BeforeTest
    public void setUp() {
        RestAssured.enableLoggingOfRequestAndResponseIfValidationFails();
        RestAssured.authentication =   preemptive().basic("username","password");

}

@Test
public void successfulTest() {
    given()
            .contentType("application/json; charset=UTF-8");

    when().

            get("http://mydomain/testpath/Id=2").
    then().
            statusCode(200);

}

【问题讨论】:

    标签: java api authentication rest-assured bearer-token


    【解决方案1】:
    Response response =
          given()
              .headers(
                  "Authorization",
                  "Bearer " + bearerToken,
                  "Content-Type",
                  ContentType.JSON,
                  "Accept",
                  ContentType.JSON)
              .when()
              .get(url)
              .then()
              .contentType(ContentType.JSON)
              .extract()
              .response();
    

    【讨论】:

      【解决方案2】:

      为了获得不记名令牌,您可以使用此代码来授权您的请求:

      PreemptiveBasicAuthScheme authScheme = new PreemptiveBasicAuthScheme();
      authScheme.setUserName("login");
      authScheme.setPassword("password");
      RestAssured.authentication = authScheme;
      

      获得令牌后,以这种方式在您的请求中发送它:

      response = given().auth().oauth2(token).get("http://mydomain/testpath/Id=2");
      

      【讨论】:

        【解决方案3】:

        我的 Cucumber 步骤定义如下所示:

            // Class variables
            private String token_resource = "/yourApp/oauth/token?username=";
            private String endpoint_rest="https://your.app.domain.com/";
            private String acessToken;
        
            @When("^user gets access token using userId \"(.+)\" and password \"(.+)\"$")
        public void getAccessToken(String userName, String password){
            RequestSpecification requestSpec = RestAssured.with();
            requestSpec.given().contentType("application/json");
            requestSpec.headers("Authorization", "Basic  your-string-here");
            Response response = requestSpec.post(endpoint_rest + token_resource + userName + "&password=" + password + "&client_id=yourApp&grant_type=password");
            String responseMsg = response.asString();
            System.out.println(">> responseMsg=" + responseMsg);
            assertTrue("Missing access token",responseMsg.contains("access_token"));
            System.out.println(">> Get Access token RESPONSE: " + responseMsg);
        
            DocumentContext doc = JsonPath.parse(responseMsg);
            acessToken= doc.read("access_token");
        
            System.out.println(" >> doc.read access_token= " + acessToken);  
        }
        

        很大程度上取决于您的端点是如何编码的。

        当我想学习这种东西时,我会去放心examples 并搜索。

        例如Here

        【讨论】:

          【解决方案4】:

          如果错误是“连接被拒绝”,这听起来更像是网络问题而不是身份验证。基本上,您还没有达到使用该服务对您的客户进行身份验证的地步。 您可以检查您的服务是否在 80 以外的其他端口上运行。如果是这种情况,只需在发送请求之前提供该端口即可:

          given().port(your_port_number)
          

          您可以使用更可视化的 REST 客户端应用程序来尝试您的请求,以确保它确实有效,然后再将其放入您的代码中。 “邮递员”可能是一个不错的候选人。

          【讨论】:

            【解决方案5】:
            Response postResponse = given().headers(cookieName, "Bearer " + cookieValue, "Content-Type",
                            ContentType.JSON, "Accept", ContentType.JSON)
                .when().get(url).then().contentType(ContentType.JSON)
                .extract().response();
            

            【讨论】:

              猜你喜欢
              • 2017-11-19
              • 1970-01-01
              • 2015-10-18
              • 2016-06-13
              • 2014-10-11
              • 2019-04-29
              • 2016-11-11
              • 2019-06-27
              • 2018-09-16
              相关资源
              最近更新 更多