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

The following examples show how to use org.springframework.mock.web.MockHttpServletRequest#setParameters() . 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: HttpMockServiceImpl.java    From AnyMock with Apache License 2.0 6 votes vote down vote up
private MockHttpServletRequest buildMockRequest(HttpServletRequest request) {
    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    mockRequest.setParameters(request.getParameterMap());

    Enumeration headerNames = request.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        String key = (String)headerNames.nextElement();
        mockRequest.addHeader(key, request.getHeader(key));
    }

    mockRequest.setMethod(request.getMethod());
    mockRequest.setRequestURI(request.getRequestURI());
    mockRequest.setRemoteAddr(request.getRemoteAddr());
    mockRequest.setRemoteHost(request.getRemoteHost());
    mockRequest.setRemotePort(request.getRemotePort());
    mockRequest.setRemoteUser(request.getRemoteUser());
    mockRequest.setAttribute(BODY, request.getAttribute(BODY));
    return mockRequest;
}
 
Example 2
Source File: WmsControllerTest.java    From geomajas-project-server with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void testReadImage() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();

	Map<String, String> parameters = new HashMap<String, String>();
	parameters.put("SERVICE", "WMS");
	parameters.put("layers", "bluemarble");
	parameters.put("WIDTH", "512");
	parameters.put("HEIGHT", "512");
	parameters.put("bbox", "-52.01245495052001,-28.207099921352835,11.947593278789554,35.75294830795673");
	parameters.put("format", "image/jpeg");
	parameters.put("version", "1.1.1");
	parameters.put("srs", "EPSG:4326");
	parameters.put("styles", "");
	parameters.put("request", "GetMap");
	request.setParameters(parameters);
	request.setRequestURI("d/wms/proxyBlue/");
	request.setQueryString("SERVICE=WMS&layers=bluemarble&" +
			"WIDTH=512&HEIGHT=512&bbox=-52.01245495052001,-28.207099921352835,11.947593278789554," +
			"35.75294830795673&format=image/jpeg&version=1.1.1&srs=EPSG:4326&styles=&request=GetMap");
	request.setMethod("GET");
	wmsController.getWms(request, response);
	new ImageAssert(response).assertEqualImage("wms.jpg", false, DELTA);
}
 
Example 3
Source File: SampleKatharsisServletTest.java    From katharsis-framework with Apache License 2.0 6 votes vote down vote up
@Test
public void testKatharsisInclude() throws Exception {

	Node root = new Node(1L, null, null);
	Node child1 = new Node(2L, root, Collections.<Node>emptySet());
	Node child2 = new Node(3L, root, Collections.<Node>emptySet());
	root.setChildren(new LinkedHashSet<>(Arrays.asList(child1, child2)));
	nodeRepository.save(root);
	nodeRepository.save(child1);
	nodeRepository.save(child2);
	MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
	request.setMethod("GET");
	request.setServletPath("/api");
	request.setPathInfo("/nodes/1");
	request.setRequestURI("/api/nodes/1");
	request.setQueryString("include[nodes]=parent");
	request.setContentType(JsonApiMediaType.APPLICATION_JSON_API);
	Map<String, String> params = new HashMap<>();
	params.put("include[nodes]", "children");
	request.setParameters(params);
	request.addHeader("Accept", "*/*");
	MockHttpServletResponse response = new MockHttpServletResponse();
	katharsisServlet.service(request, response);
	String responseContent = response.getContentAsString();
	assertTopLevelNodesCorrectWithChildren(responseContent);
}
 
Example 4
Source File: SampleKatharsisServletTest.java    From katharsis-framework with Apache License 2.0 6 votes vote down vote up
@Test
public void testKatharsisIncludeWithNullIterableRelationshipCall() throws Exception {
	Node root = new Node(1L, null, null);
	// by making the setting children null and requesting them in the include statement should cause a serialization error
	Node child1 = new Node(2L, root, null);
	Node child2 = new Node(3L, root, null);
	root.setChildren(new LinkedHashSet<>(Arrays.asList(child1, child2)));
	nodeRepository.save(root);
	nodeRepository.save(child1);
	nodeRepository.save(child2);
	MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
	request.setMethod("GET");
	request.setServletPath("/api");
	request.setPathInfo("/nodes");
	request.setRequestURI("/api/nodes");
	request.setQueryString("include[nodes]=children");
	request.setContentType(JsonApiMediaType.APPLICATION_JSON_API);
	Map<String, String> params = new HashMap<>();
	params.put("include[nodes]", "children");
	request.setParameters(params);
	request.addHeader("Accept", "*/*");
	MockHttpServletResponse response = new MockHttpServletResponse();
	katharsisServlet.service(request, response);
	assertEquals(500, response.getStatus());
}
 
Example 5
Source File: KatharsisServletTest.java    From katharsis-framework with Apache License 2.0 6 votes vote down vote up
@Test
public void testKatharsisInclude() throws Exception {

	Node root = new Node(1L, null, null);
	Node child1 = new Node(2L, root, Collections.<Node>emptySet());
	Node child2 = new Node(3L, root, Collections.<Node>emptySet());
	root.setChildren(new LinkedHashSet<>(Arrays.asList(child1, child2)));
	nodeRepository.save(root);
	nodeRepository.save(child1);
	nodeRepository.save(child2);
	MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
	request.setMethod("GET");
	request.setServletPath("/api");
	request.setPathInfo("/nodes/1");
	request.setRequestURI("/api/nodes/1");
	request.setQueryString("include[nodes]=parent");
	request.setContentType(JsonApiMediaType.APPLICATION_JSON_API);
	Map<String, String> params = new HashMap<>();
	params.put("include[nodes]", "children");
	request.setParameters(params);
	request.addHeader("Accept", "*/*");
	MockHttpServletResponse response = new MockHttpServletResponse();
	katharsisServlet.service(request, response);
	String responseContent = response.getContentAsString();
	assertTopLevelNodesCorrectWithChildren(responseContent);
}
 
Example 6
Source File: KatharsisServletTest.java    From katharsis-framework with Apache License 2.0 6 votes vote down vote up
@Test
public void testKatharsisIncludeWithNullIterableRelationshipCall() throws Exception {
	Node root = new Node(1L, null, null);
	// by making the setting children null and requesting them in the
	// include statement should cause a serialization error
	Node child1 = new Node(2L, root, null);
	Node child2 = new Node(3L, root, null);
	root.setChildren(new LinkedHashSet<>(Arrays.asList(child1, child2)));
	nodeRepository.save(root);
	nodeRepository.save(child1);
	nodeRepository.save(child2);
	MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
	request.setMethod("GET");
	request.setServletPath("/api");
	request.setPathInfo("/nodes");
	request.setRequestURI("/api/nodes");
	request.setQueryString("include[nodes]=children");
	request.setContentType(JsonApiMediaType.APPLICATION_JSON_API);
	Map<String, String> params = new HashMap<>();
	params.put("include[nodes]", "children");
	request.setParameters(params);
	request.addHeader("Accept", "*/*");
	MockHttpServletResponse response = new MockHttpServletResponse();
	katharsisServlet.service(request, response);
	assertEquals(500, response.getStatus());
}
 
Example 7
Source File: WmsControllerCacheTest.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
//@Ignore // test fails as the passivated state is not removed, see CACHE-33
public void testReadCachedImage() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	Map<String, String> parameters = new HashMap<String, String>();
	parameters.put("SERVICE", "WMS");
	parameters.put("layers", "bluemarble");
	parameters.put("WIDTH", "512");
	parameters.put("HEIGHT", "512");
	parameters.put("bbox", "-52.01245495052001,-28.207099921352835,11.947593278789554,35.75294830795673");
	parameters.put("format", "image/jpeg");
	parameters.put("version", "1.1.1");
	parameters.put("srs", "EPSG:4326");
	parameters.put("styles", "");
	parameters.put("request", "GetMap");
	request.setParameters(parameters);
	request.setRequestURI("d/wms/cachedBlue/");
	request.setQueryString("SERVICE=WMS&layers=bluemarble&" +
			"WIDTH=512&HEIGHT=512&bbox=-52.01245495052001,-28.207099921352835,11.947593278789554," +
			"35.75294830795673&format=image/jpeg&version=1.1.1&srs=EPSG:4326&styles=&request=GetMap");
	request.setMethod("GET");

	testRecorder.clear();
	wmsController.getWms(request, response);
	new ImageAssert(response).assertEqualImage("wms.jpg", false, DELTA);
	assertThat(testRecorder.matches(CachingLayerHttpService.TEST_RECORDER_GROUP,
			CachingLayerHttpService.TEST_RECORDER_PUT_IN_CACHE)).isEmpty();

	testRecorder.clear();
	wmsController.getWms(request, response);
	new ImageAssert(response).assertEqualImage("wms.jpg", false, DELTA);
	assertThat(testRecorder.matches(CachingLayerHttpService.TEST_RECORDER_GROUP,
			CachingLayerHttpService.TEST_RECORDER_GET_FROM_CACHE)).isEmpty();
}
 
Example 8
Source File: WmsControllerTest.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
@DirtiesContext // changing the controller
public void testDoSomething() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	wmsController.setHttpService(new MockHttpService());

	Map<String, String> parameters = new HashMap<String, String>();
	parameters.put("SERVICE", "WMS");
	parameters.put("layers", "bluemarble");
	parameters.put("WIDTH", "512");
	parameters.put("HEIGHT", "512");
	parameters.put("bbox", "-52.01245495052001,-28.207099921352835,11.947593278789554,35.75294830795673");
	parameters.put("format", "image/jpeg");
	parameters.put("version", "1.1.1");
	parameters.put("srs", "EPSG:4326");
	parameters.put("styles", "");
	parameters.put("request", "GetMap");
	request.setParameters(parameters);
	request.setRequestURI("d/wms/proxyBlue/");
	request.setQueryString("SERVICE=WMS&layers=bluemarble&" +
			"WIDTH=512&HEIGHT=512&bbox=-52.01245495052001,-28.207099921352835,11.947593278789554," +
			"35.75294830795673&format=image/jpeg&version=1.1.1&srs=EPSG:4326&styles=&request=GetMap");
	request.setMethod("GET");
	wmsController.getWms(request, response);
	Assert.assertEquals(TEST_VALUE, new String(response.getContentAsByteArray(), "UTF-8"));
}
 
Example 9
Source File: MockWebscriptServletRequest.java    From alfresco-mvc with Apache License 2.0 5 votes vote down vote up
static public MockWebscriptServletRequest createMockWebscriptServletRequest(AbstractWebScript webScript,
		String method, String webscriptUrl, String controllerMapping, final Map<String, String> parameters,
		final Map<String, String> body, final Cookie[] cookies, final Map<String, Object> headers,
		final String contentType) throws IOException {
	Match match = new Match(null, ImmutableMap.of("", ""), webscriptUrl, webScript);
	MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest(method,
			"http://localhost/alfresco" + webscriptUrl + controllerMapping);
	mockHttpServletRequest.setServletPath("alfresco");
	mockHttpServletRequest.setContextPath("http://localhost/");
	mockHttpServletRequest.setContentType(contentType);
	if (parameters != null) {
		mockHttpServletRequest.setParameters(parameters);
	}
	if (cookies != null) {
		mockHttpServletRequest.setCookies(cookies);
	}
	if (headers != null && !headers.isEmpty()) {
		for (Entry<String, Object> headerEntry : headers.entrySet()) {
			mockHttpServletRequest.addHeader(headerEntry.getKey(), headerEntry.getValue());
		}
	}

	if (HttpMethod.POST.name().equals(method) && body != null) {
		mockHttpServletRequest.setContent(new ObjectMapper().writeValueAsString(body).getBytes());
	}

	MockWebscriptServletRequest webscriptServletRequest = new MockWebscriptServletRequest(mock(Runtime.class),
			mockHttpServletRequest, match);
	// if (content != null && !content.isEmpty()) {
	// Content mockedContent = mock(Content.class);
	// webscriptServletRequest.content = mockedContent;
	// }

	return webscriptServletRequest;
}
 
Example 10
Source File: OpenURLServiceImplTest.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * Sets up the query string and parameters based on the URL.
 * @param req
 */
public void init(MockHttpServletRequest req) {
	String url = req.getRequestURL().toString();
	int queryStart = url.indexOf('?');
	if (queryStart >= 0) {
		String query = url.substring(queryStart+1);
		req.setQueryString(query);
		req.setParameters(parseQueryString(query));
	}
}
 
Example 11
Source File: OpenURLServiceImplTest.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * Sets up the query string and parameters based on the URL.
 * @param req
 */
public void init(MockHttpServletRequest req) {
	String url = req.getRequestURL().toString();
	int queryStart = url.indexOf('?');
	if (queryStart >= 0) {
		String query = url.substring(queryStart+1);
		req.setQueryString(query);
		req.setParameters(parseQueryString(query));
	}
}
 
Example 12
Source File: KatharsisServletTest.java    From katharsis-framework with Apache License 2.0 5 votes vote down vote up
@Test
public void testKatharsisIncludeNestedWithDefault() throws Exception {
	Node root = new Node(1L, null, null);
	Locale engLocale = new Locale(1L, java.util.Locale.ENGLISH);
	Node child1 = new Node(2L, root, Collections.<Node>emptySet());
	NodeComment child1Comment = new NodeComment(1L, "Child 1", child1, engLocale);
	child1.setNodeComments(new LinkedHashSet<>(Collections.singleton(child1Comment)));
	Node child2 = new Node(3L, root, Collections.<Node>emptySet(), Collections.<NodeComment>emptySet());
	root.setChildren(new LinkedHashSet<>(Arrays.asList(child1, child2)));
	nodeRepository.save(root);
	nodeRepository.save(child1);
	nodeRepository.save(child2);

	MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
	request.setMethod("GET");
	request.setServletPath("/api");
	request.setPathInfo("/nodes/1");
	request.setRequestURI("/api/nodes/1");
	request.setQueryString("include[nodes]=children.nodeComments");
	request.setContentType(JsonApiMediaType.APPLICATION_JSON_API);
	Map<String, String> params = new HashMap<>();
	params.put("include[nodes]", "children.nodeComments.langLocale");
	request.setParameters(params);
	request.addHeader("Accept", "*/*");
	MockHttpServletResponse response = new MockHttpServletResponse();
	katharsisServlet.service(request, response);
	String responseContent = response.getContentAsString();
	assertTopLevelNodesCorrectWithChildren(responseContent);
}
 
Example 13
Source File: SampleKatharsisServletTest.java    From katharsis-framework with Apache License 2.0 5 votes vote down vote up
@Test
public void testKatharsisIncludeNestedWithDefault() throws Exception {
	Node root = new Node(1L, null, null);
	Locale engLocale = new Locale(1L, java.util.Locale.ENGLISH);
	Node child1 = new Node(2L, root, Collections.<Node>emptySet());
	NodeComment child1Comment = new NodeComment(1L, "Child 1", child1, engLocale);
	child1.setNodeComments(new LinkedHashSet<>(Collections.singleton(child1Comment)));
	Node child2 = new Node(3L, root, Collections.<Node>emptySet(), Collections.<NodeComment>emptySet());
	root.setChildren(new LinkedHashSet<>(Arrays.asList(child1, child2)));
	nodeRepository.save(root);
	nodeRepository.save(child1);
	nodeRepository.save(child2);

	MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
	request.setMethod("GET");
	request.setServletPath("/api");
	request.setPathInfo("/nodes/1");
	request.setRequestURI("/api/nodes/1");
	request.setQueryString("include[nodes]=children.nodeComments");
	request.setContentType(JsonApiMediaType.APPLICATION_JSON_API);
	Map<String, String> params = new HashMap<>();
	params.put("include[nodes]", "children.nodeComments.langLocale");
	request.setParameters(params);
	request.addHeader("Accept", "*/*");
	MockHttpServletResponse response = new MockHttpServletResponse();
	katharsisServlet.service(request, response);
	String responseContent = response.getContentAsString();
	assertTopLevelNodesCorrectWithChildren(responseContent);
}
 
Example 14
Source File: RequestProcessorTest.java    From auth0-java-mvc-common with MIT License 5 votes vote down vote up
private MockHttpServletRequest getRequest(Map<String, Object> parameters) {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setScheme("https");
    request.setServerName("me.auth0.com");
    request.setServerPort(80);
    request.setRequestURI("/callback");
    request.setParameters(parameters);
    return request;
}
 
Example 15
Source File: CrnkServletTest.java    From crnk-framework with Apache License 2.0 5 votes vote down vote up
@Test
public void testIncludeNestedWithDefault() throws Exception {
    Node root = new Node(1L, null, null);
    Locale engLocale = new Locale(1L, java.util.Locale.ENGLISH);
    Node child1 = new Node(2L, root, Collections.emptySet());
    NodeComment child1Comment = new NodeComment(1L, "Child 1", child1, engLocale);
    child1.setNodeComments(new LinkedHashSet<>(Collections.singleton(child1Comment)));
    Node child2 = new Node(3L, root, Collections.emptySet(), Collections.emptySet());
    root.setChildren(new LinkedHashSet<>(Arrays.asList(child1, child2)));

    ResourceRepository nodeRepository = (ResourceRepository) servlet.getBoot().getResourceRegistry()
            .getEntry(Node.class).getResourceRepository().getImplementation();
    nodeRepository.save(root);
    nodeRepository.save(child1);
    nodeRepository.save(child2);

    MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
    request.setMethod("GET");
    request.setServletPath("/api");
    request.setPathInfo("/nodes/1");
    request.setRequestURI("/api/nodes/1");
    request.setQueryString("include[nodes]=children.nodeComments");
    request.setContentType(HttpHeaders.JSONAPI_CONTENT_TYPE);
    Map<String, String> params = new HashMap<>();
    params.put("include[nodes]", "children.nodeComments.langLocale");
    request.setParameters(params);
    request.addHeader("Accept", "*/*");
    MockHttpServletResponse response = new MockHttpServletResponse();
    servlet.service(request, response);
    String responseContent = response.getContentAsString();
    assertTopLevelNodesCorrectWithChildren(responseContent);
}
 
Example 16
Source File: CrnkServletTest.java    From crnk-framework with Apache License 2.0 5 votes vote down vote up
@Test
public void testInclude() throws Exception {

    Node root = new Node(1L, null, null);
    Node child1 = new Node(2L, root, Collections.emptySet());
    Node child2 = new Node(3L, root, Collections.emptySet());
    root.setChildren(new LinkedHashSet<>(Arrays.asList(child1, child2)));

    ResourceRepository nodeRepository = (ResourceRepository) servlet.getBoot().getResourceRegistry()
            .getEntry(Node.class).getResourceRepository().getImplementation();
    nodeRepository.save(root);
    nodeRepository.save(child1);
    nodeRepository.save(child2);
    MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
    request.setMethod("GET");
    request.setServletPath("/api");
    request.setPathInfo("/nodes/1");
    request.setRequestURI("/api/nodes/1");
    request.setQueryString("include[nodes]=parent");
    request.setContentType(HttpHeaders.JSONAPI_CONTENT_TYPE);
    Map<String, String> params = new HashMap<>();
    params.put("include[nodes]", "children");
    request.setParameters(params);
    request.addHeader("Accept", "*/*");
    MockHttpServletResponse response = new MockHttpServletResponse();
    servlet.service(request, response);
    String responseContent = response.getContentAsString();
    assertTopLevelNodesCorrectWithChildren(responseContent);
}
 
Example 17
Source File: OpenURLServiceImplTest.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
private MockHttpServletRequest createRequest(String openUrl) {
	MockHttpServletRequest req = new MockHttpServletRequest("GET", openUrl);
	req.setQueryString(openUrl);
	req.setParameters(parseQueryString(openUrl));
	return req;
}
 
Example 18
Source File: OpenURLServiceImplTest.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
private MockHttpServletRequest createRequest(String openUrl) {
	MockHttpServletRequest req = new MockHttpServletRequest("GET", openUrl);
	req.setQueryString(openUrl);
	req.setParameters(parseQueryString(openUrl));
	return req;
}