Search in sources :

Example 6 with HttpRequest

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

the class HttpRequestTest method testAttribute.

@Test
public void testAttribute() {
    Request mockRequest = mockHttpRequest("GET");
    Map<String, Object> attr = new HashMap<>();
    attr.put("name", "biezhi");
    when(mockRequest.attributes()).thenReturn(attr);
    Request request = new HttpRequest(mockRequest);
    assertEquals("biezhi", request.attribute("name"));
}
Also used : HttpRequest(com.blade.mvc.http.HttpRequest) 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)

Example 7 with HttpRequest

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

the class MergeRequestHandler method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) {
    if (msg instanceof io.netty.handler.codec.http.HttpRequest) {
        httpRequest = new HttpRequest();
        httpRequest.setNettyRequest((io.netty.handler.codec.http.HttpRequest) msg);
        return;
    }
    if (null != httpRequest && msg instanceof HttpContent) {
        httpRequest.appendContent((HttpContent) msg);
    }
    if (msg instanceof LastHttpContent) {
        if (null != httpRequest) {
            ctx.fireChannelRead(httpRequest);
        } else {
            ctx.fireChannelRead(msg);
        }
    }
}
Also used : HttpRequest(com.blade.mvc.http.HttpRequest) io.netty.handler.codec.http(io.netty.handler.codec.http)

Example 8 with HttpRequest

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

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

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

HttpRequest (com.blade.mvc.http.HttpRequest)15 Test (org.junit.Test)14 Request (com.blade.mvc.http.Request)12 HashMap (java.util.HashMap)7 CaseInsensitiveHashMap (com.blade.kit.CaseInsensitiveHashMap)6 RouteContext (com.blade.mvc.RouteContext)2 WebContext (com.blade.mvc.WebContext)2 Response (com.blade.mvc.http.Response)2 AuthOption (com.blade.security.web.auth.AuthOption)2 BasicAuthMiddleware (com.blade.security.web.auth.BasicAuthMiddleware)2 Cookie (com.blade.mvc.http.Cookie)1 FileItem (com.blade.mvc.multipart.FileItem)1 io.netty.handler.codec.http (io.netty.handler.codec.http)1 List (java.util.List)1