Restlet - JUnit - 测试 MULTIPART_FORM_DATA 表单 Post

Restlet - JUnit - testing MULTIPART_FORM_DATA form Post

我想知道使用 JUnit 测试表单资源的最佳方法是什么 post?

在 @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());

}

我不知道您在服务器应用程序中遇到的错误到底是什么。如果我们能有一个堆栈跟踪,我会很有趣。

也就是说,您可以使用 org.restlet.ext.html 扩展以编程方式构建 HTML 表单。有关详细信息,您可以阅读此博客 post: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);

希望对你有帮助, 蒂埃里