java - 不解析 JSON 文档以返回访问令牌变量 - 放心/Java
问题描述
我有一个步骤定义,旨在执行基本身份验证以获取访问令牌。但是,当它调用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");
}
解决方案
和朋友一起调试了几次之后,我们发现问题是我们明确指定 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");
}
然后我再次运行它而不添加内容类型标头,它工作正常,我能够将访问令牌作为字符串获取。
推荐阅读
- node.js - 错误:找不到模块“./models/modela”
- c# - 用大写替换中间字母
- rust - 为什么 '?' 运算符使用 From 而不是 Into?
- c++ - 将模板类的成员初始化为此时的编译器警告
- java - 如何获取 EditText 的单行,将其添加到 Listview
- c# - 如何从我的 csv 文件的第二列中读取信息,并使用存储在该列每一行中的数据进行计算?
- javascript - 如何检查sqlite3 nodejs中是否存在表
- vue.js - vuejs 嵌套 json 渲染
- mongodb - 单击时如何仅显示带有任务的集合条目:“测试”?
- python - 将带有数组的 json 数据转换为 CSV