I'm developing a set of REST services, using RESTEasy and WildFly 9.0.2.
One of them allows me to upload a file.
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response upload(MultipartFormDataInput input);
I successfully managed to call the REST service and upload the file to the server, so I started to implement the integration tests.
For that, I'm using an embedded Grizzly HTTP server:
@Before
public void setUp() throws Exception {
// Weld and container initialization
weld = new Weld();
weld.initialize();
ResourceConfig resourceConfig = new ResourceConfig().packages("...");
server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), resourceConfig);
}
The problem arose when I tried to test the newly created service:
@Test
public void hubbleMapServiceUploadMapTest() throws FileNotFoundException, IOException {
Client client = null;
WebTarget webTarget = null;
Builder builder = null;
Response response = null;
try {
client = ClientBuilder.newBuilder().build();
webTarget = client.target(BASE_URI).path("/service/upload");
builder = webTarget.request(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN, MediaType.WILDCARD);
response = builder.post(Entity.entity(getEntityAsMultipart(), MediaType.MULTIPART_FORM_DATA));
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (response != null) {
response.close();
}
if (client != null) {
client.close();
}
}
assertNotNull(response);
assertThat(response.getStatus(), is(Status.OK.getStatusCode()));
}
private GenericEntity<MultipartFormDataOutput> getEntityAsMultipart() {
String resource = "my.file";
MultipartFormDataOutput mfdo = new MultipartFormDataOutput();
try {
mfdo.addFormData("file",
new FileInputStream(new File(getClass().getClassLoader().getResource(resource).getFile())),
MediaType.APPLICATION_OCTET_STREAM_TYPE, resource);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
GenericEntity<MultipartFormDataOutput> entity = new GenericEntity<MultipartFormDataOutput>(mfdo) {
};
return entity;
}
The response code is always 415 Unsupported Media Type.
All the remaining integration tests work properly.
What am I doing wrong? Do I need, somehow, to enable Grizzly's Multipart feature?