Search in sources :

Example 6 with MultipartBody

use of io.micronaut.http.client.multipart.MultipartBody in project micronaut-core by micronaut-projects.

the class UploadControllerSpec method testFileUploadOutputStream.

@Test
public void testFileUploadOutputStream() {
    MultipartBody body = MultipartBody.builder().addPart("file", "file.json", MediaType.APPLICATION_JSON_TYPE, "{\"title\":\"Foo\"}".getBytes()).build();
    Flux<HttpResponse<String>> flowable = Flux.from(client.exchange(HttpRequest.POST("/upload/outputStream", body).contentType(MediaType.MULTIPART_FORM_DATA).accept(MediaType.TEXT_PLAIN_TYPE), String.class));
    HttpResponse<String> response = flowable.blockFirst();
    assertEquals(HttpStatus.OK.getCode(), response.code());
    assertEquals("Uploaded", response.getBody().get());
}
Also used : MultipartBody(io.micronaut.http.client.multipart.MultipartBody) HttpResponse(io.micronaut.http.HttpResponse) Test(org.junit.Test)

Example 7 with MultipartBody

use of io.micronaut.http.client.multipart.MultipartBody in project micronaut-core by micronaut-projects.

the class UploadControllerSpec method testCompletedFileUploadWithNoFileNameAndNoBytes.

@Test
public void testCompletedFileUploadWithNoFileNameAndNoBytes() {
    MultipartBody body = MultipartBody.builder().addPart("file", "", 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);
}
Also used : HttpClientResponseException(io.micronaut.http.client.exceptions.HttpClientResponseException) MultipartBody(io.micronaut.http.client.multipart.MultipartBody) HttpResponse(io.micronaut.http.HttpResponse) Map(java.util.Map) Test(org.junit.Test)

Example 8 with MultipartBody

use of io.micronaut.http.client.multipart.MultipartBody in project micronaut-core by micronaut-projects.

the class UploadControllerSpec method testCompletedFileUploadWithFilenameButNoBytes.

@Test
public void testCompletedFileUploadWithFilenameButNoBytes() {
    MultipartBody body = MultipartBody.builder().addPart("file", "file.json", 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));
    HttpResponse<String> response = flowable.blockFirst();
    assertEquals(HttpStatus.OK.getCode(), response.code());
    assertEquals("Uploaded", response.getBody().get());
}
Also used : MultipartBody(io.micronaut.http.client.multipart.MultipartBody) HttpResponse(io.micronaut.http.HttpResponse) Test(org.junit.Test)

Example 9 with MultipartBody

use of io.micronaut.http.client.multipart.MultipartBody in project micronaut-core by micronaut-projects.

the class UploadControllerSpec method testFileUpload.

@Test
public void testFileUpload() {
    MultipartBody body = MultipartBody.builder().addPart("file", "file.json", MediaType.APPLICATION_JSON_TYPE, "{\"title\":\"Foo\"}".getBytes()).build();
    Flux<HttpResponse<String>> flowable = Flux.from(client.exchange(HttpRequest.POST("/upload", body).contentType(MediaType.MULTIPART_FORM_DATA).accept(MediaType.TEXT_PLAIN_TYPE), String.class));
    HttpResponse<String> response = flowable.blockFirst();
    assertEquals(HttpStatus.OK.getCode(), response.code());
    assertEquals("Uploaded", response.getBody().get());
}
Also used : MultipartBody(io.micronaut.http.client.multipart.MultipartBody) HttpResponse(io.micronaut.http.HttpResponse) Test(org.junit.Test)

Example 10 with MultipartBody

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);
}
Also used : HttpClientResponseException(io.micronaut.http.client.exceptions.HttpClientResponseException) MultipartBody(io.micronaut.http.client.multipart.MultipartBody) HttpResponse(io.micronaut.http.HttpResponse) Map(java.util.Map) Test(org.junit.Test)

Aggregations

MultipartBody (io.micronaut.http.client.multipart.MultipartBody)15 HttpResponse (io.micronaut.http.HttpResponse)10 Test (org.junit.Test)10 MediaType (io.micronaut.http.MediaType)5 HttpClientResponseException (io.micronaut.http.client.exceptions.HttpClientResponseException)5 File (java.io.File)5 Map (java.util.Map)3 Test (org.junit.jupiter.api.Test)3 MemberProfile (com.objectcomputing.checkins.services.memberprofile.MemberProfile)2 EmployeeHours (com.objectcomputing.checkins.services.employee_hours.EmployeeHours)1 EmployeeHoursServices (com.objectcomputing.checkins.services.employee_hours.EmployeeHoursServices)1 NonNull (io.micronaut.core.annotation.NonNull)1 Nullable (io.micronaut.core.annotation.Nullable)1 MultipartDataFactory (io.micronaut.http.client.multipart.MultipartDataFactory)1 NettyClientSslBuilder (io.micronaut.http.client.netty.ssl.NettyClientSslBuilder)1 MultipartException (io.micronaut.http.multipart.MultipartException)1 NettyHttpRequestBuilder (io.micronaut.http.netty.NettyHttpRequestBuilder)1 NettyHttpResponseBuilder (io.micronaut.http.netty.NettyHttpResponseBuilder)1 UriBuilder (io.micronaut.http.uri.UriBuilder)1 ByteBuf (io.netty.buffer.ByteBuf)1