@Override public void handle(Void aVoid) { if (!finished.compareAndSet(false, true)) return; VertxHttpServerAdapter.setCurrentMethodAndPath( context.request().rawMethod(), context.currentRoute().getPath() ); try { serverHandler.handleSend(context.response(), context.failure(), span); } finally { VertxHttpServerAdapter.setCurrentMethodAndPath(null, null); } } }
private String getAndCheckRoutePath(RoutingContext ctx) { Route currentRoute = ctx.currentRoute(); String path = currentRoute.getPath(); if (path == null) { throw new IllegalStateException("Sub routers must be mounted on constant paths (no regex or patterns)"); } return path; }
public static String pathOffset(String path, RoutingContext context) { int prefixLen = 0; String mountPoint = context.mountPoint(); if (mountPoint != null) { prefixLen = mountPoint.length(); } String routePath = context.currentRoute().getPath(); if (routePath != null) { prefixLen += routePath.length(); // special case we need to verify if a trailing slash is present and exclude if (routePath.charAt(routePath.length() - 1) == '/') { prefixLen--; } } return prefixLen != 0 ? path.substring(prefixLen) : path; }
private String authURI(String redirectURL) { final JsonObject config = new JsonObject() .put("state", redirectURL); if (host != null) { config.put("redirect_uri", host + callback.getPath()); } if (extraParams != null) { config.mergeIn(extraParams); } if (scopes.size() > 0) { JsonArray _scopes = new JsonArray(); // scopes are passed as an array because the auth provider has the knowledge on how to encode them for (String authority : scopes) { _scopes.add(authority); } config.put("scopes", _scopes); } return ((OAuth2Auth) authProvider).authorizeURL(config); }
context.normalisedPath().equals(callback.getPath())) {
@Test public void testDefaultIndex() { TemplateEngine templateEngine = mock(TemplateEngine.class); RoutingContext routingContext = mock(RoutingContext.class); when(routingContext.normalisedPath()).thenReturn("/"); Route currentRoute = mock(Route.class); when(currentRoute.getPath()).thenReturn("/"); when(routingContext.currentRoute()).thenReturn(currentRoute); TemplateHandler templateHandler = new TemplateHandlerImpl(templateEngine, "templates", "ext"); templateHandler.handle(routingContext); verify(templateEngine).render(any(JsonObject.class), eq("templates/index"), any()); }
@Test public void testSimpleTemplate() { TemplateEngine templateEngine = mock(TemplateEngine.class); RoutingContext routingContext = mock(RoutingContext.class); when(routingContext.normalisedPath()).thenReturn("/about"); Route currentRoute = mock(Route.class); when(currentRoute.getPath()).thenReturn("/"); when(routingContext.currentRoute()).thenReturn(currentRoute); TemplateHandler templateHandler = new TemplateHandlerImpl(templateEngine, "templates", "ext"); templateHandler.handle(routingContext); verify(templateEngine).render(any(JsonObject.class), eq("templates/about"), any()); }
@Test public void testSetIndex() { TemplateEngine templateEngine = mock(TemplateEngine.class); RoutingContext routingContext = mock(RoutingContext.class); when(routingContext.normalisedPath()).thenReturn("/"); Route currentRoute = mock(Route.class); when(currentRoute.getPath()).thenReturn("/"); when(routingContext.currentRoute()).thenReturn(currentRoute); TemplateHandler templateHandler = new TemplateHandlerImpl(templateEngine, "templates", "ext"); templateHandler.setIndexTemplate("home"); templateHandler.handle(routingContext); verify(templateEngine).render(any(JsonObject.class), eq("templates/home"), any()); }
@Test public void testTurnOffIndex() { TemplateEngine templateEngine = mock(TemplateEngine.class); RoutingContext routingContext = mock(RoutingContext.class); when(routingContext.normalisedPath()).thenReturn("/"); Route currentRoute = mock(Route.class); when(currentRoute.getPath()).thenReturn("/"); when(routingContext.currentRoute()).thenReturn(currentRoute); TemplateHandler templateHandler = new TemplateHandlerImpl(templateEngine, "templates", "ext"); templateHandler.setIndexTemplate(null); templateHandler.handle(routingContext); verify(templateEngine).render(any(JsonObject.class), eq("templates/"), any()); }
config.put("redirect_uri", host + route.getPath());
@Test public void testFixedContent() { Buffer buffer = new JsonObject().put("toto", "titi").toBuffer(); testRoute.produces("application/json").handler(rc -> rc.response().end(buffer)); client.get(testRoute.getPath(), onSuccess(resp -> { assertEquals("application/json", contentType(resp)); assertEquals(Integer.valueOf(buffer.length()), contentLength(resp)); resp.bodyHandler(buf -> { assertEquals(buffer, buf); testComplete(); }); })).putHeader(HttpHeaders.ACCEPT, "application/json").end(); await(); }
@Test public void testNoContent() { testRoute.produces("application/json").handler(rc -> rc.response().end()); client.get(testRoute.getPath(), onSuccess(resp -> { assertNull(contentType(resp)); assertEquals(Integer.valueOf(0), contentLength(resp)); testComplete(); })).putHeader(HttpHeaders.ACCEPT, "application/json").end(); await(); }
@Test public void testDisableFlag() { Random random = new Random(); byte[] bytes = new byte[128]; random.nextBytes(bytes); Buffer buffer = Buffer.buffer(bytes); testRoute.produces("application/json").handler(rc -> { rc.put(ResponseContentTypeHandler.DEFAULT_DISABLE_FLAG, true); rc.response().end(buffer); }); client.get(testRoute.getPath(), onSuccess(resp -> { assertNull(contentType(resp)); assertEquals(Integer.valueOf(buffer.length()), contentLength(resp)); resp.bodyHandler(buf -> { assertEquals(buffer, buf); testComplete(); }); })).putHeader(HttpHeaders.ACCEPT, "application/json").end(); await(); }
@Test public void testNoMatch() { testRoute.handler(rc -> rc.response().end()); client.get(testRoute.getPath(), onSuccess(resp -> { assertNull(contentType(resp)); testComplete(); })).putHeader(HttpHeaders.ACCEPT, "application/json").end(); await(); }
@Test public void testChunkedContent() { Buffer buffer = new JsonObject().put("toto", "titi").toBuffer(); testRoute.produces("application/json").handler(rc -> rc.response().setChunked(true).end(buffer)); client.get(testRoute.getPath(), onSuccess(resp -> { assertEquals("application/json", contentType(resp)); assertNull(contentLength(resp)); resp.bodyHandler(buf -> { assertEquals(buffer, buf); testComplete(); }); })).putHeader(HttpHeaders.ACCEPT, "application/json").end(); await(); }
@Test public void testExistingHeader() { testRoute.produces("application/json").handler(rc -> rc.response().putHeader(CONTENT_TYPE, "text/plain").end()); client.get(testRoute.getPath(), onSuccess(resp -> { assertEquals("text/plain", contentType(resp)); testComplete(); })).putHeader(HttpHeaders.ACCEPT, "application/json").end(); await(); }
@Test public void testDefaultIndex() { TemplateEngine templateEngine = mock(TemplateEngine.class); RoutingContext routingContext = mock(RoutingContext.class); when(routingContext.normalisedPath()).thenReturn("/"); Route currentRoute = mock(Route.class); when(currentRoute.getPath()).thenReturn("/"); when(routingContext.currentRoute()).thenReturn(currentRoute); TemplateHandler templateHandler = new TemplateHandlerImpl(templateEngine, "templates", "ext"); templateHandler.handle(routingContext); verify(templateEngine).render(any(JsonObject.class), eq("templates/index"), any()); }
assertEquals("blah2", rc.get("key2")); assertEquals("blah3", rc.get("key3")); rc.response().setStatusMessage(rc.currentRoute().getPath()).end(); });
@Test public void testNoMatch() { testRoute.handler(rc -> rc.response().end()); client.get(testRoute.getPath(), resp -> { assertNull(contentType(resp)); testComplete(); }).putHeader(HttpHeaders.ACCEPT, "application/json").end(); await(); }