private void enrichResponse(HttpServerResponse httpResponse, ServiceMessage response) { Optional.ofNullable(response.header(CLIENT_SEND_TIME)) .ifPresent(value -> httpResponse.header(CLIENT_SEND_TIME, value)); Optional.ofNullable(response.header(CLIENT_RECV_TIME)) .ifPresent(value -> httpResponse.header(CLIENT_RECV_TIME, value)); Optional.ofNullable(response.header(SERVICE_RECV_TIME)) .ifPresent(value -> httpResponse.header(SERVICE_RECV_TIME, value)); Optional.ofNullable(response.header(SERVICE_SEND_TIME)) .ifPresent(value -> httpResponse.header(SERVICE_SEND_TIME, value)); } }
private void enrichResponse(HttpServerResponse httpResponse, ServiceMessage response) { Optional.ofNullable(response.header(CLIENT_SEND_TIME)) .ifPresent(value -> httpResponse.header(CLIENT_SEND_TIME, value)); Optional.ofNullable(response.header(CLIENT_RECV_TIME)) .ifPresent(value -> httpResponse.header(CLIENT_RECV_TIME, value)); Optional.ofNullable(response.header(SERVICE_RECV_TIME)) .ifPresent(value -> httpResponse.header(SERVICE_RECV_TIME, value)); Optional.ofNullable(response.header(SERVICE_SEND_TIME)) .ifPresent(value -> httpResponse.header(SERVICE_SEND_TIME, value)); } }
@Override public Mono<Void> sendRedirect(String location) { Objects.requireNonNull(location, "location"); return this.status(HttpResponseStatus.FOUND) .header(HttpHeaderNames.LOCATION, location) .send(); }
@Override public Mono<Void> sendRedirect(String location) { Objects.requireNonNull(location, "location"); return this.status(HttpResponseStatus.FOUND) .header(HttpHeaderNames.LOCATION, location) .send(); }
@Test public void testDropMessage() { ByteBuf data = ByteBufAllocator.DEFAULT.buffer(); data.writeCharSequence("test", Charset.defaultCharset()); doTestDropData( (req, res) -> res.header("Content-Length", "0") .sendObject(data), (req, out) -> out); assertThat(ReferenceCountUtil.refCnt(data)).isEqualTo(0); }
@Test public void testDropPublisher() { ByteBuf data = ByteBufAllocator.DEFAULT.buffer(); data.writeCharSequence("test", Charset.defaultCharset()); doTestDropData( (req, res) -> res.header("Content-Length", "0") .send(Mono.fromRunnable(() -> Flux.just(data, data.retain(), data.retain()))) .then() .doOnCancel(() -> ReferenceCountUtil.release(data)), (req, out) -> out); assertThat(ReferenceCountUtil.refCnt(data)).isEqualTo(0); }
@Test public void testDropMessageConnectionClose() { ByteBuf data = ByteBufAllocator.DEFAULT.buffer(); data.writeCharSequence("test", Charset.defaultCharset()); doTestDropData( (req, res) -> res.header("Content-Length", "0") .sendObject(data), (req, out) -> { req.addHeader("Connection", "close"); return out; }); assertThat(ReferenceCountUtil.refCnt(data)).isEqualTo(0); }
@Test public void testDropPublisherConnectionClose() { ByteBuf data = ByteBufAllocator.DEFAULT.buffer(); data.writeCharSequence("test", Charset.defaultCharset()); doTestDropData( (req, res) -> res.header("Content-Length", "0") .send(Mono.fromRunnable(() -> Flux.just(data, data.retain(), data.retain()))) .then() .doOnCancel(() -> ReferenceCountUtil.release(data)), (req, out) -> { req.addHeader("Connection", "close"); return out; }); assertThat(ReferenceCountUtil.refCnt(data)).isEqualTo(0); }
@Test public void sendFileAsync4096Negative() throws IOException, URISyntaxException { doTestSendFileAsync((req, resp) -> req.receive() .take(10) .doOnNext(b -> resp.status(500) .header(HttpHeaderNames.CONNECTION, "close")) .then(), 4096, "error".getBytes(Charset.defaultCharset())); }
@Test @Ignore public void startContentServer() { Random random = new Random(0); byte[] content = new byte[1024 * 10]; random.nextBytes(content); HttpServer server = HttpServer.create() .port(CONTENT_SERVER_PORT) .tcpConfiguration(tcpServer -> tcpServer.host("0.0.0.0") .option(ChannelOption.SO_LINGER, -1)); server.route(routes -> routes.get("/**", (req, res) -> res.header("Content-length", String.valueOf(content.length)) .header("Content-type", "application/octet-stream") .header("Connection", "Close") .sendByteArray(Flux.just(content)))) .wiretap(true) .bindNow(); }
@Test public void testPreferContentLengthWhenPost() { DisposableServer server = HttpServer.create() .port(0) .wiretap(true) .handle((req, res) -> res.header(HttpHeaderNames.CONTENT_LENGTH, req.requestHeaders() .get(HttpHeaderNames.CONTENT_LENGTH)) .send(req.receive() .aggregate() .retain())) .bindNow(); StepVerifier.create( createHttpClientForContextWithAddress(server) .chunkedTransfer(false) .headers(h -> h.add(HttpHeaderNames.CONTENT_LENGTH, 5)) .post() .uri("/") .send(Mono.just(Unpooled.wrappedBuffer("hello".getBytes(Charset.defaultCharset())))) .responseContent() .aggregate() .asString()) .expectNextMatches("hello"::equals) .expectComplete() .verify(Duration.ofSeconds(30)); server.disposeNow(); }
.route(r -> r.get("/test/{param}", (req, res) -> { System.out.println(req.requestHeaders().get("test")); return res.header("content-type", "text/plain") .sendWebsocket((in, out) -> out.options(NettyPipeline.SendOptions::flushOnEach)
.port(0) .handle((req, resp) -> resp.header(HttpHeaderNames.CONTENT_LENGTH, "1") .sendString(Mono.just(i.incrementAndGet()) .flatMap(d ->
@Test public void serverCompressionEnabledSmallResponse() { HttpServer server = HttpServer.create() .port(0) .compress(25); DisposableServer connection = server.handle((in, out) -> out.header("content-length", "5") .sendString(Mono.just("reply"))) .bindNow(); //don't activate compression on the client options to avoid auto-handling (which removes the header) HttpClient client = HttpClient.create() .addressSupplier(() -> address(connection)); Tuple2<String, HttpHeaders> resp = //edit the header manually to attempt to trigger compression on server side client.headers(h -> h.add("Accept-Encoding", "gzip")) .get() .uri("/test") .response((res, byteBufFlux) -> byteBufFlux.asString() .zipWith(Mono.just(res.responseHeaders()))) .blockFirst(); //check the server didn't send the gzip header, only transfer-encoding HttpHeaders headers = resp.getT2(); assertThat(headers.get("conTENT-encoding")).isNull(); //check the server sent plain text String reply = resp.getT1(); Assert.assertEquals("reply", reply); connection.dispose(); connection.onDispose() .block(); }
.get("/2", (req, res) -> res.status(301) .header(HttpHeaderNames.LOCATION, "http://localhost:" + serverPort1 + "/3") .send()) .get("/3",
.route(r -> r.get("/301", (req, res) -> res.status(301) .header(HttpHeaderNames.LOCATION, "http://localhost:" + serverPort + "/redirect")) .get("/302", (req, res) -> res.status(302) .header(HttpHeaderNames.LOCATION, "http://localhost:" + serverPort + "/redirect")) .get("/304", (req, res) -> res.status(304)) .get("/307", (req, res) -> res.status(307) .header(HttpHeaderNames.LOCATION, "http://localhost:" + serverPort + "/redirect")) .get("/308", (req, res) -> res.status(308) .header(HttpHeaderNames.LOCATION, "http://localhost:" + serverPort + "/redirect")) .get("/predicate", (req, res) -> res.header("test", "test") .status(302) .header(HttpHeaderNames.LOCATION, "http://localhost:" + serverPort + "/redirect")) .get("/redirect", (req, res) -> res.sendString(Mono.just("OK")))) .wiretap(true)