英文:
java.lang.AssertionError: 1 expectation failed. Expected status code <200> but was <500>
问题
我正在尝试进行一个PATCH调用,希望能更新一条记录,但是我收到了500错误。我已经检查过端点是否正确。测试代码如下:
import org.json.simple.JSONObject;
import org.testng.annotations.Test;
import io.restassured.http.ContentType;
import static io.restassured.RestAssured.*;
public class Test03_PartialUpdateBooking {
@Test
public void PartialUpdateBooking() {
JSONObject payload = new JSONObject();
payload.put("firstname", "Tiger");
payload.put("lastname", "Test");
System.out.println(payload);
System.out.println(payload.toJSONString());
given().
header("Authorization", "Basic YWRtaW46cGFzc3dvcmQxMjM=").
contentType(ContentType.JSON).
accept(ContentType.JSON).
header("cookie", "token=abc123").
body(payload.toJSONString()).
when().
patch("https://restful-booker.herokuapp.com/booking/2").
then().
statusCode(200).
log().all();
}
}
我期望代码能够正确运行并返回状态码200,但实际上返回了状态码500,这意味着它无法连接到服务器本身。该请求在Postman中运行良好。有人可以提供帮助吗?
英文:
I am trying to do a PATCH call where i expect to update a record but i am getting 500 error, I checked the endpoint is mentioned correctly.The test code is as mentioned below:
import org.json.simple.JSONObject;
import org.testng.annotations.Test;
import io.restassured.http.ContentType;
import java.util.Map;
import static io.restassured.RestAssured.*;
public class Test03_PartialUpdateBooking {
@Test
public void PartialUpdateBooking() {
JSONObject payload = new JSONObject();
payload.put("firstname","Tiger");
payload.put("lastname","Test");
//String AuthToken ="Basic YWRtaW46cGFzc3dvcmQxMjM=";
System.out.println(payload);
System.out.println(payload.toJSONString());
given().
header("Authorization", "Basic YWRtaW46cGFzc3dvcmQxMjM=").
//header("Authorization","AuthToken").
contentType(ContentType.JSON).
accept(ContentType.JSON).
header("cookie","token=abc123").
body(payload.toJSONString()).
when().
patch("https://restful-booker.herokuapp.com/booking/2").
then().
statusCode(200).
log().all();
}
}
i am expecting to run the code correctly and return 200 but it is returning the 500 which means it is unable to reach the server itself. The request runs fine in postman.Can someone help here?
答案1
得分: 0
问题是这里的ContentType.JSON
不等于application/json
。对我来说,这似乎是一个错误,但我不知道背后的原因。
修复方法将是
contentType("application/json").
accept("application/json").
英文:
Problem is ContentType.JSON
here is not equal application/json
. It seems a bug to me, but I don't know the reason behind it.
The fix would be
contentType("application/json").
accept("application/json").
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论