我有一个接受输入流的 put 方法。我想在 JUnit 中使用放心调用此方法。
这是我使用的:
with().body(inpustream).put("/service/1"); // i got error 404 forbidden.
我有一个接受输入流的 put 方法。我想在 JUnit 中使用放心调用此方法。
这是我使用的:
with().body(inpustream).put("/service/1"); // i got error 404 forbidden.
POST 将返回状态码 201,PUT 将返回 200,POST 将创建一个新资源,但 PUT 将更新现有资源。这意味着我们必须在 URI 本身中提及我们希望更新的资源,如下所示。
import io.restassured.RestAssured;
import static io.restassured.RestAssured.*;
import java.util.HashMap;
import java.util.Map;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import static org.hamcrest.Matchers.*;
public class PUTMethod {
public static Map<String, String> map = new HashMap<String, String>();
@BeforeTest
public void putdata(){
map.put("userId", "2");
map.put("id", "19");
map.put("title", "this is projectdebug.com");
map.put("body", "i am testing REST api with REST-Assured and sending a PUT request.");
RestAssured.baseURI = "http://jsonplaceholder.typicode.com";
RestAssured.basePath = "/posts/";
}
@Test
public void testPUT(){
given()
.contentType("application/json")
.body(map)
.when()
.put("/100")
.then()
.statusCode(200)
.and()
.body("title", equalTo("this is projectdebug.com"));
}
}
访问http://www.projectdebug.com/send-put-request-using-rest-assured/ 了解更多信息。
实际上,您做得很好,但是通过 PUT 发送多部分是不安全的并且是非常随机的(https://jira.spring.io/browse/SPR-9079)。在这种情况下,修改您的 spring-security.xml 以添加过滤器或使用 POST 方法。
您还可以通过调用另一个没有流的 PUT 网络服务来尝试您的代码。
(错误代码是什么?404 还是 403?)
使用 MultipartFilter 解决了类似的问题:Spring 3.0 FileUpload only with POST?
Have a look at the following example, where it explains how to use PUT
request using Rest Assured:
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import static com.jayway.restassured.RestAssured.*;
import com.jayway.restassured.RestAssured;
import com.jayway.restassured.response.Response;
public class GetStatusCodeTest {
@BeforeClass
public void setBaseUri () {
RestAssured.baseURI = "https://localhost:3000";
}
@Test
public void updateUsingPut () {
Posts post = new Posts();
post.setId ("3");
post.setTitle ("Hello Bhutan");
post.setAuthor ("StaffWriter");
given().body (post)
.when ()
.contentType (ContentType.JSON)
.put ("/posts/3");
}
}
For detailed explanation, you may check out the following link: https://restservicestesting.blogspot.in/2016/10/automating-put-request-using-rest.html