List of usage examples for org.springframework.mock.web MockHttpServletRequest setUserPrincipal
public void setUserPrincipal(@Nullable Principal userPrincipal)
From source file:org.jasig.cas.adaptors.trusted.web.flow.PrincipalFromRequestUserPrincipalNonInteractiveCredentialsActionTests.java
public void testRemoteUserExists() throws Exception { final MockHttpServletRequest request = new MockHttpServletRequest(); request.setUserPrincipal(new Principal() { public String getName() { return "test"; }/*w ww. ja v a 2 s . c o m*/ }); final MockRequestContext context = new MockRequestContext(); context.setExternalContext( new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse())); assertEquals("success", this.action.execute(context).getId()); }
From source file:org.geogig.geoserver.functional.GeoServerTestSupport.java
/** * Issue a POST request to the provided URL with the given file passed as form data. * * @param resourceUri the url to issue the request to * @param formFieldName the form field name for the file to be posted * @param file the file to post/* w w w. ja v a 2s.c om*/ * * @return the response to the request */ public MockHttpServletResponse postFile(String resourceUri, String formFieldName, File file) throws Exception { try (FileInputStream fis = new FileInputStream(file)) { MockMultipartFile mFile = new MockMultipartFile(formFieldName, fis); MockMultipartHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders .fileUpload(new URI(resourceUri)).file(mFile); MockHttpServletRequest request = requestBuilder.buildRequest(applicationContext.getServletContext()); /** * Duplicated from GeoServerSystemTestSupport#createRequest to do the same work on the * MockMultipartHttpServletRequest */ request.setScheme("http"); request.setServerName("localhost"); request.setServerPort(8080); request.setContextPath("/geoserver"); request.setRequestURI( ResponseUtils.stripQueryString(ResponseUtils.appendPath("/geoserver/", resourceUri))); // request.setRequestURL(ResponseUtils.appendPath("http://localhost:8080/geoserver", // path ) ); request.setQueryString(ResponseUtils.getQueryString(resourceUri)); request.setRemoteAddr("127.0.0.1"); request.setServletPath(ResponseUtils.makePathAbsolute(ResponseUtils.stripRemainingPath(resourceUri))); request.setPathInfo(ResponseUtils.makePathAbsolute( ResponseUtils.stripBeginningPath(ResponseUtils.stripQueryString(resourceUri)))); request.addHeader("Host", "localhost:8080"); // deal with authentication if (username != null) { String token = username + ":"; if (password != null) { token += password; } request.addHeader("Authorization", "Basic " + new String(Base64.encodeBase64(token.getBytes()))); } kvp(request, resourceUri); request.setUserPrincipal(null); /** * End duplication */ return dispatch(request); } }
From source file:org.jtalks.jcommune.web.exception.PrettyLogExceptionResolverTest.java
@Test public void testLogExceptionWithIncomingAccessDeniedException() throws Exception { Log mockLog = replaceLoggerWithMock(prettyLogExceptionResolver); AccessDeniedException accessDeniedException = new AccessDeniedException("Access denied"); MockHttpServletRequest request = new MockHttpServletRequest("POST", "/testing/url/42"); request.setServerName("testserver.com"); request.setServerPort(8080);// w w w . j av a 2 s . co m request.setContent("12345".getBytes()); request.setUserPrincipal(new UsernamePasswordAuthenticationToken("username", "password")); prettyLogExceptionResolver.logException(accessDeniedException, request); verify(mockLog).info( "Access was denied for user [username] trying to POST http://testserver.com:8080/testing/url/42"); }
From source file:org.opennms.core.test.rest.AbstractSpringJerseyRestTestCase.java
protected static MockHttpServletRequest createRequest(final ServletContext context, final String requestType, final String urlPath, Map<String, String> parameterMap, final String username, final Collection<String> roles) { final MockHttpServletRequest request = new MockHttpServletRequestThatWorks(context, requestType, contextPath + urlPath);//from w w w . j a v a 2s. c o m request.setContextPath(contextPath); request.setUserPrincipal(MockUserPrincipal.getInstance()); MockUserPrincipal.setName(username); if (username != null) { for (final String role : roles) { request.addUserRole(role); } } if (parameterMap != null) { for (Entry<String, String> eachEntry : parameterMap.entrySet()) { request.addParameter(eachEntry.getKey(), eachEntry.getValue()); } } return request; }
From source file:org.opennms.web.rest.AbstractSpringJerseyRestTestCase.java
protected MockHttpServletRequest createRequest(final String requestType, final String urlPath) { final MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), requestType, contextPath + urlPath) {/*w w w . j a va 2s . co m*/ @Override // FIXME: remove when we update to Spring 3.1 public void setContentType(final String contentType) { super.setContentType(contentType); } }; request.setContextPath(contextPath); request.setUserPrincipal(MockUserPrincipal.getInstance()); return request; }