My EditAccount POST request get login failed but my Login Request is successful Java RestAssured

杨伟明
杨伟明
订阅者
278
文章
0
粉丝
测试交流1 171字数 354阅读1分10秒阅读模式
摘要This edit request need an access_token that was given back when I login. But when I use co...

编辑 请求需要在我登录时返回的accessu令牌。但当我使用下面的代码时,它不起作用。在此处输入图像描述。当我使用此电子邮件和密码进行登录测试时,它接收到代码和消息,这意味着我已成功登录并收到accessu令牌。但当我使用此令牌进行编辑帐户请求时,它收到了;登录失败;。有什么办法吗?

import java.util.HashMap;
import java.util.Map;
import static io.restassured.RestAssured.*;
import static org.testng.Assert.assertNotEquals;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import org.json.simple.JSONObject;
import org.testng.annotations.Test;
public class EditTest {
    Map < String, Object > map = new HashMap < String, Object > ();
    private final String JSON = "application/json";
    @Test
    public void TestCase01() {
        JSONObject request = new JSONObject();
        baseURI = "https://auction-app3.herokuapp.com/api";
        JSONObject req = new JSONObject();
        req.put("email", "ntv@gmail.com");
        req.put("password", "123456");
        Response res = given().contentType(JSON).
        body(req.toJSONString()).
        when().
        post("/login");
        System.out.println(res.getBody().asString());
        JsonPath jp = res.jsonPath();
        int code = jp.getInt("code");
        String ACCESS_TOKEN = jp.getString("access_token");
        request.put("access_token", ACCESS_TOKEN);
        request.put("email", "ninhvinh@gmail.com");
        request.put("password", "123456");
        request.put("re_pass", "123456");
        request.put("address", "MyAddress");
        request.put("name", "MyName");
        request.put("phone", "09090909090");
        request.put("avatar", null);
        Response response = given().contentType(JSON).body(request.toJSONString()).when().post("/编辑");
        System.out.println(response.getBody().asString());
        JsonPath jpath = response.jsonPath();
        assertNotEquals(jpath.getInt("code"), 1000);
    }
}

非常感谢你

 
    • lucas-nguyen-17
      lucas-nguyen-17 9

      这里有两个问题:

      首先,您尚未提取 accessu令牌,必须是

      String ACCESS_TOKEN = jp.getString("accessu令牌");

      —&燃气轮机;
      String ACCESS_TOKEN = jp.getString("data.accessu令牌");

      第二 accessu令牌 应该在标题中,而不是请求正文中。所以删除

      request.put("accessu令牌", ACCESS_TOKEN);

      并添加一个标题。
      Response response = given().contentType(JSON)
      .header("Authorization", "Bearer " + ACCESS_TOKEN)
      .body(request.toJSONString()).when().post("/edit");

      结果:

      还有一个问题是 assertNotEquals(jpath.getInt(“code”),1000);,但它并没有解决这个问题。
      如果我的答案适合你,请接受它作为正确的答案。

    匿名

    发表评论

    匿名网友
    :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
    确定

    拖动滑块以完成验证