use of io.micronaut.http.HttpResponse in project micronaut-core by micronaut-projects.
the class HelloControllerTest method testRetrieveWithPOJOResponse.
@Test
public void testRetrieveWithPOJOResponse() {
EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class);
HttpClient client = embeddedServer.getApplicationContext().createBean(HttpClient.class, embeddedServer.getURL());
// tag::pojoresponse[]
Flux<HttpResponse<Message>> call = Flux.from(client.exchange(// <1>
GET("/greet/John"), // <1>
Message.class));
HttpResponse<Message> response = call.blockFirst();
// <2>
Optional<Message> message = response.getBody(Message.class);
// check the status
Assertions.assertEquals(HttpStatus.OK, // <3>
response.getStatus());
// check the body
Assertions.assertTrue(message.isPresent());
Assertions.assertEquals("Hello John", message.get().getText());
// end::pojoresponse[]
embeddedServer.stop();
client.stop();
}
use of io.micronaut.http.HttpResponse in project micronaut-core by micronaut-projects.
the class HelloControllerTest method testPostRequestWithString.
@Test
public void testPostRequestWithString() {
EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class);
HttpClient client = embeddedServer.getApplicationContext().createBean(HttpClient.class, embeddedServer.getURL());
// tag::poststring[]
Flux<HttpResponse<String>> call = Flux.from(client.exchange(// <1>
POST("/hello", "Hello John").contentType(MediaType.TEXT_PLAIN_TYPE).accept(// <2>
MediaType.TEXT_PLAIN_TYPE), // <3>
String.class));
// end::poststring[]
HttpResponse<String> response = call.blockFirst();
// <2>
Optional<String> message = response.getBody(String.class);
// check the status
Assertions.assertEquals(HttpStatus.CREATED, // <3>
response.getStatus());
// check the body
Assertions.assertTrue(message.isPresent());
Assertions.assertEquals("Hello John", message.get());
embeddedServer.stop();
client.stop();
}
use of io.micronaut.http.HttpResponse in project micronaut-core by micronaut-projects.
the class UploadController method receiveMultipleCompleted.
@Post(value = "/receive-multiple-completed", consumes = MediaType.MULTIPART_FORM_DATA)
public Publisher<HttpResponse> receiveMultipleCompleted(Publisher<CompletedFileUpload> data, String title) {
List<Map> results = new ArrayList<>();
ReplayProcessor<HttpResponse> subject = ReplayProcessor.create();
Flux.from(data).subscribeOn(Schedulers.boundedElastic()).subscribe(new Subscriber<CompletedFileUpload>() {
Subscription subscription;
@Override
public void onSubscribe(Subscription s) {
s.request(1);
this.subscription = s;
}
@Override
public void onNext(CompletedFileUpload upload) {
Map<String, Object> result = new LinkedHashMap<>();
result.put("name", upload.getFilename());
result.put("size", upload.getSize());
results.add(result);
subscription.request(1);
}
@Override
public void onError(Throwable t) {
subject.onError(t);
}
@Override
public void onComplete() {
Map<String, Object> body = new LinkedHashMap<>();
body.put("files", results);
body.put("title", title);
subject.onNext(HttpResponse.ok(body));
subject.onComplete();
}
});
return subject.asFlux();
}
use of io.micronaut.http.HttpResponse in project micronaut-core by micronaut-projects.
the class MultipartFileUploadSpec method testMultipartFileRequestByteArrayWithoutContentType.
@Test
public void testMultipartFileRequestByteArrayWithoutContentType() throws IOException {
String toWrite = "test file";
File file = File.createTempFile("data", ".txt");
FileWriter writer = new FileWriter(file);
writer.write(toWrite);
writer.close();
file.createNewFile();
Flux<HttpResponse<String>> flowable = Flux.from(client.exchange(HttpRequest.POST("/multipart/upload", MultipartBody.builder().addPart("data", file.getName(), file)).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 9 bytes", body);
}
use of io.micronaut.http.HttpResponse in project micronaut-core by micronaut-projects.
the class ExceptionHandlerSpec method testExceptionIsHandled.
@Test
public void testExceptionIsHandled() {
HttpRequest request = HttpRequest.GET("/books/stock/1234");
Argument<Map<String, Object>> errorType = Argument.mapOf(String.class, Object.class);
HttpClientResponseException ex = Assertions.assertThrows(HttpClientResponseException.class, () -> {
client.toBlocking().exchange(request, Argument.LONG, errorType);
});
HttpResponse response = ex.getResponse();
Map<String, Object> body = (Map<String, Object>) response.getBody(errorType).get();
Map<String, Object> embedded = (Map<String, Object>) body.get("_embedded");
Object message = ((Map<String, Object>) ((List) embedded.get("errors")).get(0)).get("message");
assertEquals(response.status(), HttpStatus.BAD_REQUEST);
assertEquals(message, "No stock available");
}
Aggregations