Search in sources :

Example 26 with HttpResponse

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

the class PersonControllerSpec method testGlobalErrorHandler.

@Test
public void testGlobalErrorHandler() {
    HttpClientResponseException e = Assertions.assertThrows(HttpClientResponseException.class, () -> Flux.from(client.exchange("/people/error", Map.class)).blockFirst());
    HttpResponse<Map> response = (HttpResponse<Map>) e.getResponse();
    assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatus());
    assertEquals("Bad Things Happened: Something went wrong", response.getBody().get().get("message"));
}
Also used : HttpClientResponseException(io.micronaut.http.client.exceptions.HttpClientResponseException) HttpResponse(io.micronaut.http.HttpResponse) Map(java.util.Map) Test(org.junit.Test)

Example 27 with HttpResponse

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

the class PersonControllerSpec method testSaveInvalidJson.

@Test
public void testSaveInvalidJson() {
    HttpClientResponseException e = Assertions.assertThrows(HttpClientResponseException.class, () -> client.toBlocking().exchange(HttpRequest.POST("/people", "{\""), Argument.of(Person.class), Argument.of(Map.class)));
    HttpResponse<Map> response = (HttpResponse<Map>) e.getResponse();
    assertTrue(response.getBody(Map.class).get().get("message").toString().startsWith("Invalid JSON: Unexpected end-of-input"));
    assertEquals(HttpStatus.BAD_REQUEST, response.getStatus());
}
Also used : HttpClientResponseException(io.micronaut.http.client.exceptions.HttpClientResponseException) HttpResponse(io.micronaut.http.HttpResponse) Map(java.util.Map) Test(org.junit.Test)

Example 28 with HttpResponse

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

the class BookControllerTest method testPostWithURITemplate.

@Test
public void testPostWithURITemplate() {
    EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class);
    HttpClient client = embeddedServer.getApplicationContext().createBean(HttpClient.class, embeddedServer.getURL());
    // tag::posturitemplate[]
    Flux<HttpResponse<Book>> call = Flux.from(client.exchange(POST("/amazon/book/{title}", new Book("The Stand")), Book.class));
    // end::posturitemplate[]
    HttpResponse<Book> response = call.blockFirst();
    // <2>
    Optional<Book> message = response.getBody(Book.class);
    // check the status
    Assertions.assertEquals(HttpStatus.CREATED, // <3>
    response.getStatus());
    // check the body
    Assertions.assertTrue(message.isPresent());
    Assertions.assertEquals("The Stand", message.get().getTitle());
    embeddedServer.stop();
    client.stop();
}
Also used : HttpClient(io.micronaut.http.client.HttpClient) EmbeddedServer(io.micronaut.runtime.server.EmbeddedServer) HttpResponse(io.micronaut.http.HttpResponse) Test(org.junit.jupiter.api.Test)

Example 29 with HttpResponse

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

the class BookControllerTest method testPostFormData.

@Test
public void testPostFormData() {
    EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class);
    HttpClient client = embeddedServer.getApplicationContext().createBean(HttpClient.class, embeddedServer.getURL());
    // tag::postform[]
    Flux<HttpResponse<Book>> call = Flux.from(client.exchange(POST("/amazon/book/{title}", new Book("The Stand")).contentType(MediaType.APPLICATION_FORM_URLENCODED), Book.class));
    // end::postform[]
    HttpResponse<Book> response = call.blockFirst();
    // <2>
    Optional<Book> message = response.getBody(Book.class);
    // check the status
    Assertions.assertEquals(HttpStatus.CREATED, // <3>
    response.getStatus());
    // check the body
    Assertions.assertTrue(message.isPresent());
    Assertions.assertEquals("The Stand", message.get().getTitle());
    embeddedServer.stop();
    client.stop();
}
Also used : HttpClient(io.micronaut.http.client.HttpClient) EmbeddedServer(io.micronaut.runtime.server.EmbeddedServer) HttpResponse(io.micronaut.http.HttpResponse) Test(org.junit.jupiter.api.Test)

Example 30 with HttpResponse

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

the class HelloControllerTest method testPostRequestWithPOJO.

@Test
public void testPostRequestWithPOJO() {
    EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class);
    HttpClient client = embeddedServer.getApplicationContext().createBean(HttpClient.class, embeddedServer.getURL());
    // tag::postpojo[]
    Flux<HttpResponse<Message>> call = Flux.from(client.exchange(// <1>
    POST("/greet", new Message("Hello John")), // <2>
    Message.class));
    // end::postpojo[]
    HttpResponse<Message> response = call.blockFirst();
    // <2>
    Optional<Message> message = response.getBody(Message.class);
    // check the status
    Assertions.assertEquals(HttpStatus.CREATED, // <3>
    response.getStatus());
    // check the body
    Assertions.assertTrue(message.isPresent());
    Assertions.assertEquals("Hello John", message.get().getText());
    embeddedServer.stop();
    client.stop();
}
Also used : HttpClient(io.micronaut.http.client.HttpClient) EmbeddedServer(io.micronaut.runtime.server.EmbeddedServer) HttpResponse(io.micronaut.http.HttpResponse) Test(org.junit.jupiter.api.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