【问题标题】:Not parsing JSON document to return access token variable - Rest Assured / Java不解析 JSON 文档以返回访问令牌变量 - 放心/Java
【发布时间】:2021-05-30 13:15:41
【问题描述】:

我有一个步骤定义,旨在执行基本身份验证以获取访问令牌。但是,当它调用 RestAssuredExtension 类中的 authenticateWithBasicAuth 方法时,我无法检索该字符串。

我收到错误io.restassured.path.json.exception.JsonPathException: Failed to parse the JSON document

这是步骤定义:

  @And("I perform basic auth operation to get access token")
    public void i_Perform_Basic_Auth_Operation_To_Get_Access_Token() throws Throwable {

        String username = CommonTestData.consumerKey;
        String password = CommonTestData.consumerSecret;
        String firstParameterName = CommonTestData.basicAuthQueryParamsKey1Variable;
        String firstParameterValue = CommonTestData.basicAuthQueryParamsValue1Variable;
        String secondParameterName = CommonTestData.basicAuthQueryParamsKey2Variable;
        String secondParameterValue = CommonTestData.basicAuthQueryParamsValue2Variable;

        RestAssuredExtension restAssuredExtension = new RestAssuredExtension(APIConstant.ApiMethods.POST,null);
        token = restAssuredExtension.authenticateWithBasicAuth(username, password, firstParameterName, firstParameterValue, secondParameterName, secondParameterValue);

        System.out.println("access_token is " + token);
    }

这是通用的 RestAssuredExtension 类和方法:

我认为问题出在return executeAPI().getBody().jsonPath().getString("access_token");,因为我无法解析 JSON 对象?这就是我被困的地方。

public class RestAssuredExtension {

    private RequestSpecBuilder builder = new RequestSpecBuilder();
    private String method;
    private String url;

    /**
     * RestAssuredExtension constructor to pass the initial settings for the the following method
     * @param method
     * @param token
     */
    public RestAssuredExtension(String method, String token) {

        //Formulate the API url
        this.url = "https://api.business.govt.nz/services/token";
        this.method = method;

        if(token != null)
            builder.addHeader("Authorization", "Bearer " + token);
    }

    /**
     * ExecuteAPI to execute the API for GET/POST/DELETE
     * @return ResponseOptions<Response>
     */
    private ResponseOptions<Response> executeAPI() {
        RequestSpecification requestSpecification = builder.build();
        RequestSpecification request = RestAssured.given();
        request.contentType(ContentType.JSON);
        request.spec(requestSpecification);

        if(this.method.equalsIgnoreCase(APIConstant.ApiMethods.POST))
            return request.post(this.url);
        else if(this.method.equalsIgnoreCase(APIConstant.ApiMethods.DELETE))
            return request.delete(this.url);
        else if(this.method.equalsIgnoreCase(APIConstant.ApiMethods.GET))
            return request.get(this.url);
        else if(this.method.equalsIgnoreCase(APIConstant.ApiMethods.PUT))
            return request.get(this.url);
        return null;
    }


    /**
     * Authenticate to get the token variable
     * @return string token
     */
    public String authenticateWithBasicAuth(String username, String password, String firstParameterName, String firstParameterValue, String secondParameterName, String secondParameterValue){
        builder.setAuth(RestAssured.preemptive().basic(username, password)).addQueryParam(firstParameterName, firstParameterValue).addQueryParam(secondParameterName, secondParameterValue);
        return executeAPI().getBody().jsonPath().getString("access_token");
    }

【问题讨论】:

  • @fakataha 你对此有什么建议吗?
  • 或@Kartmcad 对此有何建议?

标签: java json rest-assured jsonpath rest-assured-jsonpath


【解决方案1】:

上面和朋友调试了几次后,我们发现问题是我们明确指定 ContentType.JSON 作为内容类型。

所以我们做了什么来确保我们可以成功运行测试,我们从 executeAPI 方法中注释掉了行 request.contentType(ContentType.JSON) 并将 Content 类型作为头添加到 addHeader 到 authenticateWithBasicAuth 方法中:

/**
     * Authenticate to get the token variable
     * @return string token
     */
    public String authenticateWithBasicAuth(String username, String password, String firstParameterName, String firstParameterValue, String secondParameterName, String secondParameterValue){
//        builder.setAuth(RestAssured.preemptive().basic(username, password)).addQueryParam(firstParameterName, firstParameterValue).addQueryParam(secondParameterName, secondParameterValue).addHeader("Content-Type", "application/x-www-form-urlencoded");
        builder.setAuth(RestAssured.preemptive().basic(username, password)).addQueryParam(firstParameterName, firstParameterValue).addQueryParam(secondParameterName, secondParameterValue);
        String token = executeAPI().getBody().jsonPath().getString("access_token");
        return executeAPI().getBody().jsonPath().getString("access_token");
    }

然后我在没有添加内容类型标头的情况下再次运行它,它工作正常,我能够将访问令牌作为字符串获取。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-16
    • 2019-01-18
    • 1970-01-01
    • 2016-05-27
    • 1970-01-01
    • 2023-04-03
    相关资源
    最近更新 更多