@Test public void testSetRegexGroupsNamesMethod() throws Exception { List<String> groupNames = new ArrayList<>(); groupNames.add("hello"); Route route1 = router.getWithRegex("\\/(?<p0>[a-z]{2})"); route1.setRegexGroupsNames(groupNames); route1.handler(routingContext -> routingContext .response() .setStatusCode(200) .setStatusMessage(routingContext.pathParam("hello")) .end()); testRequest(HttpMethod.GET, "/hi", 200, "hi"); }
@Test public void testRegexGroupsNamesWithMethodOverride() throws Exception { List<String> groupNames = new ArrayList<>(); groupNames.add("FirstParam"); groupNames.add("SecondParam"); Route route = router.getWithRegex("\\/([a-z]{2})([a-z]{2})"); route.setRegexGroupsNames(groupNames); route.handler(routingContext -> routingContext .response() .setStatusCode(200) .setStatusMessage(routingContext.pathParam("FirstParam") + "-" + routingContext.pathParam("SecondParam")) .end()); testRequest(HttpMethod.GET, "/aabb", 200, "aa-bb"); }
@Test public void testSetRegexGroupsNamesMethodWithUnorderedGroups() throws Exception { List<String> groupNames = new ArrayList<>(); groupNames.add("firstParam"); groupNames.add("secondParam"); Route route1 = router.getWithRegex("\\/(?<p1>[a-z]{2})(?<p0>[a-z]{2})"); route1.setRegexGroupsNames(groupNames); route1.handler(routingContext -> routingContext .response() .setStatusCode(200) .setStatusMessage(routingContext.pathParam("firstParam") + "-" + routingContext.pathParam("secondParam")) .end()); testRequest(HttpMethod.GET, "/bbaa", 200, "aa-bb"); }
@Test public void testSetRegexGroupsNamesMethodWithNestedRegex() throws Exception { List<String> groupNames = new ArrayList<>(); groupNames.add("firstParam"); groupNames.add("secondParam"); Route route1 = router.getWithRegex("\\/(?<p1>[a-z]{2}(?<p0>[a-z]{2}))"); route1.setRegexGroupsNames(groupNames); route1.handler(routingContext -> routingContext .response() .setStatusCode(200) .setStatusMessage(routingContext.pathParam("firstParam") + "-" + routingContext.pathParam("secondParam")) .end()); testRequest(HttpMethod.GET, "/bbaa", 200, "aa-bbaa"); }
router.getWithRegex("\\/iframe-[^\\/]*\\.html").handler(iframeHandler);
WebSocketTransport(Vertx vertx, Router router, LocalMap<String, SockJSSession> sessions, SockJSHandlerOptions options, Handler<SockJSSocket> sockHandler) { super(vertx, sessions, options); String wsRE = COMMON_PATH_ELEMENT_RE + "websocket"; router.getWithRegex(wsRE).handler(rc -> { HttpServerRequest req = rc.request(); String connectionHeader = req.headers().get(io.vertx.core.http.HttpHeaders.CONNECTION); if (connectionHeader == null || !connectionHeader.toLowerCase().contains("upgrade")) { rc.response().setStatusCode(400); rc.response().end("Can \"Upgrade\" only to \"WebSocket\"."); } else { ServerWebSocket ws = rc.request().upgrade(); if (log.isTraceEnabled()) log.trace("WS, handler"); SockJSSession session = new SockJSSession(vertx, sessions, rc, options.getHeartbeatInterval(), sockHandler); session.register(req, new WebSocketListener(ws, session)); } }); router.getWithRegex(wsRE).handler(rc -> { if (log.isTraceEnabled()) log.trace("WS, get: " + rc.request().uri()); rc.response().setStatusCode(400); rc.response().end("Can \"Upgrade\" only to \"WebSocket\"."); }); router.routeWithRegex(wsRE).handler(rc -> { if (log.isTraceEnabled()) log.trace("WS, all: " + rc.request().uri()); rc.response().putHeader("Allow", "GET").setStatusCode(405).end(); }); }
HtmlFileTransport(Vertx vertx, Router router, LocalMap<String, SockJSSession> sessions, SockJSHandlerOptions options, Handler<SockJSSocket> sockHandler) { super(vertx, sessions, options); String htmlFileRE = COMMON_PATH_ELEMENT_RE + "htmlfile.*"; router.getWithRegex(htmlFileRE).handler(rc -> { if (log.isTraceEnabled()) log.trace("HtmlFile, get: " + rc.request().uri()); String callback = rc.request().getParam("callback"); if (callback == null) { callback = rc.request().getParam("c"); if (callback == null) { rc.response().setStatusCode(500).end("\"callback\" parameter required\n"); return; } } if (CALLBACK_VALIDATION.matcher(callback).find()) { rc.response().setStatusCode(500); rc.response().end("invalid \"callback\" parameter\n"); return; } HttpServerRequest req = rc.request(); String sessionID = req.params().get("param0"); SockJSSession session = getSession(rc, options.getSessionTimeout(), options.getHeartbeatInterval(), sessionID, sockHandler); session.register(req, new HtmlFileListener(options.getMaxBytesStreaming(), rc, callback, session)); }); }
@Test public void testRegexGroupsNames() throws Exception { router.getWithRegex("\\/(?<firstParam>[a-z]{2})(?<secondParam>[a-z]{2})").handler(routingContext -> routingContext .response() .setStatusCode(200) .setStatusMessage(routingContext.pathParam("firstParam") + "-" + routingContext.pathParam("secondParam")) .end()); testRequest(HttpMethod.GET, "/aabb", 200, "aa-bb"); }
@Test public void testRegexGroupsNamesWithNestedGroups() throws Exception { router.getWithRegex("\\/(?<secondParam>[a-z]{2}(?<firstParam>[a-z]{2}))").handler(routingContext -> routingContext .response() .setStatusCode(200) .setStatusMessage(routingContext.pathParam("firstParam") + "-" + routingContext.pathParam("secondParam")) .end()); testRequest(HttpMethod.GET, "/bbaa", 200, "aa-bbaa"); }
EventSourceTransport(Vertx vertx, Router router, LocalMap<String, SockJSSession> sessions, SockJSHandlerOptions options, Handler<SockJSSocket> sockHandler) { super(vertx, sessions, options); String eventSourceRE = COMMON_PATH_ELEMENT_RE + "eventsource"; router.getWithRegex(eventSourceRE).handler(rc -> { if (log.isTraceEnabled()) log.trace("EventSource transport, get: " + rc.request().uri()); String sessionID = rc.request().getParam("param0"); SockJSSession session = getSession(rc, options.getSessionTimeout(), options.getHeartbeatInterval(), sessionID, sockHandler); HttpServerRequest req = rc.request(); session.register(req, new EventSourceListener(options.getMaxBytesStreaming(), rc, session)); }); }
@Test public void testGetWithRegex() throws Exception { router.getWithRegex("\\/somepath\\/.*").handler(rc -> rc.response().setStatusMessage("foo").end()); testRequest(HttpMethod.GET, "/somepath/whatever", 200, "foo"); testRequest(HttpMethod.GET, "/otherpath/whatever", 404, "Not Found"); testRequest(HttpMethod.POST, "/somepath/whatever", 404, "Not Found"); testRequest(HttpMethod.PUT, "/somepath/whatever", 404, "Not Found"); testRequest(HttpMethod.DELETE, "/somepath/whatever", 404, "Not Found"); testRequest(HttpMethod.OPTIONS, "/somepath/whatever", 404, "Not Found"); testRequest(HttpMethod.HEAD, "/somepath/whatever", 404, "Not Found"); }
router.getWithRegex(jsonpRE).handler(rc -> { if (log.isTraceEnabled()) log.trace("JsonP, get: " + rc.request().uri()); String callback = rc.request().getParam("callback");
/** * Add a route that matches a HTTP GET request and the specified path regex * @param regex URI paths that begin with a match for this regex will match * @return the route */ public io.vertx.rxjava.ext.web.Route getWithRegex(String regex) { io.vertx.rxjava.ext.web.Route ret = io.vertx.rxjava.ext.web.Route.newInstance(delegate.getWithRegex(regex)); return ret; }
/** * Add a route that matches a HTTP GET request and the specified path regex * @param regex URI paths that begin with a match for this regex will match * @return the route */ public io.vertx.rxjava.ext.web.Route getWithRegex(String regex) { io.vertx.rxjava.ext.web.Route ret = io.vertx.rxjava.ext.web.Route.newInstance(delegate.getWithRegex(regex)); return ret; }
@Test public void testRegexGroupsNamesWithMethodOverride() throws Exception { List<String> groupNames = new ArrayList<>(); groupNames.add("FirstParam"); groupNames.add("SecondParam"); Route route = router.getWithRegex("\\/([a-z]{2})([a-z]{2})"); route.setRegexGroupsNames(groupNames); route.handler(routingContext -> routingContext .response() .setStatusCode(200) .setStatusMessage(routingContext.pathParam("FirstParam") + "-" + routingContext.pathParam("SecondParam")) .end()); testRequest(HttpMethod.GET, "/aabb", 200, "aa-bb"); }
@Test public void testSetRegexGroupsNamesMethodWithNestedRegex() throws Exception { List<String> groupNames = new ArrayList<>(); groupNames.add("firstParam"); groupNames.add("secondParam"); Route route1 = router.getWithRegex("\\/(?<p1>[a-z]{2}(?<p0>[a-z]{2}))"); route1.setRegexGroupsNames(groupNames); route1.handler(routingContext -> routingContext .response() .setStatusCode(200) .setStatusMessage(routingContext.pathParam("firstParam") + "-" + routingContext.pathParam("secondParam")) .end()); testRequest(HttpMethod.GET, "/bbaa", 200, "aa-bbaa"); }
@Test public void testRegexGroupsNamesWithNestedGroups() throws Exception { router.getWithRegex("\\/(?<secondParam>[a-z]{2}(?<firstParam>[a-z]{2}))").handler(routingContext -> routingContext .response() .setStatusCode(200) .setStatusMessage(routingContext.pathParam("firstParam") + "-" + routingContext.pathParam("secondParam")) .end()); testRequest(HttpMethod.GET, "/bbaa", 200, "aa-bbaa"); }
@Test public void testRegexGroupsNames() throws Exception { router.getWithRegex("\\/(?<firstParam>[a-z]{2})(?<secondParam>[a-z]{2})").handler(routingContext -> routingContext .response() .setStatusCode(200) .setStatusMessage(routingContext.pathParam("firstParam") + "-" + routingContext.pathParam("secondParam")) .end()); testRequest(HttpMethod.GET, "/aabb", 200, "aa-bb"); }
EventSourceTransport(Vertx vertx, Router router, LocalMap<String, SockJSSession> sessions, SockJSHandlerOptions options, Handler<SockJSSocket> sockHandler) { super(vertx, sessions, options); String eventSourceRE = COMMON_PATH_ELEMENT_RE + "eventsource"; router.getWithRegex(eventSourceRE).handler(rc -> { if (log.isTraceEnabled()) log.trace("EventSource transport, get: " + rc.request().uri()); String sessionID = rc.request().getParam("param0"); SockJSSession session = getSession(rc, options.getSessionTimeout(), options.getHeartbeatInterval(), sessionID, sockHandler); HttpServerRequest req = rc.request(); session.register(req, new EventSourceListener(options.getMaxBytesStreaming(), rc, session)); }); }
@Test public void testGetWithRegex() throws Exception { router.getWithRegex("\\/somepath\\/.*").handler(rc -> rc.response().setStatusMessage("foo").end()); testRequest(HttpMethod.GET, "/somepath/whatever", 200, "foo"); testRequest(HttpMethod.GET, "/otherpath/whatever", 404, "Not Found"); testRequest(HttpMethod.POST, "/somepath/whatever", 404, "Not Found"); testRequest(HttpMethod.PUT, "/somepath/whatever", 404, "Not Found"); testRequest(HttpMethod.DELETE, "/somepath/whatever", 404, "Not Found"); testRequest(HttpMethod.OPTIONS, "/somepath/whatever", 404, "Not Found"); testRequest(HttpMethod.HEAD, "/somepath/whatever", 404, "Not Found"); }