Search in sources :

Example 11 with Response

use of com.blade.mvc.http.Response in project blade by biezhi.

the class BasicAuthMiddlewareTest method testAuthFail.

@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, null));
    AuthOption authOption = AuthOption.builder().build();
    authOption.addUser("admin", "123456");
    BasicAuthMiddleware basicAuthMiddleware = new BasicAuthMiddleware(authOption);
    boolean flag = basicAuthMiddleware.before(context);
    assertFalse(flag);
}
Also used : HttpRequest(com.blade.mvc.http.HttpRequest) BasicAuthMiddleware(com.blade.security.web.auth.BasicAuthMiddleware) WebContext(com.blade.mvc.WebContext) HashMap(java.util.HashMap) HttpRequest(com.blade.mvc.http.HttpRequest) Request(com.blade.mvc.http.Request) RouteContext(com.blade.mvc.RouteContext) Response(com.blade.mvc.http.Response) AuthOption(com.blade.security.web.auth.AuthOption) Test(org.junit.Test)

Example 12 with Response

use of com.blade.mvc.http.Response in project blade by biezhi.

the class BasicAuthMiddlewareTest method testAuthSuccess.

@Test
public void testAuthSuccess() throws Exception {
    Request mockRequest = mockHttpRequest("GET");
    WebContext.init(Blade.of(), "/");
    Map<String, String> headers = new HashMap<>();
    headers.put("Authorization", "Basic YWRtaW46MTIzNDU2");
    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, null));
    AuthOption authOption = AuthOption.builder().build();
    authOption.addUser("admin", "123456");
    BasicAuthMiddleware basicAuthMiddleware = new BasicAuthMiddleware(authOption);
    boolean flag = basicAuthMiddleware.before(context);
    assertTrue(flag);
}
Also used : HttpRequest(com.blade.mvc.http.HttpRequest) BasicAuthMiddleware(com.blade.security.web.auth.BasicAuthMiddleware) WebContext(com.blade.mvc.WebContext) HashMap(java.util.HashMap) HttpRequest(com.blade.mvc.http.HttpRequest) Request(com.blade.mvc.http.Request) RouteContext(com.blade.mvc.RouteContext) Response(com.blade.mvc.http.Response) AuthOption(com.blade.security.web.auth.AuthOption) Test(org.junit.Test)

Aggregations

Response (com.blade.mvc.http.Response)12 Test (org.junit.Test)10 HttpResponse (com.blade.mvc.http.HttpResponse)8 WebContext (com.blade.mvc.WebContext)3 Request (com.blade.mvc.http.Request)3 RouteContext (com.blade.mvc.RouteContext)2 HttpRequest (com.blade.mvc.http.HttpRequest)2 AuthOption (com.blade.security.web.auth.AuthOption)2 BasicAuthMiddleware (com.blade.security.web.auth.BasicAuthMiddleware)2 HashMap (java.util.HashMap)2 Before (org.junit.Before)1