@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)); }
@Override public void start(Blade blade) throws Exception { this.blade = blade; this.environment = blade.environment(); this.processors = blade.processors(); this.loaders = blade.loaders(); long startMs = System.currentTimeMillis(); int padSize = 16; log.info("{} {}{}", StringKit.padRight("app.env", padSize), getPrefixSymbol(), environment.get(ENV_KEY_APP_ENV, "default")); log.info("{} {}{}", StringKit.padRight("app.pid", padSize), getPrefixSymbol(), BladeKit.getPID()); log.info("{} {}{}", StringKit.padRight("app.devMode", padSize), getPrefixSymbol(), blade.devMode()); log.info("{} {}{}", StringKit.padRight("jdk.version", padSize), getPrefixSymbol(), System.getProperty("java.version")); log.info("{} {}{}", StringKit.padRight("user.dir", padSize), getPrefixSymbol(), System.getProperty("user.dir")); log.info("{} {}{}", StringKit.padRight("java.io.tmpdir", padSize), getPrefixSymbol(), System.getProperty("java.io.tmpdir")); log.info("{} {}{}", StringKit.padRight("user.timezone", padSize), getPrefixSymbol(), System.getProperty("user.timezone")); log.info("{} {}{}", StringKit.padRight("file.encoding", padSize), getPrefixSymbol(), System.getProperty("file.encoding")); log.info("{} {}{}", StringKit.padRight("app.classpath", padSize), getPrefixSymbol(), CLASSPATH); this.initConfig(); String contextPath = environment.get(ENV_KEY_CONTEXT_PATH, "/"); WebContext.init(blade, contextPath); this.initIoc(); this.watchEnv(); this.startServer(startMs); this.sessionCleaner(); this.startTask(); this.shutdownHook(); }
@Override public void start(Blade blade) throws Exception { this.blade = blade; this.environment = blade.environment(); this.processors = blade.processors(); this.loaders = blade.loaders(); long startMs = System.currentTimeMillis(); int padSize = 16; log.info("{} {}{}", StringKit.padRight("app.env", padSize), getPrefixSymbol(), environment.get(ENV_KEY_APP_ENV, "default")); log.info("{} {}{}", StringKit.padRight("app.pid", padSize), getPrefixSymbol(), BladeKit.getPID()); log.info("{} {}{}", StringKit.padRight("app.devMode", padSize), getPrefixSymbol(), blade.devMode()); log.info("{} {}{}", StringKit.padRight("jdk.version", padSize), getPrefixSymbol(), System.getProperty("java.version")); log.info("{} {}{}", StringKit.padRight("user.dir", padSize), getPrefixSymbol(), System.getProperty("user.dir")); log.info("{} {}{}", StringKit.padRight("java.io.tmpdir", padSize), getPrefixSymbol(), System.getProperty("java.io.tmpdir")); log.info("{} {}{}", StringKit.padRight("user.timezone", padSize), getPrefixSymbol(), System.getProperty("user.timezone")); log.info("{} {}{}", StringKit.padRight("file.encoding", padSize), getPrefixSymbol(), System.getProperty("file.encoding")); log.info("{} {}{}", StringKit.padRight("app.classpath", padSize), getPrefixSymbol(), CLASSPATH); this.initConfig(); String contextPath = environment.get(ENV_KEY_CONTEXT_PATH, "/"); WebContext.init(blade, contextPath); this.initIoc(); this.watchEnv(); this.startServer(startMs); this.sessionCleaner(); this.startTask(); this.shutdownHook(); }
@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); }
@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); }
@Override public void start(Blade blade) throws Exception { this.blade = blade; this.environment = blade.environment(); this.processors = blade.processors(); this.loaders = blade.loaders(); long startMs = System.currentTimeMillis(); int padSize = 16; log.info("{} {}{}", StringKit.padRight("app.env", padSize), getPrefixSymbol(), environment.get(ENV_KEY_APP_ENV, "default")); log.info("{} {}{}", StringKit.padRight("app.pid", padSize), getPrefixSymbol(), BladeKit.getPID()); log.info("{} {}{}", StringKit.padRight("app.devMode", padSize), getPrefixSymbol(), blade.devMode()); log.info("{} {}{}", StringKit.padRight("jdk.version", padSize), getPrefixSymbol(), System.getProperty("java.version")); log.info("{} {}{}", StringKit.padRight("user.dir", padSize), getPrefixSymbol(), System.getProperty("user.dir")); log.info("{} {}{}", StringKit.padRight("java.io.tmpdir", padSize), getPrefixSymbol(), System.getProperty("java.io.tmpdir")); log.info("{} {}{}", StringKit.padRight("user.timezone", padSize), getPrefixSymbol(), System.getProperty("user.timezone")); log.info("{} {}{}", StringKit.padRight("file.encoding", padSize), getPrefixSymbol(), System.getProperty("file.encoding")); log.info("{} {}{}", StringKit.padRight("app.classpath", padSize), getPrefixSymbol(), CLASSPATH); this.initConfig(); String contextPath = environment.get(ENV_KEY_CONTEXT_PATH, "/"); WebContext.init(blade, contextPath); this.initIoc(); this.watchEnv(); this.startServer(startMs); this.sessionCleaner(); this.startTask(); this.shutdownHook(); }