问题描述
我有一个步骤定义,用于执行基本身份验证以获取访问令牌。但是,当它调用 RestAssuredExtension 类中的 authenticateWithBasicAuth 方法时,我无法检索该字符串。
我收到错误 io.restassured.path.json.exception.JsonPathException:解析 JSON 文档失败
这是步骤定义:
@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");
}
解决方法
和朋友调试了几次后,我们发现问题是我们明确指定了 ContentType.JSON 作为内容类型。
为了确保我们可以成功运行测试,我们从 executeAPI 方法中注释掉了 request.contentType(ContentType.JSON) 行,并将内容类型作为标题添加到了 authenticateWithBasicAuth 方法的 addHeader 中:
>/**
* 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,secondParameterValue);
String token = executeAPI().getBody().jsonPath().getString("access_token");
return executeAPI().getBody().jsonPath().getString("access_token");
}
然后我在没有添加内容类型标头的情况下再次运行它,它运行良好,我能够以字符串的形式获取访问令牌。