I should try to test the follow WebFilter with Mockito
Class ValidIpFilter
@Component
@WebFilter
@NoArgsConstructor
@Slf4j
public class ValidIpFilter extends GenericFilterBean {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterchain)
throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
String ctxtPath = httpRequest.getContextPath();
if (!verify(IPWITHSUBNET, httpRequest.getRemoteAddr())) {
httpResponse.sendRedirect(ctxtPath.concat("/redirect"));
}
filterchain.doFilter(request, response);
}
private static boolean isIpFound(String ip) {
boolean b = false;
if (Objects.nonNull(ip)) {
IPAddressString s = new IPAddressString(ip);
b = s.isIPAddress();
}
return b;
}
private boolean verify(String ip, String ipRequest) {
if (isIpFound(ip) && isIpFound(ipRequest)) {
try {
IPAddress ipAddress = new IPAddressString(ip).toAddress();
IPAddress ipRequestAddress = new IPAddressString(ipRequest).toAddress();
return ipAddress.contains(ipRequestAddress);
} catch (AddressStringException e) {
log.error("Error AddressStringException {}", e);
} catch (IncompatibleAddressException e) {
log.error("Error IncompatibleAddressException {}", e);
}
}
return false;
}
}
Class ValidIpFilterTest
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
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.status;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import javax.servlet.FilterChain;
import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.asm.Attribute;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.mock.web.MockFilterChain;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
@Import({ValidIpFilter.class,MyAbbonamentiServiceClient.class})
public class ValidIpFilterTest extends TestTemplate {
@Autowired
private BeanFactory beanFactory;
@InjectMocks
private ValidIpFilter validIpFilter;
@Rule
public ExpectedException expectedEx = ExpectedException.none();
@Autowired
private MyController myController;
private MockMvc mockMvc;
private static final String CONTEXT_OPEN_SESSION = "/";
@MockBean
private MyServiceService myService;
@Before
public void setup() throws GeneralSecurityException, IOException {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.standaloneSetup(myController).build();
}
@Test
public void testFilter_LocalhostResponseRedirect_OK() throws Exception {
MockHttpServletRequest request = setRequest("192.168.0.12");
MockHttpServletResponse response = new MockHttpServletResponse();
MockFilterChain filterChain = new MockFilterChain();
validIpFilter.doFilter(request, response, filterChain);
assertEquals(302, response.getStatus());
assertEquals("/ipfilter/redirect", response.getHeader("Location"));
}
@Test
public void testFilter() throws Exception {
when(myService.
methodWithParam("XXXXXXXXX")
)
.thenReturn(new ArrayList<>());
MockHttpServletRequestBuilder request = get(CONTEXT_OPEN_SESSION);
request.header("connection", "keep-alive");
request.header("pragma", "no-cache");
request.header("cache-control", "no-cache");
request.header("save-data", "on");
request.header("user-agent",
"Mozilla/5.0 (Linux; Android 9; SM-G950F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.105 Mobile Safari/537.36");
request.header("dnt", 1);
request.header("accept", "image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8");
request.header("accept-encoding", "gzip, deflate");
request.header("accept-language", "it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7");
request.header("Origin", "*");
request.param("remoteAddress", "192.168.0.12");
MockHttpServletResponse response = new MockHttpServletResponse();
MockFilterChain filterChain = new MockFilterChain();
ServletContext co = this.mockMvc.getDispatcherServlet().getServletContext();
validIpFilter.doFilter(request.buildRequest(co), response, filterChain);
this.mockMvc.perform(request).andDo(print()). andExpect(status().isOk());
}
}
The first Test work correctly return status 302 and the redirect at the page, but the second test(is similar) with use the MockMvc non return the same result. I suppose there is the MockHttpServletRequestBuilder and the perform is not correct, can you help me understand?
Best Regards
Alessandro
Aucun commentaire:
Enregistrer un commentaire