【发布时间】:2022-01-09 21:31:11
【问题描述】:
我正在尝试使用 RestAssured 创建一个基于 cookie 的会话。然后我试图在我之前创建的 jira 票上添加评论。似乎我可以使用 Authenticate() 方法创建会话 id,但我不能在不同 java 文件中的其他方法中使用该会话。似乎 ilter(Authenticator.session) 在我的其他方法中不起作用,因为在控制台中我收到错误
您无权为此问题创建附件
但是当我在单个 java 类的 main 方法下运行所有这些时,它可以正常工作。
public class Authenticator {
public static SessionFilter session = new SessionFilter();
public static void Authenticate(){
RestAssured.baseURI = "http://localhost:8080";
String authenticationResponse = given().header("Content-Type", "application/json")
.filter(session)
.body(JiraInputs.auth())
.when().post("/rest/auth/1/session")
.then().log().all().extract().response().asString();
}
public class AddAttachment {
public static void addAttachment(){
// Add Attachment
RestAssured.baseURI="http://localhost:8080";
System.out.println(Authenticator.session.getSessionId());
given().header("X-Atlassian-Token","no-check")
.header("Content-Type","multipart/form-data")
.multiPart("file",new File("src/test/java/Repo/jira"))
.pathParam("id","10000").filter(Authenticator.session).when().
post("/rest/api/2/issue/{id}/attachments")
.then().log().all().extract().response().asString();
System.out.println(Authenticator.session.getSessionId());
}
}
public class MainClass {
public static void main(String[] args) {
// Authenticate
Authenticator.Authenticate();
AddAttachment.addAttachment();
}
}
{
"errorMessages": [
"You do not have permission to create attachments for this issue."
],
"errors": {
}
}
【问题讨论】:
-
你试过静态设置
RestAssured.filters(new SessionFilter());吗? -
对不起,我听不懂 :(,你能在一小段代码中举个例子吗?这样我就可以在我的代码中实现了。谢谢!
标签: java authentication automation automated-tests rest-assured