package com.blade.security;

import com.blade.BaseTestCase;
import com.blade.Blade;
import com.blade.mvc.RouteContext;
import com.blade.mvc.WebContext;
import com.blade.mvc.handler.RouteHandler;
import com.blade.mvc.http.HttpRequest;
import com.blade.mvc.http.Request;
import com.blade.mvc.http.Response;
import com.blade.mvc.route.Route;
import com.blade.security.web.auth.BasicAuthMiddleware;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

/**
 * CsrfToken Test
 *
 * @author biezhi
 * 2017/6/5
 */
@Slf4j
public class BasicAuthMiddlewareTest extends BaseTestCase {

    class AuthHandler implements RouteHandler {
        @Override
        public void handle(RouteContext context) {
            context.text("登录成功");
        }

    }

    @Test
    public void testAuthSuccess() throws Exception {

        Request   mockRequest = mockHttpRequest("GET");

        WebContext.init(Blade.of(), "/");


        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", "Basic YmxhZGU6YmxhZGU=");

        when(mockRequest.parameters()).thenReturn(new HashMap<>());
        when(mockRequest.headers()).thenReturn(headers);

        Request  request  = new HttpRequest(mockRequest);
        Response response = mockHttpResponse(200);

        RouteContext context = new RouteContext(request, response);
        context.initRoute(Route.builder()
                .action(AuthHandler.class.getMethod("handle", RouteContext.class))
                .targetType(AuthHandler.class)
                .target(new AuthHandler()).build());

        WebContext.set(new WebContext(request, response));

        BasicAuthMiddleware basicAuthMiddleware = new BasicAuthMiddleware();
        boolean             flag                = basicAuthMiddleware.before(context);
        assertEquals(false, flag);
    }

    @Test
    public void testAuthFail() throws Exception {
        Request   mockRequest = mockHttpRequest("GET");

        WebContext.init(Blade.of(), "/");

        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", "Basic YmxhZGU6YmxhZGUyMg==");

        when(mockRequest.parameters()).thenReturn(new HashMap<>());
        when(mockRequest.headers()).thenReturn(headers);

        Request  request  = new HttpRequest(mockRequest);
        Response response = mockHttpResponse(200);

        RouteContext context = new RouteContext(request, response);

        context.initRoute(Route.builder()
                .action(AuthHandler.class.getMethod("handle", RouteContext.class))
                .targetType(AuthHandler.class)
                .target(new AuthHandler()).build());

        WebContext.set(new WebContext(request, response));

        BasicAuthMiddleware basicAuthMiddleware = new BasicAuthMiddleware();
        boolean             flag                = basicAuthMiddleware.before(context);
        assertEquals(false, flag);
    }

}

@@ -332,16 +332,63 @@
 .Route;%0A
+import com.blade.security.web.auth.AuthOption;%0A
 import c
@@ -435,16 +435,16 @@
 leware;%0A
-
 import l
@@ -587,16 +587,103 @@
 Equals;%0A
+import static org.junit.Assert.assertFalse;%0Aimport static org.junit.Assert.assertTrue;%0A
 import s
@@ -1083,34 +1083,32 @@
         Request 
-  
 mockRequest = mo
@@ -1173,17 +1173,16 @@
  %22/%22);%0A%0A
-%0A
         
@@ -1277,23 +1277,23 @@
 ic Y
-mxhZGU6YmxhZGU=
+WRtaW46MTIzNDU2
 %22);%0A
@@ -1875,32 +1875,142 @@
 t, response));%0A%0A
+        AuthOption authOption = AuthOption.builder().build();%0A        authOption.addUser(%22admin%22, %22123456%22);%0A%0A
         BasicAut
@@ -2059,32 +2059,42 @@
 cAuthMiddleware(
+authOption
 );%0A        boole
@@ -2177,38 +2177,29 @@
       assert
-Equals(false, 
+True(
 flag);%0A    %7D
@@ -2276,18 +2276,16 @@
 Request 
-  
 mockRequ
@@ -3065,32 +3065,142 @@
 t, response));%0A%0A
+        AuthOption authOption = AuthOption.builder().build();%0A        authOption.addUser(%22admin%22, %22123456%22);%0A%0A
         BasicAut
@@ -3257,16 +3257,26 @@
 dleware(
+authOption
 );%0A     
@@ -3375,22 +3375,14 @@
 sert
-Equals(f
+F
 alse
-, 
+(
 flag

update test compile fail