Search in sources :

Example 21 with Request

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

the class RouteActionArguments method getRouteActionParameters.

public static Object[] getRouteActionParameters(RouteContext context) {
    Method actionMethod = context.routeAction();
    Request request = context.request();
    actionMethod.setAccessible(true);
    Parameter[] parameters = actionMethod.getParameters();
    Object[] args = new Object[parameters.length];
    String[] parameterNames = ASMUtils.findMethodParmeterNames(actionMethod);
    for (int i = 0, len = parameters.length; i < len; i++) {
        Parameter parameter = parameters[i];
        String paramName = Objects.requireNonNull(parameterNames)[i];
        Type argType = parameter.getParameterizedType();
        if (containsAnnotation(parameter)) {
            args[i] = getAnnotationParam(parameter, paramName, request);
            continue;
        }
        if (ReflectKit.isBasicType(argType)) {
            args[i] = request.query(paramName);
            continue;
        }
        args[i] = getCustomType(parameter, paramName, context);
    }
    return args;
}
Also used : Request(com.blade.mvc.http.Request)

Example 22 with Request

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

the class ExceptionHandlerTest method before.

@Before
public void before() {
    request = mock(Request.class);
    when(request.header("Accept")).thenReturn("text/html");
    response = mock(Response.class);
    WebContext.init(Blade.me(), "/");
    WebContext.set(new WebContext(request, response, null));
}
Also used : Response(com.blade.mvc.http.Response) WebContext(com.blade.mvc.WebContext) Request(com.blade.mvc.http.Request) Before(org.junit.Before)

Example 23 with Request

use of com.blade.mvc.http.Request 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 24 with Request

use of com.blade.mvc.http.Request 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)

Example 25 with Request

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

the class HttpRequestTest method testFileItems.

@Test
public void testFileItems() {
    Request mockRequest = mockHttpRequest("GET");
    Map<String, FileItem> attr = new HashMap<>();
    FileItem fileItem = new FileItem();
    fileItem.setName("file");
    fileItem.setFileName("hello.png");
    fileItem.setPath("/usr/hello.png");
    fileItem.setContentType("image/png");
    fileItem.setLength(20445L);
    attr.put("img", fileItem);
    when(mockRequest.fileItems()).thenReturn(attr);
    Request request = new HttpRequest(mockRequest);
    FileItem img = request.fileItem("img").get();
    assertNotNull(img);
    assertNull(img.getFile());
    assertEquals("file", img.getName());
    assertEquals("hello.png", img.getFileName());
    assertEquals("/usr/hello.png", img.getPath());
    assertEquals(Long.valueOf(20445), Optional.of(img.getLength()).get());
    assertEquals("image/png", img.getContentType());
}
Also used : HttpRequest(com.blade.mvc.http.HttpRequest) FileItem(com.blade.mvc.multipart.FileItem) HashMap(java.util.HashMap) CaseInsensitiveHashMap(com.blade.kit.CaseInsensitiveHashMap) HttpRequest(com.blade.mvc.http.HttpRequest) Request(com.blade.mvc.http.Request) Test(org.junit.Test)

Aggregations

Request (com.blade.mvc.http.Request)29 HttpRequest (com.blade.mvc.http.HttpRequest)18 Test (org.junit.Test)18 HashMap (java.util.HashMap)7 CaseInsensitiveHashMap (com.blade.kit.CaseInsensitiveHashMap)5 WebContext (com.blade.mvc.WebContext)3 Response (com.blade.mvc.http.Response)3 RouteContext (com.blade.mvc.RouteContext)2 AuthOption (com.blade.security.web.auth.AuthOption)2 BasicAuthMiddleware (com.blade.security.web.auth.BasicAuthMiddleware)2 ForbiddenException (com.blade.exception.ForbiddenException)1 NotFoundException (com.blade.exception.NotFoundException)1 TemplateException (com.blade.exception.TemplateException)1 Session (com.blade.mvc.http.Session)1 FileItem (com.blade.mvc.multipart.FileItem)1 Contents (com.tale.model.entity.Contents)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 DefaultFileRegion (io.netty.channel.DefaultFileRegion)1 SslHandler (io.netty.handler.ssl.SslHandler)1