【问题标题】:Cannot use sessionfilter parameter in my other classes无法在我的其他课程中使用 sessionfilter 参数
【发布时间】: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


【解决方案1】:

静态设置可以解决这个问题。 RestAssured.filters(new SessionFilter());

删除public static SessionFilter session = new SessionFilter();

public class Authenticator {

  public static void Authenticate(){
    RestAssured.baseURI = "http://localhost:8080";

    String authenticationResponse = given().header("Content-Type", "application/json")
            .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"; 

    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")
    .when()
            .post("/rest/api/2/issue/{id}/attachments")
            .then().log().all().extract().response().asString();
  }
}


public class MainClass {

    public static void main(String[] args) {
        RestAssured.filters(new SessionFilter());
        Authenticator.Authenticate();
        AddAttachment.addAttachment();
    }
}

【讨论】:

    猜你喜欢
    • 2015-03-08
    • 2015-04-10
    • 2018-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    • 2016-03-31
    • 1970-01-01
    相关资源
    最近更新 更多