package test.http.router.handler.body;

import com.firefly.$;
import com.firefly.codec.http2.model.Cookie;
import com.firefly.server.http2.HTTP2ServerBuilder;
import com.firefly.server.http2.router.handler.error.DefaultErrorResponseHandler;
import com.firefly.server.http2.router.handler.session.HTTPSessionConfiguration;
import com.firefly.server.http2.router.handler.session.LocalHTTPSessionHandler;
import org.junit.Assert;
import org.junit.Test;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.Phaser;

import static org.hamcrest.Matchers.is;

/**
 * @author Pengtao Qiu
 */
public class TestLocalHTTPSessionHandler extends AbstractHTTPHandlerTest {

    @Test
    public void test() throws Exception {
        int maxGetSession = 3;
        Phaser phaser = new Phaser(1 + maxGetSession);
        HTTP2ServerBuilder httpServer = $.httpServer();
        httpServer.router().path("*").handler(new LocalHTTPSessionHandler(new HTTPSessionConfiguration()))
                  .router().path("*").handler(new DefaultErrorResponseHandler())
                  .router().post("/session/:name")
                  .handler(ctx -> {
                      String name = ctx.getRouterParameter("name");
                      System.out.println("the path param -> " + name);
                      Assert.assertThat(name, is("foo"));
                      HttpSession session = ctx.getSession(true);
                      session.setAttribute(name, "bar");
                      session.setMaxInactiveInterval(1); // 1 second later, the session will expire
                      ctx.end("create session success");
                  })
                  .router().get("/session/:name")
                  .handler(ctx -> {
                      String name = ctx.getRouterParameter("name");
                      Assert.assertThat(name, is("foo"));
                      HttpSession session = ctx.getSession();
                      if (session != null) {
                          Assert.assertThat(session.getAttribute("foo"), is("bar"));
                          ctx.end("session value is " + session.getAttribute("foo"));
                      } else {
                          ctx.end("session is invalid");
                      }
                  })
                  .listen(host, port);

        List<Cookie> c = $.httpClient().post(uri + "/session/foo").submit()
                          .thenApply(res -> {
                              List<Cookie> cookies = res.getCookies();
                              System.out.println(res.getStatus());
                              System.out.println(cookies);
                              System.out.println(res.getStringBody());
                              Assert.assertThat(res.getStringBody(), is("create session success"));
                              return cookies;
                          })
                          .thenApply(cookies -> {
                              for (int i = 0; i < maxGetSession; i++) {
                                  $.httpClient().get(uri + "/session/foo").cookies(cookies).submit()
                                   .thenAccept(res2 -> {
                                       String sessionFoo = res2.getStringBody();
                                       System.out.println(sessionFoo);
                                       Assert.assertThat(sessionFoo, is("session value is bar"));
                                       phaser.arrive();
                                   });
                              }
                              return cookies;
                          }).get();

//        $.thread.sleep(4000);
//        $.httpClient().get(uri + "/session/foo").cookies(c).submit()
//         .thenAccept(res2 -> {
//             String body = res2.getStringBody();
//             System.out.println(body);
//             Assert.assertThat(body, is("session is invalid"));
//             phaser.arrive();
//         });

        phaser.arriveAndAwaitAdvance();
        httpServer.stop();
        $.httpClient().stop();
    }
}

@@ -897,46 +897,49 @@
     
-httpServer.router().path(%22*%22).h
+LocalHTTPSessionHandler sessionH
 andler
-(
+ = 
 new 
@@ -993,16 +993,78 @@
 ation())
+;%0A        httpServer.router().path(%22*%22).handler(sessionHandler
 )%0A      
@@ -2397,25 +2397,8 @@
     
- List%3CCookie%3E c =
  $.h
@@ -2445,33 +2445,16 @@
 ubmit()%0A
-                 
         
@@ -2487,33 +2487,16 @@
         
-          
-       
 List%3CCoo
@@ -2528,33 +2528,16 @@
 kies();%0A
-                 
         
@@ -2591,33 +2591,16 @@
         
-         
-        
 System.o
@@ -2620,33 +2620,16 @@
 okies);%0A
-                 
         
@@ -2687,33 +2687,16 @@
         
-        
-         
 Assert.a
@@ -2757,33 +2757,16 @@
 ess%22));%0A
-                 
         
@@ -2799,45 +2799,11 @@
     
-                 %7D)%0A                 
+%7D)%0A
     
@@ -2844,33 +2844,16 @@
         
-     
-            
 for (int
@@ -2903,33 +2903,16 @@
         
-          
-       
 $.httpCl
@@ -2988,33 +2988,16 @@
         
-               
-  
 .thenAcc
@@ -3010,33 +3010,16 @@
 s2 -%3E %7B%0A
-                 
         
@@ -3096,33 +3096,16 @@
         
-   
-              
 System.o
@@ -3150,33 +3150,16 @@
         
-        
-         
 Assert.a
@@ -3231,33 +3231,16 @@
         
-             
-    
 phaser.a
@@ -3270,46 +3270,12 @@
     
-                 %7D);%0A                 
+%7D);%0A
     
@@ -3298,33 +3298,16 @@
         
-      
-           
 return c
@@ -3327,374 +3327,15 @@
     
-                 %7D).get();%0A%0A//        $.thread.sleep(4000);%0A//        $.httpClient().get(uri + %22/session/foo%22).cookies(c).submit()%0A//         .thenAccept(res2 -%3E %7B%0A//             String body = res2.getStringBody();%0A//             System.out.println(body);%0A//             Assert.assertThat(body, is(%22session is invalid%22));%0A//             phaser.arrive();%0A//         %7D
+%7D).get(
 );%0A%0A
@@ -3397,24 +3397,24 @@
 ver.stop();%0A
-
         $.ht
@@ -3428,20 +3428,51 @@
 t().stop();%0A
+        sessionHandler.stop();%0A
     %7D%0A%7D%0A

Clean up code