use of io.micronaut.http.client.multipart.MultipartBody in project micronaut-core by micronaut-projects.
the class UploadControllerSpec method testCompletedFileUploadWithNoPart.
@Test
public void testCompletedFileUploadWithNoPart() {
MultipartBody body = MultipartBody.builder().addPart("filex", "", MediaType.APPLICATION_JSON_TYPE, new byte[0]).build();
Flux<HttpResponse<String>> flowable = Flux.from(client.exchange(HttpRequest.POST("/upload/completed", body).contentType(MediaType.MULTIPART_FORM_DATA).accept(MediaType.TEXT_PLAIN_TYPE), String.class));
HttpClientResponseException ex = Assertions.assertThrows(HttpClientResponseException.class, () -> flowable.blockFirst());
Map<String, Object> embedded = (Map<String, Object>) ex.getResponse().getBody(Map.class).get().get("_embedded");
Object message = ((Map<String, Object>) ((List) embedded.get("errors")).get(0)).get("message");
assertEquals("Required argument [CompletedFileUpload file] not specified", message);
}
use of io.micronaut.http.client.multipart.MultipartBody in project micronaut-core by micronaut-projects.
the class DefaultHttpClient method buildMultipartRequest.
private HttpPostRequestEncoder buildMultipartRequest(MutableHttpRequest clientHttpRequest, Object bodyValue) throws HttpPostRequestEncoder.ErrorDataEncoderException {
HttpDataFactory factory = new DefaultHttpDataFactory(DefaultHttpDataFactory.MINSIZE);
io.netty.handler.codec.http.HttpRequest request = NettyHttpRequestBuilder.toHttpRequest(clientHttpRequest);
HttpPostRequestEncoder postRequestEncoder = new HttpPostRequestEncoder(factory, request, true, CharsetUtil.UTF_8, HttpPostRequestEncoder.EncoderMode.HTML5);
if (bodyValue instanceof MultipartBody.Builder) {
bodyValue = ((MultipartBody.Builder) bodyValue).build();
}
if (bodyValue instanceof MultipartBody) {
final MultipartBody multipartBody = (MultipartBody) bodyValue;
postRequestEncoder.setBodyHttpDatas(multipartBody.getData(new MultipartDataFactory<InterfaceHttpData>() {
@NonNull
@Override
public InterfaceHttpData createFileUpload(@NonNull String name, @NonNull String filename, @NonNull MediaType contentType, @Nullable String encoding, @Nullable Charset charset, long length) {
return factory.createFileUpload(request, name, filename, contentType.toString(), encoding, charset, length);
}
@NonNull
@Override
public InterfaceHttpData createAttribute(@NonNull String name, @NonNull String value) {
return factory.createAttribute(request, name, value);
}
@Override
public void setContent(InterfaceHttpData fileUploadObject, Object content) throws IOException {
if (fileUploadObject instanceof FileUpload) {
FileUpload fu = (FileUpload) fileUploadObject;
if (content instanceof InputStream) {
fu.setContent((InputStream) content);
} else if (content instanceof File) {
fu.setContent((File) content);
} else if (content instanceof byte[]) {
final ByteBuf buffer = Unpooled.wrappedBuffer((byte[]) content);
fu.setContent(buffer);
}
}
}
}));
} else {
throw new MultipartException(String.format("The type %s is not a supported type for a multipart request body", bodyValue.getClass().getName()));
}
return postRequestEncoder;
}
use of io.micronaut.http.client.multipart.MultipartBody in project micronaut-core by micronaut-projects.
the class MultipartFileUploadSpec method testMultipartFileRequestByteArray.
@Test
public void testMultipartFileRequestByteArray() throws IOException {
// tag::file[]
String toWrite = "test file";
File file = File.createTempFile("data", ".txt");
FileWriter writer = new FileWriter(file);
writer.write(toWrite);
writer.close();
// end::file[]
// tag::multipartBody[]
MultipartBody requestBody = // <1>
MultipartBody.builder().addPart("data", file.getName(), MediaType.TEXT_PLAIN_TYPE, file).build();
// end::multipartBody[]
Flux<HttpResponse<String>> flowable = Flux.from(client.exchange(// tag::request[]
// <1>
HttpRequest.POST("/multipart/upload", requestBody).contentType(// <2>
MediaType.MULTIPART_FORM_DATA_TYPE).accept(MediaType.TEXT_PLAIN_TYPE), String.class));
HttpResponse<String> response = flowable.blockFirst();
String body = response.getBody().get();
assertEquals("Uploaded 9 bytes", body);
}
use of io.micronaut.http.client.multipart.MultipartBody in project micronaut-core by micronaut-projects.
the class MultipartFileUploadSpec method testMultipartFileRequestByteArrayWithContentType.
@Test
public void testMultipartFileRequestByteArrayWithContentType() {
// tag::multipartBodyBytes[]
MultipartBody requestBody = MultipartBody.builder().addPart("data", "sample.txt", MediaType.TEXT_PLAIN_TYPE, "test content".getBytes()).build();
// end::multipartBodyBytes[]
Flux<HttpResponse<String>> flowable = Flux.from(client.exchange(HttpRequest.POST("/multipart/upload", requestBody).contentType(MediaType.MULTIPART_FORM_DATA_TYPE).accept(MediaType.TEXT_PLAIN_TYPE), String.class));
HttpResponse<String> response = flowable.blockFirst();
String body = response.getBody().get();
assertEquals("Uploaded 12 bytes", body);
}
use of io.micronaut.http.client.multipart.MultipartBody in project check-ins by objectcomputing.
the class EmployeeHoursControllerTest method testCreateEmployeeHoursWithNoAdminRole.
@Test
void testCreateEmployeeHoursWithNoAdminRole() {
File file = new File("src/test/java/com/objectcomputing/checkins/services/employee_hours/test.csv");
MultipartBody multipartBody = MultipartBody.builder().addPart("file", "test.csv", new MediaType("text/csv"), file).build();
final HttpRequest<MultipartBody> request = HttpRequest.POST("/upload", multipartBody).basicAuth(MEMBER_ROLE, MEMBER_ROLE).contentType(MediaType.MULTIPART_FORM_DATA);
HttpClientResponseException responseException = assertThrows(HttpClientResponseException.class, () -> client.toBlocking().exchange(request, Map.class));
assertEquals(HttpStatus.BAD_REQUEST, responseException.getStatus());
}
Aggregations