所以这是我的功能:
@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) {
e.printStackTrace();
return Response.status(401).entity("Status 401.").build();
} catch (SocketException e) {
e.printStackTrace();
return Response.status(404).entity("Status 404.").build();
} catch (IOException e) {
e.printStackTrace();
return Response.status(400).entity("Status 400.").build();
} catch (JAXBException e) {
e.printStackTrace();
return Response.status(500).entity("Status 500.").build();
} catch (Exception e) {
e.printStackTrace();
return Response.status(500).entity("Status 500.").build();
}
return Response.status(200).entity("Success!").build();
}
我想在Junit进行测试。但我不知道如何添加标题和文件。这是我的测试(它有效):
@Test
public void firstTest()
throws ClientProtocolException, IOException {
HttpUriRequest request = new HttpGet("http://localhost:8080/JerseyWebApp/ftpaction/upload");
HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
assertThat(httpResponse.getStatusLine().getStatusCode(), equalTo(HttpStatus.SC_METHOD_NOT_ALLOWED));
}
但是我怎么能在这里添加标题和文件?
标题我认为我可以这样做:request.addHeader(value, key);
或我错了吗?
我完全不知道如何添加文件进行测试:/
答案 0 :(得分:0)
您需要使用Mockito或其他框架模拟Header对象
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("x-real-ip","127.0.0.1");