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

The following examples show how to use org.springframework.mock.web.MockHttpServletRequest#setSession() . 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: CustomAuditEventRepositoryIntTest.java    From Spring-5.0-Projects 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 2
Source File: OAuth20CallbackAuthorizeControllerTests.java    From cas4.0.x-server-wechat with Apache License 2.0 6 votes vote down vote up
@Test
public void testOK() throws Exception {
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest(
            "GET",
            CONTEXT
            + OAuthConstants.CALLBACK_AUTHORIZE_URL);
    mockRequest.addParameter(OAuthConstants.TICKET, SERVICE_TICKET);
    final MockHttpSession mockSession = new MockHttpSession();
    mockSession.putValue(OAuthConstants.OAUTH20_CALLBACKURL, REDIRECT_URI);
    mockSession.putValue(OAuthConstants.OAUTH20_SERVICE_NAME, SERVICE_NAME);
    mockRequest.setSession(mockSession);
    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();
    final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
    oauth20WrapperController.afterPropertiesSet();
    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertEquals(OAuthConstants.CONFIRM_VIEW, modelAndView.getViewName());
    final Map<String, Object> map = modelAndView.getModel();
    assertEquals(SERVICE_NAME, map.get("serviceName"));
    assertEquals(REDIRECT_URI + "?" + OAuthConstants.CODE + "=" + SERVICE_TICKET, map.get("callbackUrl"));
}
 
Example 3
Source File: CustomAuditEventRepositoryIT.java    From alchemy 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 4
Source File: CustomAuditEventRepositoryIntTest.java    From jhipster-microservices-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 5
Source File: CustomAuditEventRepositoryIT.java    From java-microservices-examples 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 6
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 7
Source File: TestAddCobar.java    From tddl5 with Apache License 2.0 6 votes vote down vote up
public void testAddCobar() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    MockHttpSession session = new MockHttpSession();
    UserDO user = new UserDO();
    user.setStatus(ConstantDefine.NORMAL);
    user.setUser_role(ConstantDefine.CLUSTER_ADMIN);
    session.setAttribute("user", user);
    request.setSession(session);
    request.addParameter("clusterId", "1");
    request.addParameter("host", "1.2.4.3");
    request.addParameter("cobarName", "test");
    request.addParameter("port", "8066");
    request.addParameter("userName", "test");
    request.addParameter("password", "TTT");
    request.addParameter("status", "ACTIVE");

    ModelAndView mav = addcobar.handleRequest(request, new MockHttpServletResponse());
    Assert.assertEquals("add cobar success", String.valueOf(mav.getModel().get("info")));
}
 
Example 8
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 9
Source File: HttpSessionChallengeRepositoryTest.java    From webauthn4j-spring-security with Apache License 2.0 6 votes vote down vote up
@Test
public void loadChallenge_test() {
    MockHttpSession session = new MockHttpSession();
    MockHttpServletRequest prevRequest = new MockHttpServletRequest();
    prevRequest.setSession(session);

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(session);
    String attrName = ".test-challenge";

    target.setSessionAttributeName(attrName);
    Challenge challenge = target.generateChallenge();
    target.saveChallenge(challenge, prevRequest);
    Challenge loadedChallenge = target.loadChallenge(request);

    assertThat(loadedChallenge).isEqualTo(challenge);
}
 
Example 10
Source File: ApiCatalogLogoutSuccessHandlerTest.java    From api-layer with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testOnLogoutSuccess() {
    MockHttpServletRequest httpServletRequest = new MockHttpServletRequest();
    MockHttpSession mockHttpSession = new MockHttpSession();
    httpServletRequest.setSession(mockHttpSession);

    MockHttpServletResponse httpServletResponse = new MockHttpServletResponse();

    AuthConfigurationProperties securityConfigurationProperties = new AuthConfigurationProperties();
    ApiCatalogLogoutSuccessHandler apiCatalogLogoutSuccessHandler = new ApiCatalogLogoutSuccessHandler(securityConfigurationProperties);

    apiCatalogLogoutSuccessHandler.onLogoutSuccess(
        httpServletRequest,
        httpServletResponse,
        new TokenAuthentication("TEST_TOKEN_STRING")
    );

    assertTrue(mockHttpSession.isInvalid());
    assertEquals(HttpStatus.OK.value(), httpServletResponse.getStatus());

    Cookie cookie = httpServletResponse.getCookie(
        securityConfigurationProperties.getCookieProperties().getCookieName());
    assertNotNull(cookie);
    assertTrue(cookie.getSecure());
    assertTrue(cookie.isHttpOnly());
}
 
Example 11
Source File: OAuth20CallbackAuthorizeControllerTests.java    From springboot-shiro-cas-mybatis with MIT License 6 votes vote down vote up
@Test
public void verifyOK() throws Exception {
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest(
            "GET",
            CONTEXT
            + OAuthConstants.CALLBACK_AUTHORIZE_URL);
    mockRequest.addParameter(OAuthConstants.TICKET, SERVICE_TICKET);
    final MockHttpSession mockSession = new MockHttpSession();
    mockSession.putValue(OAuthConstants.OAUTH20_CALLBACKURL, REDIRECT_URI);
    mockSession.putValue(OAuthConstants.OAUTH20_SERVICE_NAME, SERVICE_NAME);
    mockRequest.setSession(mockSession);
    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();
    final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
    oauth20WrapperController.afterPropertiesSet();
    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertEquals(OAuthConstants.CONFIRM_VIEW, modelAndView.getViewName());
    final Map<String, Object> map = modelAndView.getModel();
    assertEquals(SERVICE_NAME, map.get("serviceName"));
    assertEquals(REDIRECT_URI + "?" + OAuthConstants.CODE + "=" + SERVICE_TICKET, map.get("callbackUrl"));
}
 
Example 12
Source File: ClassPathBeanDefinitionScannerScopeIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void setUp() {
	MockHttpServletRequest oldRequestWithSession = new MockHttpServletRequest();
	oldRequestWithSession.setSession(new MockHttpSession());
	this.oldRequestAttributesWithSession = new ServletRequestAttributes(oldRequestWithSession);

	MockHttpServletRequest newRequestWithSession = new MockHttpServletRequest();
	newRequestWithSession.setSession(new MockHttpSession());
	this.newRequestAttributesWithSession = new ServletRequestAttributes(newRequestWithSession);
}
 
Example 13
Source File: ClientActionTests.java    From cas4.0.x-server-wechat with Apache License 2.0 5 votes vote down vote up
@Test
public void testFinishAuthentication() throws Exception {
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    mockRequest.setParameter(Clients.DEFAULT_CLIENT_NAME_PARAMETER, "FacebookClient");

    final MockHttpSession mockSession = new MockHttpSession();
    mockSession.setAttribute(ClientAction.THEME, MY_THEME);
    mockSession.setAttribute(ClientAction.LOCALE, MY_LOCALE);
    mockSession.setAttribute(ClientAction.METHOD, MY_METHOD);
    final Service service = new SimpleWebApplicationServiceImpl(MY_SERVICE);
    mockSession.setAttribute(ClientAction.SERVICE, service);
    mockRequest.setSession(mockSession);

    final ServletExternalContext servletExternalContext = mock(ServletExternalContext.class);
    when(servletExternalContext.getNativeRequest()).thenReturn(mockRequest);

    final MockRequestContext mockRequestContext = new MockRequestContext();
    mockRequestContext.setExternalContext(servletExternalContext);

    final FacebookClient facebookClient = new MockFacebookClient();
    final Clients clients = new Clients(MY_LOGIN_URL, facebookClient);

    final ClientAction action = new ClientAction(mock(CentralAuthenticationService.class), clients);
    final Event event = action.execute(mockRequestContext);
    assertEquals("success", event.getId());
    assertEquals(MY_THEME, mockRequest.getAttribute(ClientAction.THEME));
    assertEquals(MY_LOCALE, mockRequest.getAttribute(ClientAction.LOCALE));
    assertEquals(MY_METHOD, mockRequest.getAttribute(ClientAction.METHOD));
    final MutableAttributeMap flowScope = mockRequestContext.getFlowScope();
    assertEquals(service, flowScope.get(ClientAction.SERVICE));
}
 
Example 14
Source File: AopNamespaceHandlerScopeIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testSessionScoping() throws Exception {
	MockHttpSession oldSession = new MockHttpSession();
	MockHttpSession newSession = new MockHttpSession();

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setSession(oldSession);
	RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));

	ITestBean scoped = (ITestBean) this.context.getBean("sessionScoped");
	assertTrue("Should be AOP proxy", AopUtils.isAopProxy(scoped));
	assertFalse("Should not be target class proxy", scoped instanceof TestBean);

	ITestBean scopedAlias = (ITestBean) this.context.getBean("sessionScopedAlias");
	assertSame(scoped, scopedAlias);

	ITestBean testBean = (ITestBean) this.context.getBean("testBean");
	assertTrue("Should be AOP proxy", AopUtils.isAopProxy(testBean));
	assertFalse("Regular bean should be JDK proxy", testBean instanceof TestBean);

	String rob = "Rob Harrop";
	String bram = "Bram Smeets";

	assertEquals(rob, scoped.getName());
	scoped.setName(bram);
	request.setSession(newSession);
	assertEquals(rob, scoped.getName());
	request.setSession(oldSession);
	assertEquals(bram, scoped.getName());

	assertTrue("Should have advisors", ((Advised) scoped).getAdvisors().length > 0);
}
 
Example 15
Source File: BaseTest.java    From ranger with Apache License 2.0 5 votes vote down vote up
protected MockHttpServletRequest startRequest() {
	request = new MockHttpServletRequest();
	request.setSession(session);
	RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(
			request));
	return request;
}
 
Example 16
Source File: ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void setUp() {
	this.oldRequestAttributes = new ServletRequestAttributes(new MockHttpServletRequest());
	this.newRequestAttributes = new ServletRequestAttributes(new MockHttpServletRequest());

	MockHttpServletRequest oldRequestWithSession = new MockHttpServletRequest();
	oldRequestWithSession.setSession(new MockHttpSession());
	this.oldRequestAttributesWithSession = new ServletRequestAttributes(oldRequestWithSession);

	MockHttpServletRequest newRequestWithSession = new MockHttpServletRequest();
	newRequestWithSession.setSession(new MockHttpSession());
	this.newRequestAttributesWithSession = new ServletRequestAttributes(newRequestWithSession);
}
 
Example 17
Source File: RequestLoggingFilterTest.java    From herd with Apache License 2.0 5 votes vote down vote up
private MockHttpServletRequest createServletRequest()
{
    MockHttpServletRequest request = new MockHttpServletRequest(null, "/test");
    request.setQueryString("param=value");
    request.setMethod("POST");
    MockHttpSession session = new MockHttpSession();
    request.setContent(PAYLOAD_CONTENT.getBytes());
    request.setSession(session);
    request.setRemoteUser("Test Remote User");
    return request;
}
 
Example 18
Source File: ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
	this.oldRequestAttributes = new ServletRequestAttributes(new MockHttpServletRequest());
	this.newRequestAttributes = new ServletRequestAttributes(new MockHttpServletRequest());

	MockHttpServletRequest oldRequestWithSession = new MockHttpServletRequest();
	oldRequestWithSession.setSession(new MockHttpSession());
	this.oldRequestAttributesWithSession = new ServletRequestAttributes(oldRequestWithSession);

	MockHttpServletRequest newRequestWithSession = new MockHttpServletRequest();
	newRequestWithSession.setSession(new MockHttpSession());
	this.newRequestAttributesWithSession = new ServletRequestAttributes(newRequestWithSession);
}
 
Example 19
Source File: AopNamespaceHandlerScopeIntegrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testSessionScoping() throws Exception {
	MockHttpSession oldSession = new MockHttpSession();
	MockHttpSession newSession = new MockHttpSession();

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setSession(oldSession);
	RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));

	ITestBean scoped = (ITestBean) this.context.getBean("sessionScoped");
	assertTrue("Should be AOP proxy", AopUtils.isAopProxy(scoped));
	assertFalse("Should not be target class proxy", scoped instanceof TestBean);

	ITestBean scopedAlias = (ITestBean) this.context.getBean("sessionScopedAlias");
	assertSame(scoped, scopedAlias);

	ITestBean testBean = (ITestBean) this.context.getBean("testBean");
	assertTrue("Should be AOP proxy", AopUtils.isAopProxy(testBean));
	assertFalse("Regular bean should be JDK proxy", testBean instanceof TestBean);

	String rob = "Rob Harrop";
	String bram = "Bram Smeets";

	assertEquals(rob, scoped.getName());
	scoped.setName(bram);
	request.setSession(newSession);
	assertEquals(rob, scoped.getName());
	request.setSession(oldSession);
	assertEquals(bram, scoped.getName());

	assertTrue("Should have advisors", ((Advised) scoped).getAdvisors().length > 0);
}
 
Example 20
Source File: ClassPathBeanDefinitionScannerScopeIntegrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Before
public void setUp() {
	MockHttpServletRequest oldRequestWithSession = new MockHttpServletRequest();
	oldRequestWithSession.setSession(new MockHttpSession());
	this.oldRequestAttributesWithSession = new ServletRequestAttributes(oldRequestWithSession);

	MockHttpServletRequest newRequestWithSession = new MockHttpServletRequest();
	newRequestWithSession.setSession(new MockHttpSession());
	this.newRequestAttributesWithSession = new ServletRequestAttributes(newRequestWithSession);
}