2 * ============LICENSE_START====================================
3 * DCAEGEN2-SERVICES-SDK
4 * =========================================================
5 * Copyright (C) 2019 Nokia. All rights reserved.
6 * =========================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=====================================
21 package org.onap.dcaegen2.services.sdk.rest.services.adapters.http;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.onap.dcaegen2.services.sdk.rest.services.adapters.http.test.DummyHttpServer.sendString;
26 import io.netty.handler.codec.http.HttpResponseStatus;
27 import java.net.MalformedURLException;
29 import java.time.Duration;
30 import org.junit.jupiter.api.AfterAll;
31 import org.junit.jupiter.api.BeforeAll;
32 import org.junit.jupiter.api.Test;
33 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.exceptions.HttpException;
34 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.test.DummyHttpServer;
35 import reactor.core.publisher.Mono;
36 import reactor.test.StepVerifier;
38 class RxHttpClientIT {
40 private static final Duration TIMEOUT = Duration.ofHours(5);
41 private final RxHttpClient cut = RxHttpClientFactory.create();
42 private static DummyHttpServer httpServer;
45 static void setUpClass() {
46 httpServer = DummyHttpServer.start(routes ->
47 routes.get("/sample-get", (req, resp) -> sendString(resp, Mono.just("OK")))
48 .get("/sample-get-500", (req, resp) -> resp.status(HttpResponseStatus.INTERNAL_SERVER_ERROR).send())
49 .post("/headers-post", (req, resp) -> resp
50 .sendString(Mono.just(req.requestHeaders().toString())))
51 .post("/echo-post", (req, resp) -> resp.send(req.receive().retain()))
56 static void tearDownClass() {
60 private ImmutableHttpRequest.Builder requestFor(String path) throws MalformedURLException {
61 return ImmutableHttpRequest.builder()
62 .url(new URL("http", httpServer.host(), httpServer.port(), path).toString());
66 void simpleGet() throws Exception {
68 final HttpRequest httpRequest = requestFor("/sample-get").method(HttpMethod.GET).build();
71 final Mono<String> bodyAsString = cut.call(httpRequest)
72 .doOnNext(HttpResponse::throwIfUnsuccessful)
73 .map(HttpResponse::bodyAsString);
76 StepVerifier.create(bodyAsString).expectNext("OK").expectComplete().verify(TIMEOUT);
80 void getWithError() throws Exception {
82 final HttpRequest httpRequest = requestFor("/sample-get-500").method(HttpMethod.GET).build();
85 final Mono<String> bodyAsString = cut.call(httpRequest)
86 .doOnNext(HttpResponse::throwIfUnsuccessful)
87 .map(HttpResponse::bodyAsString);
90 StepVerifier.create(bodyAsString).expectError(HttpException.class).verify(TIMEOUT);
94 void simplePost() throws Exception {
96 final String requestBody = "hello world";
97 final HttpRequest httpRequest = requestFor("/echo-post")
98 .method(HttpMethod.POST)
99 .body(RequestBody.fromString(requestBody))
103 final Mono<String> bodyAsString = cut.call(httpRequest)
104 .doOnNext(HttpResponse::throwIfUnsuccessful)
105 .map(HttpResponse::bodyAsString);
108 StepVerifier.create(bodyAsString)
109 .expectNext(requestBody)
115 void testChunkedEncoding() throws Exception {
117 final String requestBody = "hello world";
118 final HttpRequest httpRequest = requestFor("/headers-post")
119 .method(HttpMethod.POST)
120 .body(RequestBody.chunkedFromString(Mono.just(requestBody)))
124 final Mono<String> bodyAsString = cut.call(httpRequest)
125 .doOnNext(HttpResponse::throwIfUnsuccessful)
126 .map(HttpResponse::bodyAsString);
129 StepVerifier.create(bodyAsString.map(String::toLowerCase))
130 .consumeNextWith(responseBody -> {
131 assertThat(responseBody).contains("transfer-encoding: chunked");
132 assertThat(responseBody).doesNotContain("content-length");
139 void testUnchunkedEncoding() throws Exception {
141 final String requestBody = "hello world";
142 final HttpRequest httpRequest = requestFor("/headers-post")
143 .method(HttpMethod.POST)
144 .body(RequestBody.fromString(requestBody))
148 final Mono<String> bodyAsString = cut.call(httpRequest)
149 .doOnNext(HttpResponse::throwIfUnsuccessful)
150 .map(HttpResponse::bodyAsString);
153 StepVerifier.create(bodyAsString.map(String::toLowerCase))
154 .consumeNextWith(responseBody -> {
155 assertThat(responseBody).doesNotContain("transfer-encoding");
156 assertThat(responseBody).contains("content-length");