List of usage examples for org.springframework.mock.web MockHttpServletRequest setRequestURI
public void setRequestURI(@Nullable String requestURI)
From source file:org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilterTests.java
private MockHttpServletRequest createMockAuthenticationRequest() { MockHttpServletRequest request = new MockHttpServletRequest(); request.setServletPath("/j_mock_post"); request.setScheme("http"); request.setServerName("www.example.com"); request.setRequestURI("/mycontext/j_mock_post"); request.setContextPath("/mycontext"); return request; }
From source file:org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilterTests.java
@Test public void testFilterProcessesUrlVariationsRespected() throws Exception { // Setup our HTTP request MockHttpServletRequest request = createMockAuthenticationRequest(); request.setServletPath("/j_OTHER_LOCATION"); request.setRequestURI("/mycontext/j_OTHER_LOCATION"); // Setup our filter configuration MockFilterConfig config = new MockFilterConfig(null, null); // Setup our expectation that the filter chain will not be invoked, as we redirect // to defaultTargetUrl MockFilterChain chain = new MockFilterChain(false); MockHttpServletResponse response = new MockHttpServletResponse(); // Setup our test object, to grant access MockAuthenticationFilter filter = new MockAuthenticationFilter(true); filter.setFilterProcessesUrl("/j_OTHER_LOCATION"); filter.setAuthenticationSuccessHandler(successHandler); // Test/* w w w .ja v a2s. c o m*/ filter.doFilter(request, response, chain); assertThat(response.getRedirectedUrl()).isEqualTo("/mycontext/logged_in.jsp"); assertThat(SecurityContextHolder.getContext().getAuthentication()).isNotNull(); assertThat(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString()) .isEqualTo("test"); }
From source file:org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilterTests.java
@Test public void testIgnoresAnyServletPathOtherThanFilterProcessesUrl() throws Exception { // Setup our HTTP request MockHttpServletRequest request = createMockAuthenticationRequest(); request.setServletPath("/some.file.html"); request.setRequestURI("/mycontext/some.file.html"); // Setup our filter configuration MockFilterConfig config = new MockFilterConfig(null, null); // Setup our expectation that the filter chain will be invoked, as our request is // for a page the filter isn't monitoring MockFilterChain chain = new MockFilterChain(true); MockHttpServletResponse response = new MockHttpServletResponse(); // Setup our test object, to deny access MockAuthenticationFilter filter = new MockAuthenticationFilter(false); // Test/*from ww w . jav a 2 s. c o m*/ filter.doFilter(request, response, chain); }
From source file:org.springframework.security.web.authentication.www.DigestAuthenticationEntryPointTests.java
@Test public void testNormalOperation() throws Exception { DigestAuthenticationEntryPoint ep = new DigestAuthenticationEntryPoint(); ep.setRealmName("hello"); ep.setKey("key"); MockHttpServletRequest request = new MockHttpServletRequest(); request.setRequestURI("/some_path"); MockHttpServletResponse response = new MockHttpServletResponse(); ep.afterPropertiesSet();//from w ww . j av a 2 s .c o m ep.commence(request, response, new DisabledException("foobar")); // Check response is properly formed assertThat(response.getStatus()).isEqualTo(401); assertThat(response.getHeader("WWW-Authenticate").toString()).startsWith("Digest "); // Break up response header String header = response.getHeader("WWW-Authenticate").toString().substring(7); String[] headerEntries = StringUtils.commaDelimitedListToStringArray(header); Map<String, String> headerMap = DigestAuthUtils.splitEachArrayElementAndCreateMap(headerEntries, "=", "\""); assertThat(headerMap.get("realm")).isEqualTo("hello"); assertThat(headerMap.get("qop")).isEqualTo("auth"); assertThat(headerMap.get("stale")).isNull(); checkNonceValid(headerMap.get("nonce")); }
From source file:org.springframework.security.web.authentication.www.DigestAuthenticationEntryPointTests.java
@Test public void testOperationIfDueToStaleNonce() throws Exception { DigestAuthenticationEntryPoint ep = new DigestAuthenticationEntryPoint(); ep.setRealmName("hello"); ep.setKey("key"); MockHttpServletRequest request = new MockHttpServletRequest(); request.setRequestURI("/some_path"); MockHttpServletResponse response = new MockHttpServletResponse(); ep.afterPropertiesSet();/*www . j a v a2 s.co m*/ ep.commence(request, response, new NonceExpiredException("expired nonce")); // Check response is properly formed assertThat(response.getStatus()).isEqualTo(401); assertThat(response.getHeader("WWW-Authenticate").toString()).startsWith("Digest "); // Break up response header String header = response.getHeader("WWW-Authenticate").toString().substring(7); String[] headerEntries = StringUtils.commaDelimitedListToStringArray(header); Map<String, String> headerMap = DigestAuthUtils.splitEachArrayElementAndCreateMap(headerEntries, "=", "\""); assertThat(headerMap.get("realm")).isEqualTo("hello"); assertThat(headerMap.get("qop")).isEqualTo("auth"); assertThat(headerMap.get("stale")).isEqualTo("true"); checkNonceValid(headerMap.get("nonce")); }
From source file:org.sventon.web.ctrl.template.AbstractTemplateControllerTest.java
@Test public void testSetAuthenticationActionUrlWithBaseUrl() throws IOException, CacheException { BASE_URL = "http://browser.sventon.org/svn"; MockHttpServletRequest request = new MockHttpServletRequest(); request.setRequestURI("/svn/list"); final Map<String, Object> model = new HashMap<String, Object>(); final AbstractTemplateController ctrl = new TestController(); ConfigDirectory configDir = new TestConfigDirectory(); configDir.setServletContext(new MockServletContext()); ctrl.setApplication(new TestApplication(configDir)); ModelAndView modelAndView = ctrl.prepareAuthenticationRequiredView(request, model); assertEquals("http://browser.sventon.org/svn/list", modelAndView.getModel().get("action")); }
From source file:org.sventon.web.ctrl.template.AbstractTemplateControllerTest.java
@Test public void testSetAuthenticationActionUrlWithoutBaseUrl() throws IOException, CacheException { BASE_URL = null;/*w ww. j a v a2 s . c om*/ MockHttpServletRequest request = new MockHttpServletRequest(); request.setRequestURI("/svn/list"); final Map<String, Object> model = new HashMap<String, Object>(); final AbstractTemplateController ctrl = new TestController(); ConfigDirectory configDir = new TestConfigDirectory(); configDir.setServletContext(new MockServletContext()); ctrl.setApplication(new TestApplication(configDir)); ModelAndView modelAndView = ctrl.prepareAuthenticationRequiredView(request, model); assertEquals("http://localhost:80/svn/list", modelAndView.getModel().get("action")); }