提交 b1d376f9 编写于 作者: lakernote's avatar lakernote

controller层单元测试

上级 0c22dd7f
......@@ -28,6 +28,7 @@ import java.io.IOException;
* @author laker
*/
@Configuration
@ConditionalOnProperty(name = "druid.monitor", havingValue = "true", matchIfMissing = true)
@Slf4j
public class DruidConfig {
private static final String DEFAULT_ALLOW_IP = "127.0.0.1";
......
......@@ -58,8 +58,10 @@ public class MetricsAspect {
logBean.setUri(HttpServletRequestUtil.getRequestURI());
logBean.setUserId(StpUtil.isLogin() ? StpUtil.getLoginIdAsLong() : null);
UserAgent userAgent = HttpServletRequestUtil.getRequestUserAgent();
String client = userAgent.getOs().getName() + "." + userAgent.getBrowser().getName();
logBean.setClient(client);
if (userAgent != null) {
String client = userAgent.getOs().getName() + "." + userAgent.getBrowser().getName();
logBean.setClient(client);
}
logBean.setRequest(objectMapper.writeValueAsString(pjp.getArgs()));
logBean.setMethod(name);
logBean.setStatus(true);
......
......@@ -41,6 +41,9 @@ public class MySaTokenListener implements SaTokenListener {
@Override
public void doLogin(String loginType, Object loginId, SaLoginModel loginModel) {
UserAgent requestUserAgent = HttpServletRequestUtil.getRequestUserAgent();
if (requestUserAgent == null) {
return;
}
String cityInfo = IP2CityUtil.getCityInfo(HttpServletRequestUtil.getRemoteIP());
String[] split = cityInfo.split("\\|");
ONLINE_USERS.add(OnlineUser.builder()
......
......@@ -48,7 +48,7 @@ public class WafFilter implements Filter {
//Request请求过滤
chain.doFilter(new WafRequestWrapper(req, xssEnabled, sqlEnabled), response);
} catch (Exception e) {
log.error(" WafFilter exception , requestURL: " + req.getRequestURL());
log.error(" WafFilter exception , requestURL: " + req.getRequestURL(), e);
}
return;
}
......
knife4j:
# 是否生产环境 屏蔽所有资源接口
production: true
druid:
monitor: false
\ No newline at end of file
package com.laker.admin.module.sys.controller;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.laker.admin.framework.cache.ICache;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import static org.hamcrest.Matchers.containsString;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* 启动了完整的 Spring 应用程序上下文,但没有服务器。
*/
@ActiveProfiles("test")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
@AutoConfigureMockMvc
public class SysUserController_APITest {
/**
* 会走 filter controller,这个比较仿真
*/
@Autowired
private MockMvc mvc;
@MockBean
ICache iCache;
/**
* 不需要user的测试
*
* @throws Exception
*/
@Test
public void contextLoads() throws Exception {
mvc.perform(get("/captcha"))
.andDo(print())
.andExpect(status().isOk()).andExpect(content().string(containsString("data:image/png;")));
}
/**
* 需要user的测试
*
* @throws Exception
*/
@Test
public void test2() throws Exception {
StpUtil.login("1");
SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
mvc.perform(get("/sys/user/getAll").header(tokenInfo.getTokenName(), tokenInfo.getTokenValue()))
.andDo(print())
.andExpect(status().isOk());
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册