use of com.blade.mvc.WebContext 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.WebContext 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.WebContext 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);
}
Aggregations