private void contextPath(MockHttpServletRequest request, UriComponents uriComponents) { if (this.contextPath == null) { List<String> pathSegments = uriComponents.getPathSegments(); if (pathSegments.isEmpty()) { request.setContextPath(""); } else { request.setContextPath("/" + pathSegments.get(0)); } } else { String path = uriComponents.getPath(); Assert.isTrue(path != null && path.startsWith(this.contextPath), () -> "\"" + uriComponents.getPath() + "\" should start with context path \"" + this.contextPath + "\""); request.setContextPath(this.contextPath); } }
/** * Update the contextPath, servletPath, and pathInfo of the request. */ private void updatePathRequestProperties(MockHttpServletRequest request, String requestUri) { if (!requestUri.startsWith(this.contextPath)) { throw new IllegalArgumentException( "Request URI [" + requestUri + "] does not start with context path [" + this.contextPath + "]"); } request.setContextPath(this.contextPath); request.setServletPath(this.servletPath); if ("".equals(this.pathInfo)) { if (!requestUri.startsWith(this.contextPath + this.servletPath)) { throw new IllegalArgumentException( "Invalid servlet path [" + this.servletPath + "] for request URI [" + requestUri + "]"); } String extraPath = requestUri.substring(this.contextPath.length() + this.servletPath.length()); this.pathInfo = (StringUtils.hasText(extraPath) ? urlPathHelper.decodeRequestString(request, extraPath) : null); } request.setPathInfo(this.pathInfo); }
@Test public void send_redirect() throws Exception { request.setServletPath("/"); request.setContextPath("/uaa"); spyFilter.sendRedirect("/login/mfa/register", request, response); verify(response, times(1)).sendRedirect("/uaa/login/mfa/register"); }
@Before public void setup() { filter = new ReAuthenticationRequiredFilter(); authentication = mock(UaaAuthentication.class); request = new MockHttpServletRequest(); response = mock(HttpServletResponse.class); chain = mock(FilterChain.class); request.setContextPath(""); }
@Test public void requestScope() throws Exception { final String beanName = "requestScopedTestBean"; final String contextPath = "/path"; assertNull(request.getAttribute(beanName)); request.setContextPath(contextPath); TestBean testBean = wac.getBean(beanName, TestBean.class); assertEquals(contextPath, testBean.getName()); assertSame(testBean, request.getAttribute(beanName)); assertSame(testBean, wac.getBean(beanName, TestBean.class)); }
@Test public void onDeleteSessionCookiePath() throws Exception { this.request.setContextPath("/somethingunique"); this.strategy.expireSession(this.request, this.response); Cookie sessionCookie = this.response.getCookie(this.cookieName); assertThat(sessionCookie.getPath()) .isEqualTo(this.request.getContextPath() + "/"); }
@Before public void setup() { request = new MockHttpServletRequest(); request.setContextPath("/context"); response = new MockHttpServletResponse(); filterChain = new MockFilterChain(); delegate = new MockFilter(); }
@Test public void onNewSessionCookiePath() throws Exception { this.request.setContextPath("/somethingunique"); this.strategy.setSessionId(this.request, this.response, this.session.getId()); Cookie sessionCookie = this.response.getCookie(this.cookieName); assertThat(sessionCookie.getPath()) .isEqualTo(this.request.getContextPath() + "/"); }
@Test public void do_filter_mfa_completed_with_saved_request() throws Exception { SavedRequest savedRequest = mock(SavedRequest.class); String redirect = "http://localhost:8080/uaa/oauth/authorize"; when(savedRequest.getRedirectUrl()).thenReturn(redirect); when(requestCache.getRequest(same(request), same(response))).thenReturn(savedRequest); request.setContextPath("/uaa"); when(spyFilter.getNextStep(any(HttpServletRequest.class))).thenReturn(MFA_COMPLETED); spyFilter.doFilter(request, response, chain); verify(requestCache, times(1)).getRequest(same(request), same(response)); verify(spyFilter, times(1)).sendRedirect(eq(redirect), same(request), same(response)); }
@Test public void do_filter_mfa_required() throws Exception { request.setContextPath("/uaa"); when(spyFilter.getNextStep(any(HttpServletRequest.class))).thenReturn(MFA_REQUIRED); spyFilter.doFilter(request, response, chain); verify(requestCache, times(1)).saveRequest(same(request), same(response)); verify(spyFilter, times(1)).sendRedirect(eq("/login/mfa/register"), same(request), same(response)); }
@Test public void do_filter_mfa_completed_no_saved_request() throws Exception { request.setContextPath("/uaa"); when(spyFilter.getNextStep(any(HttpServletRequest.class))).thenReturn(MFA_COMPLETED); spyFilter.doFilter(request, response, chain); verify(requestCache, times(1)).getRequest(same(request), same(response)); verify(spyFilter, times(1)).sendRedirect(eq("/"), same(request), same(response)); }
@Test public void writeCookieCookiePathDefaultContextPathUsed() { this.request.setContextPath("/context"); this.serializer.writeCookieValue(cookieValue(this.sessionId)); assertThat(getCookie().getPath()).isEqualTo("/context/"); }
@Test public void writeCookieCookiePathDefaultEmptyContextPathUsed() { this.request.setContextPath(""); this.serializer.writeCookieValue(cookieValue(this.sessionId)); assertThat(getCookie().getPath()).isEqualTo("/"); }
public void setRequestData(String requestURI) { request.setRequestURI("/uaa" + requestURI); request.setPathInfo(requestURI); request.setContextPath("/uaa"); request.setServerName("localhost"); }
@Test public void writeCookieCookiePathExplicitNullCookiePathContextPathUsed() { this.request.setContextPath("/context"); this.serializer.setCookiePath(null); this.serializer.writeCookieValue(cookieValue(this.sessionId)); assertThat(getCookie().getPath()).isEqualTo("/context/"); }
@Test public void writeCookieCookiePathExplicitCookiePath() { this.request.setContextPath("/context"); this.serializer.setCookiePath("/"); this.serializer.writeCookieValue(cookieValue(this.sessionId)); assertThat(getCookie().getPath()).isEqualTo("/"); }
@Before public void setup() { sessionTimeout = 1234; request = new MockHttpServletRequest("GET", "https://uaa.somesystemdomain.com/oauth/authorize"); ((MockHttpServletRequest) request).setContextPath("/oauth/authorize"); username = "marissa"; email = "marissa@test.org"; uaaPrincipal = new UaaPrincipal("user-guid", username, email, "uaa", "", "uaa"); factory = new CurrentUserCookieFactory(sessionTimeout); }
public void setPathInfo(String pathInfo) { request.setServletPath(""); request.setPathInfo(pathInfo); request.setContextPath("/uaa"); request.setRequestURI(request.getContextPath()+request.getPathInfo()); }
@Test public void testLocalhostPortAndContextPathUrl() { MockHttpServletRequest request = new MockHttpServletRequest(); request.setScheme("http"); request.setServerName("localhost"); request.setServerPort(8080); request.setContextPath("/uaa"); ServletRequestAttributes attrs = new ServletRequestAttributes(request); RequestContextHolder.setRequestAttributes(attrs); String url = UaaUrlUtils.getUaaUrl("/something"); assertThat(url, is("http://localhost:8080/uaa/something")); }
@Test public void url_groups() { request.setServerName("localhost:8080"); setRequestData("/uaa/authenticate"); request.setPathInfo("/authenticate"); request.setContextPath("/uaa"); assertEquals("/api", filter.getUriGroup(request).getGroup()); }