Search in sources :

Example 21 with HttpResponse

use of io.micronaut.http.HttpResponse 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 22 with HttpResponse

use of io.micronaut.http.HttpResponse 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 23 with HttpResponse

use of io.micronaut.http.HttpResponse 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 24 with HttpResponse

use of io.micronaut.http.HttpResponse 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)

Example 25 with HttpResponse

use of io.micronaut.http.HttpResponse in project micronaut-core by micronaut-projects.

the class MovieTicketControllerSpec method testBindingBean.

@Test
public void testBindingBean() {
    UriTemplate template = new UriTemplate("/api/movie/ticket/terminator{?minPrice,maxPrice}");
    Map<String, Object> params = new HashMap<>();
    params.put("minPrice", 5.0);
    params.put("maxPrice", 20.0);
    HttpResponse response = client.toBlocking().exchange(template.expand(params));
    assertEquals(HttpStatus.OK, response.status());
}
Also used : HashMap(java.util.HashMap) HttpResponse(io.micronaut.http.HttpResponse) UriTemplate(io.micronaut.http.uri.UriTemplate) Test(org.junit.Test)

Aggregations

HttpResponse (io.micronaut.http.HttpResponse)36 Test (org.junit.Test)18 MultipartBody (io.micronaut.http.client.multipart.MultipartBody)11 Map (java.util.Map)10 HttpClientResponseException (io.micronaut.http.client.exceptions.HttpClientResponseException)9 Test (org.junit.jupiter.api.Test)9 Nullable (io.micronaut.core.annotation.Nullable)6 HttpClient (io.micronaut.http.client.HttpClient)6 MemberProfile (com.objectcomputing.checkins.services.memberprofile.MemberProfile)5 MediaType (io.micronaut.http.MediaType)5 MutableHttpResponse (io.micronaut.http.MutableHttpResponse)5 EmbeddedServer (io.micronaut.runtime.server.EmbeddedServer)5 List (java.util.List)5 IOException (java.io.IOException)4 Publisher (org.reactivestreams.Publisher)4 Flux (reactor.core.publisher.Flux)4 Internal (io.micronaut.core.annotation.Internal)3 HttpRequest (io.micronaut.http.HttpRequest)3 URI (java.net.URI)3 Publishers (io.micronaut.core.async.publisher.Publishers)2