@Override public String path() { return request.path(); }
public Map.Entry<String, RouteState> lookup(Request request) { for (Map.Entry<String, RouteState> entry : routes.entrySet()) { RouteState route = entry.getValue(); if (route.matches(request.path())) { return entry; } } return defaultEntry; } }
public String buildProxyPath(Request request, RouteState state) { Optional<String> pathSuffix = state .route() .groups(request.path()) // apply the regex .entrySet() .stream() // stream the entry set of matches .filter(e -> e.getKey().equals("path")) // find the entry with key path .map(e -> e.getValue()) // extract the value (aka what did the regex match) .findFirst(); // extract the first (Optional) result return pathSuffix .map(config.proxyPath()::concat) // append the path suffix (if it exists) .orElse(config.proxyPath()); // use the provided path with no suffix }
@Test public void testAlreadyConnectedWrites() throws Exception { when(connectionManager.connectionState()).thenReturn(ClientConnectionState.CONNECTED); Request request1 = requestFactory("req1"); Request request2 = requestFactory("req2"); Optional<ChannelFuture> result1Future = subject.write(request1); Optional<ChannelFuture> result2Future = subject.write(request2); Request proxiedRequest1 = channel.readOutbound(); Request proxiedRequest2 = channel.readOutbound(); assertEquals(proxiedRequest1.path(), request1.path()); assertEquals(proxiedRequest2.path(), request2.path()); assertTrue(result1Future.get().isDone() && result1Future.get().isSuccess()); assertTrue(result2Future.get().isDone() && result2Future.get().isSuccess()); }
when(request1.path()).thenReturn("dontcare"); when(request1.streamId()).thenReturn(Message.H1_STREAM_ID_NONE); when(request2.isFullMessage()).thenReturn(true); when(request2.path()).thenReturn("dontcare"); when(request2.streamId()).thenReturn(Message.H1_STREAM_ID_NONE); when(request3.isFullMessage()).thenReturn(true); when(request3.path()).thenReturn("dontcare"); when(request3.streamId()).thenReturn(Message.H1_STREAM_ID_NONE); when(request4.isFullMessage()).thenReturn(true); when(request4.path()).thenReturn("dontcare"); when(request4.streamId()).thenReturn(Message.H1_STREAM_ID_NONE);
when(request1.startOfMessage()).thenReturn(true); when(request1.endOfMessage()).thenReturn(false); when(request1.path()).thenReturn("/foo"); when(request1.streamId()).thenReturn(Message.H1_STREAM_ID_NONE); when(request2.startOfMessage()).thenReturn(false); when(request2.endOfMessage()).thenReturn(false); when(request2.path()).thenReturn("/foo"); when(request2.streamId()).thenReturn(Message.H1_STREAM_ID_NONE); when(request3.startOfMessage()).thenReturn(false); when(request3.endOfMessage()).thenReturn(true); when(request3.path()).thenReturn("/foo"); when(request3.streamId()).thenReturn(Message.H1_STREAM_ID_NONE);
when(request1.path()).thenReturn("dontcare"); when(request1.streamId()).thenReturn(Message.H1_STREAM_ID_NONE); when(request2.isFullMessage()).thenReturn(true); when(request2.path()).thenReturn("dontcare"); when(request2.streamId()).thenReturn(Message.H1_STREAM_ID_NONE); when(request3.isFullMessage()).thenReturn(true); when(request3.path()).thenReturn("dontcare"); when(request3.streamId()).thenReturn(Message.H1_STREAM_ID_NONE); when(request4.isFullMessage()).thenReturn(true); when(request4.path()).thenReturn("dontcare"); when(request4.streamId()).thenReturn(Message.H1_STREAM_ID_NONE);
@Test public void testInitialConnectedWrites() throws Exception { ChannelPromise connectPromise = channel.newPromise(); when(connectionManager.connectionState()).thenReturn(ClientConnectionState.NOT_CONNECTED); when(connectionManager.connect()).thenReturn(connectPromise); Request request1 = requestFactory("req1"); Optional<ChannelFuture> result1Future = subject.write(request1); verify(connectionManager).connect(); Request proxiedRequest1BeforeConnection = channel.readOutbound(); assertNull(proxiedRequest1BeforeConnection); connectPromise.setSuccess(); Request proxiedRequest1AfterConnectionComplete = channel.readOutbound(); assertEquals(proxiedRequest1AfterConnectionComplete.path(), request1.path()); assertTrue(result1Future.get().isDone() && result1Future.get().isSuccess()); }
when(request1.startOfMessage()).thenReturn(true); when(request1.endOfMessage()).thenReturn(false); when(request1.path()).thenReturn("/foo"); when(request1.streamId()).thenReturn(Message.H1_STREAM_ID_NONE); when(request2.startOfMessage()).thenReturn(false); when(request2.endOfMessage()).thenReturn(false); when(request2.path()).thenReturn("/foo"); when(request2.streamId()).thenReturn(Message.H1_STREAM_ID_NONE); when(request3.startOfMessage()).thenReturn(false); when(request3.endOfMessage()).thenReturn(true); when(request3.path()).thenReturn("/foo"); when(request3.streamId()).thenReturn(Message.H1_STREAM_ID_NONE);
@Test public void testConnectionInProcessWrites() throws Exception { ChannelPromise connectPromise = channel.newPromise(); when(connectionManager.connectionState()).thenReturn(ClientConnectionState.NOT_CONNECTED); when(connectionManager.connect()).thenReturn(connectPromise); Request request1 = requestFactory("req1"); Request request2 = requestFactory("req2"); Request request3 = requestFactory("req3"); Optional<ChannelFuture> result1Future = subject.write(request1); when(connectionManager.connectionState()).thenReturn(ClientConnectionState.CONNECTING); Optional<ChannelFuture> result2Future = subject.write(request2); Optional<ChannelFuture> result3Future = subject.write(request3); // shouldn't get anything written on the channel at all Request proxiedRequest1BeforeConnection = channel.readOutbound(); assertNull(proxiedRequest1BeforeConnection); connectPromise.setSuccess(); Request proxiedRequest1AfterConnectionComplete = channel.readOutbound(); Request proxiedRequest2AfterConnectionComplete = channel.readOutbound(); Request proxiedRequest3AfterConnectionComplete = channel.readOutbound(); assertEquals(proxiedRequest1AfterConnectionComplete.path(), request1.path()); assertEquals(proxiedRequest2AfterConnectionComplete.path(), request2.path()); assertEquals(proxiedRequest3AfterConnectionComplete.path(), request3.path()); assertTrue(result1Future.get().isDone() && result1Future.get().isSuccess()); assertTrue(result2Future.get().isDone() && result2Future.get().isSuccess()); assertTrue(result3Future.get().isDone() && result3Future.get().isSuccess()); }
private void writeRequest(ChannelHandlerContext ctx, Request request, ChannelPromise promise) { /* // TOOD(CK): define ACCEPT? if (!response.headers().contains(HttpHeaderNames.CONTENT_TYPE)) { response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8"); } */ Http2Headers headers = request.headers().http2Headers(); headers.authority(request.host()).method(request.method().asciiName()).path(request.path()); int streamId = request.streamId(); if (request instanceof FullRequest) { if (request.body().readableBytes() > 0) { PromiseCombiner combiner = new PromiseCombiner(); combiner.add(ctx.write(Http2Request.build(streamId, headers, false), ctx.newPromise())); Http2DataFrame data = new DefaultHttp2DataFrame(request.body(), true); combiner.add(ctx.write(Http2Request.build(streamId, data, true), ctx.newPromise())); combiner.finish(promise); } else { ctx.write(Http2Request.build(streamId, headers, true), promise); } } else { ctx.write(Http2Request.build(streamId, headers, false), promise); } }
.buildSpan(name + ".client") .withTag(Tags.HTTP_METHOD.getKey(), request.method().toString()) .withTag(Tags.HTTP_URL.getKey(), request.path()) .withTag("http.request.type", httpType) .withTag("http.request.streamId", request.streamId())
.buildSpan(name) .withTag(Tags.HTTP_METHOD.getKey(), request.method().toString()) .withTag(Tags.HTTP_URL.getKey(), request.path()) .withTag("http.request.type", httpType) .withTag("http.request.streamId", request.streamId())
@Test public void testFullRequest() throws Exception { outputReceived = new CountDownLatch(1); FullHttpRequest requestIn = new DefaultFullHttpRequest(HTTP_1_1, GET, "/"); channel.writeInbound(requestIn); channel.runPendingTasks(); // blocks Uninterruptibles.awaitUninterruptibly(outputReceived); Request requestOut = requests.remove(0); assertTrue(requestOut != null); assertTrue(requestOut instanceof FullRequest); assertEquals("HTTP/1.1", requestOut.version()); assertEquals(HttpMethod.GET, requestOut.method()); assertEquals("/", requestOut.path()); assertFalse(requestOut.hasBody()); assertFalse(requestOut.body() == null); assertEquals(0, requestOut.body().readableBytes()); }
HttpVersion.HTTP_1_1, request.method(), request.path(), request.headers().http1Headers(false, true));
@Test public void testFullRequest() throws Exception { outputReceived = new CountDownLatch(1); Http2Headers headers = new DefaultHttp2Headers().method("GET").path("/"); Http2Request requestIn = Http2Request.build(1, headers, true); channel.writeInbound(requestIn); channel.runPendingTasks(); // blocks Uninterruptibles.awaitUninterruptibly(outputReceived); Request requestOut = requests.remove(0); assertNotNull(requestOut); assertTrue(requestOut instanceof FullRequest); assertEquals("h2", requestOut.version()); assertEquals(HttpMethod.GET, requestOut.method()); assertEquals("/", requestOut.path()); assertFalse(requestOut.hasBody()); assertNotNull(requestOut.body()); assertEquals(0, requestOut.body().readableBytes()); assertEquals(1, requestOut.streamId()); }
@Test public void testFullRequestWithBody() throws Exception { outputReceived = new CountDownLatch(1); String payload = "body"; ByteBuf body = ByteBufUtil.writeUtf8(UnpooledByteBufAllocator.DEFAULT, payload); FullHttpRequest requestIn = new DefaultFullHttpRequest(HTTP_1_1, GET, "/", body); channel.writeInbound(requestIn); channel.runPendingTasks(); // blocks Uninterruptibles.awaitUninterruptibly(outputReceived); Request requestOut = requests.remove(0); assertTrue(requestOut != null); assertTrue(requestOut instanceof FullRequest); assertEquals("HTTP/1.1", requestOut.version()); assertEquals(HttpMethod.GET, requestOut.method()); assertEquals("/", requestOut.path()); assertTrue(requestOut.hasBody()); assertFalse(requestOut.body() == null); assertEquals(body, requestOut.body()); }
assertEquals("HTTP/1.1", requestOut.version()); assertEquals(HttpMethod.GET, requestOut.method()); assertEquals("/", requestOut.path()); assertFalse(requestOut.hasBody()); assertFalse(requestOut.body() == null); assertEquals("HTTP/1.1", bodyOut1.version()); assertEquals(HttpMethod.GET, bodyOut1.method()); assertEquals("/", bodyOut1.path()); assertFalse(bodyOut1.hasBody()); assertFalse(bodyOut1.body() == null); assertEquals("HTTP/1.1", bodyOut2.version()); assertEquals(HttpMethod.GET, bodyOut2.method()); assertEquals("/", bodyOut2.path()); assertFalse(bodyOut2.hasBody()); assertFalse(bodyOut2.body() == null);
assertEquals("h2", requestOut.version()); assertEquals(HttpMethod.POST, requestOut.method()); assertEquals("/", requestOut.path()); assertFalse(requestOut.hasBody()); assertNotNull(requestOut.body()); assertEquals("h2", bodyOut1.version()); assertEquals(HttpMethod.POST, bodyOut1.method()); assertEquals("/", bodyOut1.path()); assertFalse(bodyOut1.hasBody()); assertNotNull(bodyOut1.body()); assertEquals("h2", bodyOut2.version()); assertEquals(HttpMethod.POST, bodyOut2.method()); assertEquals("/", bodyOut2.path()); assertFalse(bodyOut2.hasBody()); assertNotNull(bodyOut2.body());
assertEquals("h2", requestOut.version()); assertEquals(HttpMethod.POST, requestOut.method()); assertEquals("/", requestOut.path()); assertFalse(requestOut.hasBody()); assertNotNull(requestOut.body()); assertEquals("h2", bodyOut1.version()); assertEquals(HttpMethod.POST, bodyOut1.method()); assertEquals("/", bodyOut1.path()); assertFalse(bodyOut1.hasBody()); assertNotNull(bodyOut1.body()); assertEquals("h2", bodyOut2.version()); assertEquals(HttpMethod.POST, bodyOut2.method()); assertEquals("/", bodyOut2.path()); assertFalse(bodyOut2.hasBody()); assertNotNull(bodyOut2.body()); assertEquals("h2", trailersOut.version()); assertEquals(HttpMethod.POST, trailersOut.method()); assertEquals("/", trailersOut.path()); assertFalse(trailersOut.hasBody()); assertNotNull(trailersOut.body());