我有一个接受输入流的put方法。我想在JUnit中使用Rest Assured调用此方法。
这是我使用的代码:
这是我使用的代码:
with().body(inpustream).put("/service/1"); // i got error 404 forbidden.
with().body(inpustream).put("/service/1"); // i got error 404 forbidden.
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
请求: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");
}
}
如需详细说明,请查看以下链接: https://restservicestesting.blogspot.in/2016/10/automating-put-request-using-rest.html
实际上,你做得很好,但是通过PUT发送多部分数据不安全且相当随意(https://jira.spring.io/browse/SPR-9079)。在spring-security.xml中添加过滤器或在这种情况下使用POST方法进行修改。
您还可以通过调用另一个没有流的PUT Web服务来尝试您的代码。
(错误代码是404还是403?)
使用MultipartFilter解决了类似的问题:Spring 3.0 FileUpload only with POST?