Search in sources :

Example 6 with HttpResponse

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();
}
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 7 with HttpResponse

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();
}
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 8 with HttpResponse

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();
}
Also used : CompletedFileUpload(io.micronaut.http.multipart.CompletedFileUpload) ArrayList(java.util.ArrayList) HttpResponse(io.micronaut.http.HttpResponse) MutableHttpResponse(io.micronaut.http.MutableHttpResponse) Subscription(org.reactivestreams.Subscription) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Post(io.micronaut.http.annotation.Post)

Example 9 with HttpResponse

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);
}
Also used : FileWriter(java.io.FileWriter) HttpResponse(io.micronaut.http.HttpResponse) File(java.io.File) Test(org.junit.Test)

Example 10 with HttpResponse

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");
}
Also used : HttpRequest(io.micronaut.http.HttpRequest) HttpClientResponseException(io.micronaut.http.client.exceptions.HttpClientResponseException) HttpResponse(io.micronaut.http.HttpResponse) Map(java.util.Map) 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