所以我在测试我的应用程序时遇到了问题。我正在尝试进行REST / HTTP测试。这是我的代码:
@Path("/ftpaction")
public class JerseyFileUpload {
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response postMsg(@HeaderParam("FTP-Host") String Host, @HeaderParam("FTP-Port") String Port,
@HeaderParam("FTP-User") String User, @HeaderParam("FTP-Password") String Password,
@HeaderParam("FTP-Path") String Path, @FormDataParam("file") InputStream inputStream) {
try {
InformationHandler informationHandler = new InformationHandler(Path, Host, Port, User, Password);
CountriesStructure worker = new CountriesStructure();
worker.prepareCountriesStructure(inputStream, true, informationHandler);
} catch (UsernameOrPasswordException e) {
return Response.status(401).entity("Status 401.").build();
} catch (SocketException e) {
return Response.status(404).entity("Status 404.").build();
} catch (IOException e) {
return Response.status(400).entity("Status 400.").build();
} catch (JAXBException e) {
return Response.status(500).entity("Status 500.").build();
} catch (Exception e) {
return Response.status(500).entity("Status 500.").build();
}
return Response.status(200).entity("Success!").build();
}
}
我的测试:
@RunWith(HttpJUnitRunner.class)
public class TestMain extends TestCase {
@Rule
public Destination destination = new Destination(this, "http://localhost:8080");
@Context
private Response response;
@HttpTest(method = Method.POST, path = "/JerseyWebApp/ftpaction/upload", content = "{}", file = "/CountriesList.txt", type = MediaType.MULTIPART_FORM_DATA, headers = {
@Header(name = "FTP-Host", value = "localhost"), @Header(name = "FTP-Port", value = "21"),
@Header(name = "FTP-User", value = "ftptest"), @Header(name = "FTP-Password", value = "test"),
@Header(name = "FTP-Path", value = "/test123"), @Header(name = "Accept-Encoding", value = "multipart/form-data")})
public void checkPost() {
System.out.println(response.getBody());
}
}
我有通过测试阅读文件的问题。我不知道我要做什么,因为我使用的文件是“FormDataParam”。有人知道如何上传文件以测试FormDataParam?因为现在它看不到我的文件,只返回“状态400”。