简体   繁体   English

不解析 JSON 文档以返回访问令牌变量 - Rest Assured / Java

[英]Not parsing JSON document to return access token variable - Rest Assured / Java

I have a step definition that is meant to perform a basic auth to get an access token.我有一个步骤定义,旨在执行基本身份验证以获取访问令牌。 However I am not able to retrieve the string when it calls the authenticateWithBasicAuth method in the RestAssuredExtension class.但是,当它调用RestAssuredExtension class 中的authenticateWithBasicAuth方法时,我无法检索该字符串。

I am getting the error io.restassured.path.json.exception.JsonPathException: Failed to parse the JSON document我收到错误io.restassured.path.json.exception.JsonPathException:无法解析 JSON 文档

This is the Step definition:这是步骤定义:

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

This is the generic RestAssuredExtension class & methods:这是通用的 RestAssuredExtension class 和方法:

I think the issue is return executeAPI().getBody().jsonPath().getString("access_token");我认为问题是return executeAPI().getBody().jsonPath().getString("access_token"); as I can't parse the JSON object?因为我无法解析 JSON object? This is where I'm stuck at.这就是我坚持的地方。

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

After debugging the above a few times with a friend, we found that the issue was we were explicitly specifying ContentType.JSON as the Content type.和朋友一起调试了几次之后,我们发现问题是我们明确指定 ContentType.JSON 作为内容类型。

So what we did to ensure we could run the test successfully, we commented out the line request.contentType(ContentType.JSON) from the executeAPI method and added the Content type as a header in addHeader to the authenticateWithBasicAuth method:所以我们做了什么来确保我们可以成功运行测试,我们从 executeAPI 方法中注释掉了行 request.contentType(ContentType.JSON) 并将 Content 类型添加为 addHeader 中的 header 到 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");
    }

I then ran it again without adding the Content type header and it was working fine and I was able to get the access token as a String.然后我再次运行它而不添加内容类型 header 并且它工作正常并且我能够将访问令牌作为字符串获取。

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

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