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"));
}
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());
}
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();
}
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();
}
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();
}
Aggregations