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

The following examples show how to use org.springframework.mock.web.MockHttpServletRequest#getParameterMap() . 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: PrintingResultHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
protected final MultiValueMap<String, String> getParamsMultiValueMap(MockHttpServletRequest request) {
	Map<String, String[]> params = request.getParameterMap();
	MultiValueMap<String, String> multiValueMap = new LinkedMultiValueMap<>();
	params.forEach((name, values) -> {
		if (params.get(name) != null) {
			for (String value : values) {
				multiValueMap.add(name, value);
			}
		}
	});
	return multiValueMap;
}
 
Example 2
Source File: MockHttpServletRequestBuilderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void requestParameter() {
	this.builder.param("foo", "bar", "baz");

	MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
	Map<String, String[]> parameterMap = request.getParameterMap();

	assertArrayEquals(new String[] {"bar", "baz"}, parameterMap.get("foo"));
}
 
Example 3
Source File: MockHttpServletRequestBuilderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void requestParameterFromQuery() {
	this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/?foo=bar&foo=baz");

	MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
	Map<String, String[]> parameterMap = request.getParameterMap();

	assertArrayEquals(new String[] {"bar", "baz"}, parameterMap.get("foo"));
	assertEquals("foo=bar&foo=baz", request.getQueryString());
}
 
Example 4
Source File: MockHttpServletRequestBuilderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test  // SPR-11043
public void requestParameterFromQueryNull() {
	this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/?foo");

	MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
	Map<String, String[]> parameterMap = request.getParameterMap();

	assertArrayEquals(new String[] {null}, parameterMap.get("foo"));
	assertEquals("foo", request.getQueryString());
}
 
Example 5
Source File: PrintingResultHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
protected final MultiValueMap<String, String> getParamsMultiValueMap(MockHttpServletRequest request) {
	Map<String, String[]> params = request.getParameterMap();
	MultiValueMap<String, String> multiValueMap = new LinkedMultiValueMap<>();
	params.forEach((name, values) -> {
		if (params.get(name) != null) {
			for (String value : values) {
				multiValueMap.add(name, value);
			}
		}
	});
	return multiValueMap;
}
 
Example 6
Source File: MockHttpServletRequestBuilderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void requestParameter() {
	this.builder.param("foo", "bar", "baz");

	MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
	Map<String, String[]> parameterMap = request.getParameterMap();

	assertArrayEquals(new String[] {"bar", "baz"}, parameterMap.get("foo"));
}
 
Example 7
Source File: MockHttpServletRequestBuilderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void requestParameterFromQuery() {
	this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/?foo=bar&foo=baz");

	MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
	Map<String, String[]> parameterMap = request.getParameterMap();

	assertArrayEquals(new String[] {"bar", "baz"}, parameterMap.get("foo"));
	assertEquals("foo=bar&foo=baz", request.getQueryString());
}
 
Example 8
Source File: MockHttpServletRequestBuilderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test  // SPR-11043
public void requestParameterFromQueryNull() {
	this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/?foo");

	MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
	Map<String, String[]> parameterMap = request.getParameterMap();

	assertArrayEquals(new String[] {null}, parameterMap.get("foo"));
	assertEquals("foo", request.getQueryString());
}
 
Example 9
Source File: PrintingResultHandler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
protected final MultiValueMap<String, String> getParamsMultiValueMap(MockHttpServletRequest request) {
	Map<String, String[]> params = request.getParameterMap();
	MultiValueMap<String, String> multiValueMap = new LinkedMultiValueMap<String, String>();
	for (String name : params.keySet()) {
		if (params.get(name) != null) {
			for (String value : params.get(name)) {
				multiValueMap.add(name, value);
			}
		}
	}
	return multiValueMap;
}
 
Example 10
Source File: MockHttpServletRequestBuilderTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void requestParameter() {
	this.builder.param("foo", "bar", "baz");

	MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
	Map<String, String[]> parameterMap = request.getParameterMap();

	assertArrayEquals(new String[] {"bar", "baz"}, parameterMap.get("foo"));
}
 
Example 11
Source File: MockHttpServletRequestBuilderTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void requestParameterFromQuery() {
	this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/?foo=bar&foo=baz");

	MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
	Map<String, String[]> parameterMap = request.getParameterMap();

	assertArrayEquals(new String[] {"bar", "baz"}, parameterMap.get("foo"));
	assertEquals("foo=bar&foo=baz", request.getQueryString());
}
 
Example 12
Source File: MockHttpServletRequestBuilderTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void requestParameterFromQueryNull() {
	this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/?foo");

	MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
	Map<String, String[]> parameterMap = request.getParameterMap();

	assertArrayEquals(new String[] {null}, parameterMap.get("foo"));
	assertEquals("foo", request.getQueryString());
}
 
Example 13
Source File: AdminUserJspBeanTest.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void testDoImportUsersFromFile( ) throws AccessDeniedException, UserNotSignedException, IOException
{
    AdminUserJspBean bean = new AdminUserJspBean( );
    MockHttpServletRequest request = new MockHttpServletRequest( );
    request.addParameter( SecurityTokenService.PARAMETER_TOKEN, SecurityTokenService.getInstance( ).getToken( request, "jsp/admin/user/ImportUser.jsp" ) );
    AdminUser user = getUserToModify( );
    AdminAuthenticationService.getInstance( ).registerUser( request, user );
    bean.init( request, "CORE_USERS_MANAGEMENT" );
    Map<String, List<FileItem>> multipartFiles = new HashMap<>( );
    List<FileItem> fileItems = new ArrayList<>( );
    FileItem file = new DiskFileItem( "import_file", "application/csv", true, "junit.csv", 1024, new File( System.getProperty( "java.io.tmpdir" ) ) );
    OutputStreamWriter writer = new OutputStreamWriter( file.getOutputStream( ), Charset.forName( "UTF-8" ) );
    writer.write( "test;test;test;[email protected];" + AdminUser.ACTIVE_CODE + ";" + Locale.FRANCE + ";0;false;false;;;" );
    writer.close( );
    fileItems.add( file );
    multipartFiles.put( "import_file", fileItems );
    Map<String, String [ ]> parameters = request.getParameterMap( );
    MultipartHttpServletRequest multipartRequest = new MultipartHttpServletRequest( request, multipartFiles, parameters );
    bean.getImportUsersFromFile( request ); // initialize _importAdminUserService
    AdminUser importedUser = null;
    try
    {
        bean.doImportUsersFromFile( multipartRequest );
        importedUser = AdminUserHome.findUserByLogin( "test" );
        assertNotNull( importedUser );
        assertEquals( "test", importedUser.getAccessCode( ) );
        assertEquals( "test", importedUser.getFirstName( ) );
        assertEquals( "test", importedUser.getLastName( ) );
        assertEquals( "[email protected]", importedUser.getEmail( ) );
        assertEquals( AdminUser.ACTIVE_CODE, importedUser.getStatus( ) );
    }
    finally
    {
        if ( importedUser != null )
        {
            disposeOfUser( importedUser );
        }
        disposeOfUser( user );
    }
}
 
Example 14
Source File: AdminUserJspBeanTest.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void testDoImportUsersFromFileInvalidToken( ) throws AccessDeniedException, UserNotSignedException, IOException
{
    AdminUserJspBean bean = new AdminUserJspBean( );
    MockHttpServletRequest request = new MockHttpServletRequest( );
    request.addParameter( SecurityTokenService.PARAMETER_TOKEN, SecurityTokenService.getInstance( ).getToken( request, "jsp/admin/user/ImportUser.jsp" )
            + "b" );
    AdminUser user = getUserToModify( );
    AdminAuthenticationService.getInstance( ).registerUser( request, user );
    bean.init( request, "CORE_USERS_MANAGEMENT" );
    Map<String, List<FileItem>> multipartFiles = new HashMap<>( );
    List<FileItem> fileItems = new ArrayList<>( );
    FileItem file = new DiskFileItem( "import_file", "application/csv", true, "junit.csv", 1024, new File( System.getProperty( "java.io.tmpdir" ) ) );
    OutputStreamWriter writer = new OutputStreamWriter( file.getOutputStream( ), Charset.forName( "UTF-8" ) );
    writer.write( "test;test;test;[email protected];" + AdminUser.ACTIVE_CODE + ";" + Locale.FRANCE + ";0;false;false;;;" );
    writer.close( );
    fileItems.add( file );
    multipartFiles.put( "import_file", fileItems );
    Map<String, String [ ]> parameters = request.getParameterMap( );
    MultipartHttpServletRequest multipartRequest = new MultipartHttpServletRequest( request, multipartFiles, parameters );
    bean.getImportUsersFromFile( request ); // initialize _importAdminUserService
    AdminUser importedUser = null;
    try
    {
        bean.doImportUsersFromFile( multipartRequest );
        fail( "Should have thrown" );
    }
    catch( AccessDeniedException e )
    {
        importedUser = AdminUserHome.findUserByLogin( "test" );
        assertNull( importedUser );
    }
    finally
    {
        if ( importedUser != null )
        {
            disposeOfUser( importedUser );
        }
        disposeOfUser( user );
    }
}
 
Example 15
Source File: AdminUserJspBeanTest.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void testDoImportUsersFromFileNoToken( ) throws AccessDeniedException, UserNotSignedException, IOException
{
    AdminUserJspBean bean = new AdminUserJspBean( );
    MockHttpServletRequest request = new MockHttpServletRequest( );
    AdminUser user = getUserToModify( );
    AdminAuthenticationService.getInstance( ).registerUser( request, user );
    bean.init( request, "CORE_USERS_MANAGEMENT" );
    Map<String, List<FileItem>> multipartFiles = new HashMap<>( );
    List<FileItem> fileItems = new ArrayList<>( );
    FileItem file = new DiskFileItem( "import_file", "application/csv", true, "junit.csv", 1024, new File( System.getProperty( "java.io.tmpdir" ) ) );
    OutputStreamWriter writer = new OutputStreamWriter( file.getOutputStream( ), Charset.forName( "UTF-8" ) );
    writer.write( "test;test;test;[email protected];" + AdminUser.ACTIVE_CODE + ";" + Locale.FRANCE + ";0;false;false;;;" );
    writer.close( );
    fileItems.add( file );
    multipartFiles.put( "import_file", fileItems );
    Map<String, String [ ]> parameters = request.getParameterMap( );
    MultipartHttpServletRequest multipartRequest = new MultipartHttpServletRequest( request, multipartFiles, parameters );
    bean.getImportUsersFromFile( request ); // initialize _importAdminUserService
    AdminUser importedUser = null;
    try
    {
        bean.doImportUsersFromFile( multipartRequest );
        fail( "Should have thrown" );
    }
    catch( AccessDeniedException e )
    {
        importedUser = AdminUserHome.findUserByLogin( "test" );
        assertNull( importedUser );
    }
    finally
    {
        if ( importedUser != null )
        {
            disposeOfUser( importedUser );
        }
        disposeOfUser( user );
    }
}