Java Code Examples for org.springframework.mock.web.MockHttpServletRequest#setRemoteAddr()

The following examples show how to use org.springframework.mock.web.MockHttpServletRequest#setRemoteAddr() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CustomAuditEventRepositoryIT.java    From jhipster-online with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddEventWithWebAuthenticationDetails() {
    HttpSession session = new MockHttpSession(null, "test-session-id");
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(session);
    request.setRemoteAddr("1.2.3.4");
    WebAuthenticationDetails details = new WebAuthenticationDetails(request);
    Map<String, Object> data = new HashMap<>();
    data.put("test-key", details);
    AuditEvent event = new AuditEvent("test-user", "test-type", data);
    customAuditEventRepository.add(event);
    List<PersistentAuditEvent> persistentAuditEvents = persistenceAuditEventRepository.findAll();
    assertThat(persistentAuditEvents).hasSize(1);
    PersistentAuditEvent persistentAuditEvent = persistentAuditEvents.get(0);
    assertThat(persistentAuditEvent.getData().get("remoteAddress")).isEqualTo("1.2.3.4");
    assertThat(persistentAuditEvent.getData().get("sessionId")).isEqualTo("test-session-id");
}
 
Example 2
Source File: CustomAuditEventRepositoryIntTest.java    From ehcache3-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddEventWithWebAuthenticationDetails() {
    HttpSession session = new MockHttpSession(null, "test-session-id");
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(session);
    request.setRemoteAddr("1.2.3.4");
    WebAuthenticationDetails details = new WebAuthenticationDetails(request);
    Map<String, Object> data = new HashMap<>();
    data.put("test-key", details);
    AuditEvent event = new AuditEvent("test-user", "test-type", data);
    customAuditEventRepository.add(event);
    List<PersistentAuditEvent> persistentAuditEvents = persistenceAuditEventRepository.findAll();
    assertThat(persistentAuditEvents).hasSize(1);
    PersistentAuditEvent persistentAuditEvent = persistentAuditEvents.get(0);
    assertThat(persistentAuditEvent.getData().get("remoteAddress")).isEqualTo("1.2.3.4");
    assertThat(persistentAuditEvent.getData().get("sessionId")).isEqualTo("test-session-id");
}
 
Example 3
Source File: IpsInterceptorServiceTest.java    From heimdall with Apache License 2.0 6 votes vote down vote up
@Test
   public void blockIpsWithRangeInBlacklist() throws Throwable {
	RequestContext ctx = RequestContext.getCurrentContext();
	MockHttpServletRequest mockHttp = new MockHttpServletRequest();
       mockHttp.addHeader("X-FORWARDED-FOR", "192.168.12.127:3000");
       mockHttp.setRemoteAddr("10.60.40.50");
       
       ctx.setRequest(mockHttp);
	
	Set<String> blacklist = new HashSet<>();
	blacklist.add("192.168.12.0/25");
	
	ipsInterceptorService.executeBlackList(blacklist);
	
	Assert.assertEquals(HttpStatus.UNAUTHORIZED.value(), ctx.getResponse().getStatus());
}
 
Example 4
Source File: CustomAuditEventRepositoryIntegrationTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void testAddEventWithWebAuthenticationDetails() {
    HttpSession session = new MockHttpSession(null, "test-session-id");
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(session);
    request.setRemoteAddr("1.2.3.4");
    WebAuthenticationDetails details = new WebAuthenticationDetails(request);
    Map<String, Object> data = new HashMap<>();
    data.put("test-key", details);
    AuditEvent event = new AuditEvent("test-user", "test-type", data);
    customAuditEventRepository.add(event);
    List<PersistentAuditEvent> persistentAuditEvents = persistenceAuditEventRepository.findAll();
    assertThat(persistentAuditEvents).hasSize(1);
    PersistentAuditEvent persistentAuditEvent = persistentAuditEvents.get(0);
    assertThat(persistentAuditEvent.getData().get("remoteAddress")).isEqualTo("1.2.3.4");
    assertThat(persistentAuditEvent.getData().get("sessionId")).isEqualTo("test-session-id");
}
 
Example 5
Source File: IpsInterceptorServiceTest.java    From heimdall with Apache License 2.0 6 votes vote down vote up
@Test
   public void allowIpsWithPortInWhitelist() throws Throwable {
	RequestContext ctx = RequestContext.getCurrentContext();
	MockHttpServletRequest mockHttp = new MockHttpServletRequest();
       mockHttp.addHeader("X-FORWARDED-FOR", "192.168.12.128:3000");
       mockHttp.setRemoteAddr("10.60.40.50");
       
       ctx.setRequest(mockHttp);
	
	Set<String> whitelist = new HashSet<>();
	whitelist.add("192.168.12.128");
	
	ipsInterceptorService.executeWhiteList(whitelist);
	
	Assert.assertEquals(HttpStatus.OK.value(), ctx.getResponse().getStatus());
}
 
Example 6
Source File: IpsInterceptorServiceTest.java    From heimdall with Apache License 2.0 6 votes vote down vote up
@Test
   public void allowIpsWithRangeInWhitelist() throws Throwable {
	RequestContext ctx = RequestContext.getCurrentContext();
	MockHttpServletRequest mockHttp = new MockHttpServletRequest();
       mockHttp.addHeader("X-FORWARDED-FOR", "10.60.40.50");
       mockHttp.setRemoteAddr("192.168.12.126");
       
       ctx.setRequest(mockHttp);
	
	Set<String> whitelist = new HashSet<>();
	whitelist.add("192.168.12.0/25");
	
	ipsInterceptorService.executeWhiteList(whitelist);
	
	Assert.assertEquals(HttpStatus.OK.value(), ctx.getResponse().getStatus());
}
 
Example 7
Source File: IpsInterceptorServiceTest.java    From heimdall with Apache License 2.0 6 votes vote down vote up
@Test
   public void blockIpsOutOfRangeInWhitelist() throws Throwable {
	RequestContext ctx = RequestContext.getCurrentContext();
	MockHttpServletRequest mockHttp = new MockHttpServletRequest();
       mockHttp.addHeader("X-FORWARDED-FOR", "10.60.40.50");
       mockHttp.setRemoteAddr("192.168.12.128");
       
       ctx.setRequest(mockHttp);
	
	Set<String> whitelist = new HashSet<>();
	whitelist.add("192.168.12.0/25");
	
	ipsInterceptorService.executeWhiteList(whitelist);
	
	Assert.assertEquals(HttpStatus.UNAUTHORIZED.value(), ctx.getResponse().getStatus());
}
 
Example 8
Source File: CustomAuditEventRepositoryIntTest.java    From Full-Stack-Development-with-JHipster with MIT License 6 votes vote down vote up
@Test
public void testAddEventWithWebAuthenticationDetails() {
    HttpSession session = new MockHttpSession(null, "test-session-id");
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(session);
    request.setRemoteAddr("1.2.3.4");
    WebAuthenticationDetails details = new WebAuthenticationDetails(request);
    Map<String, Object> data = new HashMap<>();
    data.put("test-key", details);
    AuditEvent event = new AuditEvent("test-user", "test-type", data);
    customAuditEventRepository.add(event);
    List<PersistentAuditEvent> persistentAuditEvents = persistenceAuditEventRepository.findAll();
    assertThat(persistentAuditEvents).hasSize(1);
    PersistentAuditEvent persistentAuditEvent = persistentAuditEvents.get(0);
    assertThat(persistentAuditEvent.getData().get("remoteAddress")).isEqualTo("1.2.3.4");
    assertThat(persistentAuditEvent.getData().get("sessionId")).isEqualTo("test-session-id");
}
 
Example 9
Source File: CustomAuditEventRepositoryIntTest.java    From e-commerce-microservice with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddEventWithWebAuthenticationDetails() {
    HttpSession session = new MockHttpSession(null, "test-session-id");
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(session);
    request.setRemoteAddr("1.2.3.4");
    WebAuthenticationDetails details = new WebAuthenticationDetails(request);
    Map<String, Object> data = new HashMap<>();
    data.put("test-key", details);
    AuditEvent event = new AuditEvent("test-user", "test-type", data);
    customAuditEventRepository.add(event);
    List<PersistentAuditEvent> persistentAuditEvents = persistenceAuditEventRepository.findAll();
    assertThat(persistentAuditEvents).hasSize(1);
    PersistentAuditEvent persistentAuditEvent = persistentAuditEvents.get(0);
    assertThat(persistentAuditEvent.getData().get("remoteAddress")).isEqualTo("1.2.3.4");
    assertThat(persistentAuditEvent.getData().get("sessionId")).isEqualTo("test-session-id");
}
 
Example 10
Source File: CustomAuditEventRepositoryIntTest.java    From TeamDojo with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddEventWithWebAuthenticationDetails() {
    HttpSession session = new MockHttpSession(null, "test-session-id");
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(session);
    request.setRemoteAddr("1.2.3.4");
    WebAuthenticationDetails details = new WebAuthenticationDetails(request);
    Map<String, Object> data = new HashMap<>();
    data.put("test-key", details);
    AuditEvent event = new AuditEvent("test-user", "test-type", data);
    customAuditEventRepository.add(event);
    List<PersistentAuditEvent> persistentAuditEvents = persistenceAuditEventRepository.findAll();
    assertThat(persistentAuditEvents).hasSize(1);
    PersistentAuditEvent persistentAuditEvent = persistentAuditEvents.get(0);
    assertThat(persistentAuditEvent.getData().get("remoteAddress")).isEqualTo("1.2.3.4");
    assertThat(persistentAuditEvent.getData().get("sessionId")).isEqualTo("test-session-id");
}
 
Example 11
Source File: CustomAuditEventRepositoryIntTest.java    From okta-jhipster-microservices-oauth-example with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddEventWithWebAuthenticationDetails() {
    HttpSession session = new MockHttpSession(null, "test-session-id");
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(session);
    request.setRemoteAddr("1.2.3.4");
    WebAuthenticationDetails details = new WebAuthenticationDetails(request);
    Map<String, Object> data = new HashMap<>();
    data.put("test-key", details);
    AuditEvent event = new AuditEvent("test-user", "test-type", data);
    customAuditEventRepository.add(event);
    List<PersistentAuditEvent> persistentAuditEvents = persistenceAuditEventRepository.findAll();
    assertThat(persistentAuditEvents).hasSize(1);
    PersistentAuditEvent persistentAuditEvent = persistentAuditEvents.get(0);
    assertThat(persistentAuditEvent.getData().get("remoteAddress")).isEqualTo("1.2.3.4");
    assertThat(persistentAuditEvent.getData().get("sessionId")).isEqualTo("test-session-id");
}
 
Example 12
Source File: CustomAuditEventRepositoryIntTest.java    From okta-jhipster-microservices-oauth-example with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddEventWithWebAuthenticationDetails() {
    HttpSession session = new MockHttpSession(null, "test-session-id");
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(session);
    request.setRemoteAddr("1.2.3.4");
    WebAuthenticationDetails details = new WebAuthenticationDetails(request);
    Map<String, Object> data = new HashMap<>();
    data.put("test-key", details);
    AuditEvent event = new AuditEvent("test-user", "test-type", data);
    customAuditEventRepository.add(event);
    List<PersistentAuditEvent> persistentAuditEvents = persistenceAuditEventRepository.findAll();
    assertThat(persistentAuditEvents).hasSize(1);
    PersistentAuditEvent persistentAuditEvent = persistentAuditEvents.get(0);
    assertThat(persistentAuditEvent.getData().get("remoteAddress")).isEqualTo("1.2.3.4");
    assertThat(persistentAuditEvent.getData().get("sessionId")).isEqualTo("test-session-id");
}
 
Example 13
Source File: AllSpnegoKnownClientSystemsFilterActionTest.java    From springboot-shiro-cas-mybatis with MIT License 6 votes vote down vote up
@Test
public void ensureRemoteIpShouldNotBeChecked() {
    final BaseSpnegoKnownClientSystemsFilterAction action =
            new BaseSpnegoKnownClientSystemsFilterAction("^192\\.158\\..+");

    final MockRequestContext ctx = new MockRequestContext();
    final MockHttpServletRequest req = new MockHttpServletRequest();
    req.setRemoteAddr("193.158.5.781");
    final ServletExternalContext extCtx = new ServletExternalContext(
            new MockServletContext(), req,
            new MockHttpServletResponse());
    ctx.setExternalContext(extCtx);

    final Event ev = action.doExecute(ctx);
    assertNotEquals(ev.getId(), new EventFactorySupport().yes(this).getId());
}
 
Example 14
Source File: HttpMockServiceImpl.java    From AnyMock with Apache License 2.0 6 votes vote down vote up
private MockHttpServletRequest buildMockRequest(HttpServletRequest request) {
    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    mockRequest.setParameters(request.getParameterMap());

    Enumeration headerNames = request.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        String key = (String)headerNames.nextElement();
        mockRequest.addHeader(key, request.getHeader(key));
    }

    mockRequest.setMethod(request.getMethod());
    mockRequest.setRequestURI(request.getRequestURI());
    mockRequest.setRemoteAddr(request.getRemoteAddr());
    mockRequest.setRemoteHost(request.getRemoteHost());
    mockRequest.setRemotePort(request.getRemotePort());
    mockRequest.setRemoteUser(request.getRemoteUser());
    mockRequest.setAttribute(BODY, request.getAttribute(BODY));
    return mockRequest;
}
 
Example 15
Source File: InspektrThrottledSubmissionByIpAddressAndUsernameHandlerInterceptorAdapterTests.java    From springboot-shiro-cas-mybatis with MIT License 6 votes vote down vote up
@Override
protected MockHttpServletResponse loginUnsuccessfully(final String username, final String fromAddress)
        throws Exception {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    request.setMethod("POST");
    request.setParameter("username", username);
    request.setRemoteAddr(fromAddress);
    final MockRequestContext context = new MockRequestContext();
    context.setCurrentEvent(new Event("", "error"));
    request.setAttribute("flowRequestContext", context);
    ClientInfoHolder.setClientInfo(new ClientInfo(request));

    getThrottle().preHandle(request, response, null);

    try {
        authenticationManager.authenticate(badCredentials(username));
    } catch (final AuthenticationException e) {
        getThrottle().postHandle(request, response, null, null);
        return response;
    }
    fail("Expected AuthenticationException");
    return null;
}
 
Example 16
Source File: AllSpnegoKnownClientSystemsFilterActionTest.java    From springboot-shiro-cas-mybatis with MIT License 6 votes vote down vote up
@Test
public void ensureHostnameShouldDoSpnego() {
    final HostNameSpnegoKnownClientSystemsFilterAction action =
            new HostNameSpnegoKnownClientSystemsFilterAction("\\w+\\.\\w+\\.\\w+");

    final MockRequestContext ctx = new MockRequestContext();
    final MockHttpServletRequest req = new MockHttpServletRequest();
    req.setRemoteAddr("74.125.136.102");
    final ServletExternalContext extCtx = new ServletExternalContext(
            new MockServletContext(), req,
            new MockHttpServletResponse());
    ctx.setExternalContext(extCtx);

    final Event ev = action.doExecute(ctx);
    assertEquals(ev.getId(), new EventFactorySupport().yes(this).getId());

}
 
Example 17
Source File: AbstractInMemoryThrottledSubmissionHandlerInterceptorAdapterTests.java    From springboot-shiro-cas-mybatis with MIT License 5 votes vote down vote up
@Override
protected MockHttpServletResponse loginUnsuccessfully(final String username, final String fromAddress) throws Exception {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    request.setMethod("POST");
    request.setParameter("username", username);
    request.setRemoteAddr(fromAddress);
    final MockRequestContext context = new MockRequestContext();
    context.setCurrentEvent(new Event("", "error"));
    request.setAttribute("flowRequestContext", context);
    getThrottle().preHandle(request, response, null);
    getThrottle().postHandle(request, response, null, null);
    return response;
}
 
Example 18
Source File: UploadInfoTest.java    From tus-java-server with MIT License 5 votes vote down vote up
@Test
public void testGetCreatorIpAddressesWithoutXForwardedFor() throws Exception {
    MockHttpServletRequest servletRequest = new MockHttpServletRequest();
    servletRequest.setRemoteAddr("10.11.12.13");

    UploadInfo info = new UploadInfo(servletRequest);
    assertThat(info.getCreatorIpAddresses(), is("10.11.12.13"));
}
 
Example 19
Source File: DefaultCasCookieValueManagerTests.java    From springboot-shiro-cas-mybatis with MIT License 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void defaultCookieWithNoRemote() {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRemoteAddr(null);
    final DefaultCasCookieValueManager mgmr = new DefaultCasCookieValueManager(new DefaultCipherExecutor(ENC_KEY, SIGN_KEY));
    mgmr.buildCookieValue("cas", request);
}
 
Example 20
Source File: RequestContextTest.java    From arctic-sea with Apache License 2.0 5 votes vote down vote up
@Test
public void ip6to4Addresses() {
    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    mockRequest.setRemoteAddr("2002:836b:1714::836b:1714");
    OwsServiceRequestContext fromRequest = OwsServiceRequestContext.fromRequest(mockRequest);
    assertEquals("131.107.23.20", fromRequest.getIPAddress().get().toString());
}