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"));
}
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);
}
}
}
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);
}
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);
}
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());
}
Aggregations