@@ -654,4 +654,53 @@ public void simultaneousGetAndPostDoesNotOverrideTokenInSession() throws Excepti
654654 req .putHeader ("Cookie" , cookieJar .get ());
655655 }, null , 200 , "OK" , null );
656656 }
657+
658+ @ Test
659+ public void testRerouteRequest () throws Exception {
660+ final AtomicReference <String > sessionCookieJar = new AtomicReference <>();
661+ final AtomicReference <String > cookieJar = new AtomicReference <>();
662+
663+ router .route ().handler (SessionHandler .create (LocalSessionStore .create (vertx )));
664+ router .route ("/home" ).handler (rc -> rc .end ("home" ));
665+ router .route ("/protected/*" ).handler (CSRFHandler .create (vertx , "Abracadabra" ));
666+ router .route ("/protected/initial" ).handler (rc -> rc .reroute ("/protected/rerouted" ));
667+ router .route ("/protected/rerouted" ).handler (rc -> rc .end ("done" ));
668+
669+ // get a session, if first request is rerouted we don't get a session because of seenHandler check in SessionHandlerImpl
670+ // and context cleaning in reroute function
671+ testRequest (HttpMethod .GET , "/home" , null , resp -> {
672+ List <String > cookies = resp .headers ().getAll ("set-cookie" );
673+ assertEquals (1 , cookies .size ());
674+
675+ StringBuilder encodedCookie = new StringBuilder ();
676+ // save the cookies
677+ for (String cookie : cookies ) {
678+ encodedCookie .append (cookie , 0 , cookie .indexOf (';' ));
679+ encodedCookie .append ("; " );
680+ }
681+ sessionCookieJar .set (encodedCookie .toString ());
682+ }, 200 , "OK" , null );
683+
684+ testRequest (HttpMethod .GET , "/protected/initial" , req -> req .putHeader ("cookie" , sessionCookieJar .get ()), resp -> {
685+ List <String > cookies = resp .headers ().getAll ("set-cookie" );
686+ assertEquals (1 , cookies .size ()); // reroute loses session cookie
687+
688+ StringBuilder encodedCookie = new StringBuilder ();
689+ // save the cookies
690+ for (String cookie : cookies ) {
691+ encodedCookie .append (cookie , 0 , cookie .indexOf (';' ));
692+ encodedCookie .append ("; " );
693+ if (cookie .startsWith (CSRFHandler .DEFAULT_COOKIE_NAME )) {
694+ tmpCookie = cookie .substring (cookie .indexOf ('=' ) + 1 , cookie .indexOf (';' ));
695+ }
696+ }
697+ cookieJar .set (sessionCookieJar .get () + encodedCookie );
698+ }, 200 , "OK" , null );
699+
700+ // POST shall be OK as the token and session align
701+ testRequest (HttpMethod .POST , "/protected/rerouted" , req -> {
702+ req .putHeader ("cookie" , cookieJar .get ());
703+ req .putHeader (CSRFHandler .DEFAULT_HEADER_NAME , tmpCookie );
704+ }, null , 200 , "OK" , null );
705+ }
657706}
0 commit comments