我想知道使用JUnit测试表单帖子资源的最佳方法是什么?
在@Get上,我通过资源获取服务值,其中包含以下内容:
@Test
public void testGetCollections() throws Exception {
String url ="http://localhost:14502/api/v1/collections";
Client client = new Client(Protocol.HTTP);
ChallengeResponse challengeResponse = new ChallengeResponse(ChallengeScheme.HTTP_BASIC,"user", "f399b0a660f684b2c5a6b4c054f22d89");
Request request = new Request(Method.GET, url);
request.setChallengeResponse(challengeResponse);
Response response = client.handle(request);
System.out.println("request"+response.getStatus().getCode());
System.out.println("request test::"+response.getEntityAsText());
assertEquals(200, response.getStatus().getCode());
ObjectMapper mapper = new ObjectMapper();
List<Collection> collectionList = mapper.readValue(response.getEntityAsText(), new TypeReference<List<Collection>>(){});
for(Collection collection : collectionList){
System.out.println("TITLE: "+collection.getTitle());
}
assertTrue(collectionList.size()>0);
}
在@Post上我试图做以下事情:
@Test
public void testPostCollections() throws Exception {
String url ="http://localhost:14502/api/v1/collections";
Client client = new Client(Protocol.HTTP);
ChallengeResponse challengeResponse = new ChallengeResponse(ChallengeScheme.HTTP_BASIC,"user", "f399b0a660f684b2c5a6b4c054f22d89");
Request request = new Request(Method.POST, url);
ClientInfo info = new ClientInfo(MediaType.APPLICATION_JSON);
info.getAcceptedMediaTypes().add(
new Preference<MediaType>(MediaType.APPLICATION_JSON));
request.setClientInfo(info);
request.setEntity(
"collectionName=testCollection123&collectionDescription=testCollectionDescription123",
MediaType.MULTIPART_FORM_DATA);
request.setChallengeResponse(challengeResponse);
Response response = client.handle(request);
//boolean valid = false;
System.out.println("request"+response.getStatus().getCode());
System.out.println("request test::"+response.getEntityAsText());
assertEquals(200, response.getStatus().getCode());
}
我收到以下500错误: 服务器遇到意外情况,导致无法完成请求。
基于以下发布的答案,我做了以下工作方法:
@Test
public void testAssetsPost() throws Exception {
ClientResource cr = new ClientResource("http://localhost:14502/api/v1/ass");
FormDataSet fds = new FormDataSet();
fds.getEntries().add(new FormData("metaData", "metaDataTest123"));
fds.setMultipart(true);
FormData fileRep = new FormData("file",
new FileRepresentation(new File("/Users/og/Documents/gump.jpg"),
MediaType.IMAGE_JPEG));
fds.getEntries().add(fileRep);
FormData fileRep2 = new FormData("associatedDoc",
new FileRepresentation(new File("/Users/og/Documents/gump.jpg"),
MediaType.IMAGE_JPEG));
fds.getEntries().add(fileRep2);
Representation r = null;
try{
r = cr.post(fds);
} catch (ResourceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println( "Got Context: " + cr.getContext() );
System.out.println( "Got Response: " + cr.getResponse());
System.out.println( "Got Resonse Attribute : " + cr.getResponseAttributes() );
System.out.println( "Got Resonse Entity: " + cr.getResponseEntity() );
System.out.println("Got response !! , response : " + r.getText());
System.out.println(r.getText());
}
答案 0 :(得分:1)
我不知道您在服务器应用程序中遇到的错误是什么。如果我们可以有堆栈跟踪,我会很有趣。
也就是说,您可以使用org.restlet.ext.html扩展以编程方式构建HTML表单。有关详细信息,请阅读此博文:http://restlet.com/blog/2015/10/27/implementing-file-upload-with-restlet-framework/。
乍一看,媒体类型不正确,因为您不发送多部分表单而是简单表单。因此,您应该使用MediaType.APPLICATION_WWW_FORM
代替MediaType.MULTIPART_FORM_DATA
。
包含文件的表单示例:
Form fileForm = new Form();
fileForm.add(Disposition.NAME_FILENAME, "myimage.png");
Disposition disposition = new Disposition(Disposition.TYPE_INLINE, fileForm);
FileRepresentation entity = new FileRepresentation(f, MediaType.IMAGE_PNG);
entity.setDisposition(disposition);
对于一个简单的形式:
FormDataSet fds = new FormDataSet();
fds.setMultipart(true);
FormData fdFile = new FormData("fileToUpload", entity);
fds.getEntries().add(fdFile);
FormData fdValue = new FormData("field", "value");
fds.getEntries().add(fdValue);
希望它可以帮到你, 亨利