Bearer令牌未包含在请求中

huangapple go评论124阅读模式
英文:

Authorization Bearer token is not included for request

问题

我正在尝试使用Serenity和Rest-Assured运行我的自动测试,但令牌未包含在我的请求生成方法中。获取令牌本身的方法是正确的。告诉我可能的原因。

  1. public class ApiSteps {
  2. private String token;
  3. public String getAccessToken() {
  4. RequestSpecification requestSpec = RestAssured.with();
  5. requestSpec.given().contentType("application/x-www-form-urlencoded");
  6. Response giveToken = RestAssured.given()
  7. .formParam("username", "user")
  8. .formParam("password", "pass")
  9. .request().post("https://test.com/token");
  10. DocumentContext doc = JsonPath.parse(giveToken.asString());
  11. token = doc.read("access_token");
  12. System.out.println(token);
  13. return token;
  14. }
  15. final RequestSpecification spec = new RequestSpecBuilder()
  16. .setBaseUri("https://test.com/api/v1")
  17. .addHeader("Content-Type","application/json")
  18. .addHeader("Accept", "text/plain")
  19. .addHeader("Authorization", "Bearer " + getAccessToken())
  20. .build();
  21. @Step
  22. public void testRest() {
  23. given()
  24. .spec(spec)
  25. .when()
  26. .get("/Test")
  27. .then()
  28. .assertThat()
  29. .statusCode(200);
  30. }
  31. }

在请求中启动测试时,令牌为null。我尝试使用注解@Before标记,但结果仍然相同。

Bearer令牌未包含在请求中

英文:

I am trying to run my autotests with serenity and rest-assured, but the token is not included in my request generation method. The method of getting the token itself works correctly. tell me what could be the reason.

  1. public class ApiSteps {
  2. private String token;
  3. public String getAccessToken() {
  4. RequestSpecification requestSpec = RestAssured.with();
  5. requestSpec.given().contentType("application/x-www-form-urlencoded");
  6. Response giveToken = RestAssured.given()
  7. .formParam("username", "user")
  8. .formParam("password", "pass")
  9. .request().post("https://test.com/token");
  10. DocumentContext doc = JsonPath.parse(giveToken.asString());
  11. token = doc.read("access_token");
  12. System.out.println(token);
  13. return token;
  14. }
  15. final RequestSpecification spec = new RequestSpecBuilder()
  16. .setBaseUri("https://test.com/api/v1")
  17. .addHeader("Content-Type","application/json")
  18. .addHeader("Accept", "text/plain")
  19. .addHeader("Authorization", "Bearer " + getAccessToken())
  20. .build();
  21. @Step
  22. public void testRest() {
  23. given()
  24. .spec(spec)
  25. .when()
  26. .get("/Test")
  27. .then()
  28. .assertThat()
  29. .statusCode(200);
  30. }
  31. }

when starting a test in a request - null. I tried to mark with the annotation @Before, but the result is the same

Bearer令牌未包含在请求中

答案1

得分: 0

你需要将获取令牌的方法移到一个单独的类中,并按以下方式使用它:

  1. NameClass nameClass = new NameClass();
  2. .nameClass.addHeader("Authorization", "Bearer " + getAccessToken())

然后在这个类中使用该方法:

  1. nameClass.getAccessToken()
英文:

You need to move the method for getting the token into a separate class and use it as follows:

  1. NameClass nameClass = new NameClass();

> .addHeader("Authorization", "Bearer " + getAccessToken())

and here use the method of this class:

  1. nameClass.getAccessToken()

huangapple
  • 本文由 发表于 2020年8月5日 00:48:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63251516.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定