/** * a {@link String} inbound {@link Mono} * * @return a {@link String} inbound {@link Mono} */ public final Mono<String> asString() { return asString(Charset.defaultCharset()); }
/** * a {@link String} inbound {@link Mono} * * @return a {@link String} inbound {@link Mono} */ public final Mono<String> asString() { return asString(Charset.defaultCharset()); }
private void doTestIssue186(HttpClient client) { Mono<String> content = client.post() .uri("/") .send(ByteBufFlux.fromString(Mono.just("bodysample"))) .responseContent() .aggregate() .asString(); StepVerifier.create(content) .expectComplete() .verify(Duration.ofSeconds(30)); }
private void doTestDropData( BiFunction<? super HttpServerRequest, ? super HttpServerResponse, ? extends Publisher<Void>> serverFn, BiFunction<? super HttpClientRequest, ? super NettyOutbound, ? extends Publisher<Void>> clientFn) { DisposableServer disposableServer = HttpServer.create() .port(0) .handle(serverFn) .wiretap(true) .bindNow(Duration.ofSeconds(30)); String response = HttpClient.create() .port(disposableServer.port()) .wiretap(true) .request(HttpMethod.GET) .uri("/") .send(clientFn) .responseContent() .aggregate() .asString() .switchIfEmpty(Mono.just("Empty")) .block(Duration.ofSeconds(30)); assertThat(response).isEqualTo("Empty"); disposableServer.disposeNow(); }
@Test public void disableChunkForced2() { Tuple2<HttpResponseStatus, String> r = HttpClient.newConnection() .tcpConfiguration(tcpClient -> tcpClient.host("google.com")) .wiretap(true) .keepAlive(false) .chunkedTransfer(false) .get() .uri("/unsupportedURI") .responseSingle((res, conn) -> Mono.just(res.status()) .zipWith(conn.asString())) .block(Duration.ofSeconds(30)); assertThat(r).isNotNull(); Assert.assertEquals(r.getT1(), HttpResponseStatus.NOT_FOUND); }
@Test public void testIssue196() throws Exception { ExecutorService threadPool = Executors.newCachedThreadPool(); int testServerPort = SocketUtils.findAvailableTcpPort(); TestServer testServer = new TestServer(testServerPort); Future<?> f = threadPool.submit(testServer); if(!testServer.await(10, TimeUnit.SECONDS)){ throw new IOException("Fail to start test server"); } HttpClient client = HttpClient.newConnection() .port(testServerPort) .wiretap(true); Flux.range(0, 2) .concatMap(i -> client.get() .uri("/205") .responseContent() .aggregate() .asString() .log()) .blockLast(Duration.ofSeconds(10)); testServer.close(); assertThat(f.get()).isNull(); }
@Test public void disableChunkForced() { Tuple2<HttpResponseStatus, String> r = HttpClient.newConnection() .tcpConfiguration(tcpClient -> tcpClient.host("google.com")) .wiretap(true) .chunkedTransfer(false) .request(HttpMethod.GET) .uri("/unsupportedURI") .send(ByteBufFlux.fromString(Flux.just("hello"))) .responseSingle((res, conn) -> Mono.just(res.status()) .zipWith(conn.asString())) .block(Duration.ofSeconds(30)); assertThat(r).isNotNull(); Assert.assertEquals(r.getT1(), HttpResponseStatus.NOT_FOUND); }
@Test public void testIssue525() { DisposableServer disposableServer = HttpServer.create() .port(0) .tcpConfiguration(tcpServer -> tcpServer.doOnConnection(c -> c.addHandlerFirst("decompressor", new HttpContentDecompressor()))) .handle((req, res) -> res.send(req.receive() .retain())) .wiretap(true) .bindNow(Duration.ofSeconds(30)); byte[] bytes = "test".getBytes(Charset.defaultCharset()); String response = HttpClient.create() .port(disposableServer.port()) .wiretap(true) .headers(h -> h.add("Content-Encoding", "gzip")) .post() .uri("/") .send(Mono.just(Unpooled.wrappedBuffer(compress(bytes)))) .responseContent() .aggregate() .asString() .block(Duration.ofSeconds(30)); assertThat(response).isEqualTo("test"); disposableServer.disposeNow(); }
@Test public void testIssue303() { DisposableServer server = HttpServer.create() .port(0) .handle((req, resp) -> resp.sendString(Mono.just("OK"))) .wiretap(true) .bindNow(); Mono<String> content = createHttpClientForContextWithPort(server) .request(HttpMethod.GET) .uri("/") .send(ByteBufFlux.fromInbound(Mono.defer(() -> Mono.just("Hello".getBytes(Charset.defaultCharset()))))) .responseContent() .aggregate() .asString(); StepVerifier.create(content) .expectNextMatches("OK"::equals) .expectComplete() .verify(Duration.ofSeconds(30)); server.disposeNow(); }
@Test public void sslExchangeRelativeGet() throws CertificateException, SSLException { SelfSignedCertificate ssc = new SelfSignedCertificate(); SslContext sslServer = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()) .build(); SslContext sslClient = SslContextBuilder.forClient() .trustManager(InsecureTrustManagerFactory.INSTANCE) .build(); DisposableServer context = HttpServer.create() .secure(ssl -> ssl.sslContext(sslServer)) .handle((req, resp) -> resp.sendString(Flux.just("hello ", req.uri()))) .wiretap(true) .bindNow(); String responseString = createHttpClientForContextWithAddress(context) .secure(ssl -> ssl.sslContext(sslClient)) .get() .uri("/foo") .responseSingle((res, buf) -> buf.asString(CharsetUtil.UTF_8)) .block(Duration.ofMillis(200)); context.disposeNow(); assertThat(responseString).isEqualTo("hello /foo"); }
@Test public void sslExchangeAbsoluteGet() throws CertificateException, SSLException { SelfSignedCertificate ssc = new SelfSignedCertificate(); SslContext sslServer = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); SslContext sslClient = SslContextBuilder.forClient() .trustManager(InsecureTrustManagerFactory.INSTANCE).build(); DisposableServer context = HttpServer.create() .secure(ssl -> ssl.sslContext(sslServer)) .handle((req, resp) -> resp.sendString(Flux.just("hello ", req.uri()))) .wiretap(true) .bindNow(); String responseString = createHttpClientForContextWithAddress(context) .secure(ssl -> ssl.sslContext(sslClient)) .get() .uri("/foo") .responseSingle((res, buf) -> buf.asString(CharsetUtil.UTF_8)) .block(); context.disposeNow(); assertThat(responseString).isEqualTo("hello /foo"); }
@Test public void proxy_2() { StepVerifier.create( HttpClient.create() .tcpConfiguration(tcpClient -> tcpClient.proxy(ops -> ops.type(ProxyProvider.Proxy.HTTP) .host("localhost") .port(hoverflyRule.getProxyPort()))) .wiretap(true) .get() .uri("http://127.0.0.1:7000/") .responseSingle((response, body) -> Mono.zip(body.asString(), Mono.just(response.responseHeaders())))) .expectNextMatches(t -> t.getT2().contains("Hoverfly") && "test".equals(t.getT1())) .expectComplete() .verify(Duration.ofSeconds(30)); }
@Test public void testIssue282() { DisposableServer server = HttpServer.create() .compress(2048) .port(0) .handle((req, res) -> res.sendString(Mono.just("testtesttesttesttest"))) .bindNow(); Mono<String> response = HttpClient.create() .port(server.address().getPort()) .get() .uri("/") .responseContent() .aggregate() .asString(); StepVerifier.create(response) .expectNextMatches(s -> "testtesttesttesttest".equals(s)) .expectComplete() .verify(); server.disposeNow(); }
@Test public void proxy_1() { StepVerifier.create( HttpClient.create() .tcpConfiguration(tcpClient -> tcpClient.proxy(ops -> ops.type(ProxyProvider.Proxy.HTTP) .host("localhost") .port(hoverflyRule.getProxyPort()))) .addressSupplier(server::address) .wiretap(true) .get() .uri("/") .responseSingle((response, body) -> Mono.zip(body.asString(), Mono.just(response.responseHeaders())))) .expectNextMatches(t -> t.getT2().contains("Hoverfly") && "test".equals(t.getT1())) .expectComplete() .verify(Duration.ofSeconds(30)); }
@Test public void withConnector() { DisposableServer server = HttpServer.create() .port(0) .handle((req, resp) -> resp.sendString(Mono.just(req.requestHeaders() .get("test")))) .bindNow(); Mono<String> content = createHttpClientForContextWithPort(server) .mapConnect((c, b) -> c.subscriberContext(Context.of("test", "success"))) .post() .uri("/") .send((req, out) -> { req.requestHeaders() .set("test", req.currentContext() .getOrDefault("test", "fail")); return Mono.empty(); }) .responseContent() .aggregate() .asString(); StepVerifier.create(content) .expectNext("success") .verifyComplete(); server.disposeNow(); }
@Test public void nonProxyHosts_2() { StepVerifier.create( HttpClient.create() .tcpConfiguration(tcpClient -> tcpClient.proxy(ops -> ops.type(ProxyProvider.Proxy.HTTP) .host("localhost") .port(hoverflyRule.getProxyPort()) .nonProxyHosts("localhost"))) .wiretap(true) .get() .uri("http://localhost:7000/") .responseSingle((response, body) -> Mono.zip(body.asString(), Mono.just(response.responseHeaders())))) .expectNextMatches(t -> !t.getT2().contains("Hoverfly") && "test".equals(t.getT1())) .expectComplete() .verify(Duration.ofSeconds(30)); } }
@Test public void nonProxyHosts_1() { StepVerifier.create( HttpClient.create() .tcpConfiguration(tcpClient -> tcpClient.proxy(ops -> ops.type(ProxyProvider.Proxy.HTTP) .host("localhost") .port(hoverflyRule.getProxyPort()) .nonProxyHosts("127.0.0.1"))) .addressSupplier(server::address) .wiretap(true) .get() .uri("/") .responseSingle((response, body) -> Mono.zip(body.asString(), Mono.just(response.responseHeaders())))) .expectNextMatches(t -> !t.getT2().contains("Hoverfly") && "test".equals(t.getT1())) .expectComplete() .verify(Duration.ofSeconds(30)); }
@Test public void testClientReuseIssue405(){ DisposableServer c = HttpServer.create() .port(0) .handle((in,out)->out.sendString(Flux.just("hello"))) .wiretap(true) .bindNow(); ConnectionProvider pool = ConnectionProvider.fixed("test", 1); HttpClient httpClient = createHttpClientForContextWithPort(c, pool); Mono<String> mono1 = httpClient.get() .responseSingle((r, buf) -> buf.asString()) .log("mono1"); Mono<String> mono2 = httpClient.get() .responseSingle((r, buf) -> buf.asString()) .log("mono1"); StepVerifier.create(Flux.zip(mono1,mono2)) .expectNext(Tuples.of("hello","hello")) .expectComplete() .verify(Duration.ofSeconds(20)); c.disposeNow(); pool.dispose(); }
@Test public void serverWebSocketFailed() { httpServer = HttpServer.create() .port(0) .handle((in, out) -> { if (!in.requestHeaders().contains("Authorization")) { return out.status(401); } else { return out.sendWebsocket((i, o) -> o.sendString(Mono.just("test"))); } }) .wiretap(true) .bindNow(); Mono<String> res = HttpClient.create() .port(httpServer.address() .getPort()) .websocket() .uri("/test") .handle((in, out) -> in.receive().aggregate().asString()) .next(); StepVerifier.create(res) .expectError(WebSocketHandshakeException.class) .verify(Duration.ofSeconds(30)); }
@Test // @Ignore public void testHttpToHttp2Ssl() throws Exception { SelfSignedCertificate cert = new SelfSignedCertificate(); SslContextBuilder serverOptions = SslContextBuilder.forServer(cert.certificate(), cert.privateKey()); DisposableServer server = HttpServer.create() .secure(sslContextSpec -> sslContextSpec.sslContext(serverOptions) .defaultConfiguration(SslProvider.DefaultConfigurationType.H2)) .handle((req, res) -> res.sendString(Mono.just("Hello"))) .wiretap(true) .bindNow(); String response = HttpClient.create() .port(server.port()) .secure(ssl -> ssl.sslContext( SslContextBuilder.forClient() .trustManager(InsecureTrustManagerFactory.INSTANCE))) .wiretap(true) .get() .uri("/") .responseContent() .aggregate() .asString() .block(Duration.ofSeconds(30)); server.disposeNow(); }