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