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

The following examples show how to use org.springframework.mock.web.MockHttpServletRequest#setServletPath() . 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: CrnkServletTest.java    From crnk-framework with Apache License 2.0 6 votes vote down vote up
@Test
public void onSimpleResourceGetShouldReturnOneResource() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
    request.setMethod("GET");
    request.setContextPath("");
    request.setServletPath("/api");
    request.setPathInfo("/tasks/1");
    request.setRequestURI("/api/tasks/1");
    request.setContentType(HttpHeaders.JSONAPI_CONTENT_TYPE);
    request.addHeader("Accept", "*/*");

    MockHttpServletResponse response = new MockHttpServletResponse();

    servlet.service(request, response);

    String responseContent = response.getContentAsString();

    log.debug("responseContent: {}", responseContent);
    assertNotNull(responseContent);

    assertJsonPartEquals("tasks", responseContent, "data.type");
    assertJsonPartEquals("\"1\"", responseContent, "data.id");
    assertJsonPartEquals(FIRST_TASK_ATTRIBUTES, responseContent, "data.attributes");
    assertJsonPartEquals(FIRST_TASK_LINKS, responseContent, "data.links");
    assertJsonPartEquals(PROJECT1_RELATIONSHIP_LINKS, responseContent, "data.relationships.project.links");
}
 
Example 2
Source File: KatharsisServletTest.java    From katharsis-framework with Apache License 2.0 6 votes vote down vote up
@Test
public void onSimpleResourceGetShouldReturnOneResource() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
	request.setMethod("GET");
	request.setContextPath("");
	request.setServletPath("/api");
	request.setPathInfo("/tasks/1");
	request.setRequestURI("/api/tasks/1");
	request.setContentType(JsonApiMediaType.APPLICATION_JSON_API);
	request.addHeader("Accept", "*/*");

	MockHttpServletResponse response = new MockHttpServletResponse();

	katharsisServlet.service(request, response);

	String responseContent = response.getContentAsString();

	log.debug("responseContent: {}", responseContent);
	assertNotNull(responseContent);

	assertJsonPartEquals("tasks", responseContent, "data.type");
	assertJsonPartEquals("\"1\"", responseContent, "data.id");
	assertJsonPartEquals(SOME_TASK_ATTRIBUTES, responseContent, "data.attributes");
	assertJsonPartEquals(FIRST_TASK_LINKS, responseContent, "data.links");
	assertJsonPartEquals(PROJECT1_RELATIONSHIP_LINKS, responseContent, "data.relationships.project.links");
}
 
Example 3
Source File: KatharsisServletTest.java    From katharsis-framework with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnacceptableRequestContentType() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
	request.setMethod("GET");
	request.setContextPath("");
	request.setServletPath("/api");
	request.setPathInfo("/tasks");
	request.setRequestURI("/api/tasks");
	request.setContentType(JsonApiMediaType.APPLICATION_JSON_API);
	request.addHeader("Accept", "application/xml");
	request.addParameter("filter[Task][name]", "John");
	request.setQueryString(URLEncoder.encode("filter[Task][name]", StandardCharsets.UTF_8.name()) + "=John");

	MockHttpServletResponse response = new MockHttpServletResponse();

	katharsisServlet.service(request, response);

	assertEquals(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, response.getStatus());
	String responseContent = response.getContentAsString();
	assertTrue(responseContent == null || "".equals(responseContent.trim()));
}
 
Example 4
Source File: CrnkServletTest.java    From crnk-framework with Apache License 2.0 6 votes vote down vote up
@Test
public void onSimpleCollectionGetShouldReturnCollectionOfResources() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
    request.setMethod("GET");
    request.setContextPath("");
    request.setServletPath("/api");
    request.setPathInfo("/tasks/");
    request.setRequestURI("/api/tasks/");
    request.setContentType(HttpHeaders.JSONAPI_CONTENT_TYPE);
    request.addHeader("Accept", "*/*");

    MockHttpServletResponse response = new MockHttpServletResponse();

    servlet.service(request, response);

    String responseContent = response.getContentAsString();

    log.debug("responseContent: {}", responseContent);
    assertNotNull(responseContent);

    assertJsonPartEquals("tasks", responseContent, "data[0].type");
    assertJsonPartEquals("\"1\"", responseContent, "data[0].id");
    assertJsonPartEquals(FIRST_TASK_ATTRIBUTES, responseContent, "data[0].attributes");
    assertJsonPartEquals(FIRST_TASK_LINKS, responseContent, "data[0].links");
    assertJsonPartEquals(PROJECT1_RELATIONSHIP_LINKS, responseContent, "data[0].relationships.project.links");
}
 
Example 5
Source File: AuthenticationFilterPathMatchingTest.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
@Test
public void testHttpBasicAuthenticationCheck() throws IOException, ServletException {
  if (authenticationExpected) {
    when(identityServiceMock.checkPassword(MockProvider.EXAMPLE_USER_ID, MockProvider.EXAMPLE_USER_PASSWORD)).thenReturn(true);
  }

  MockHttpServletResponse response = new MockHttpServletResponse();
  MockHttpServletRequest request = new MockHttpServletRequest();
  request.setRequestURI(SERVICE_PATH + servletPath + requestUrl);
  request.setContextPath(SERVICE_PATH);
  request.setServletPath(servletPath);
  applyFilter(request, response, MockProvider.EXAMPLE_USER_ID, MockProvider.EXAMPLE_USER_PASSWORD);

  Assert.assertEquals(Status.OK.getStatusCode(), response.getStatus());

  if (authenticationExpected) {
    verify(identityServiceMock).setAuthentication(MockProvider.EXAMPLE_USER_ID, groupIds, tenantIds);
    verify(identityServiceMock).clearAuthentication();

  } else {
    verify(identityServiceMock, never()).setAuthentication(any(String.class), anyListOf(String.class), anyListOf(String.class));
    verify(identityServiceMock, never()).clearAuthentication();
  }
}
 
Example 6
Source File: MockHttpServletRequestBuilder.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * 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);
}
 
Example 7
Source File: ServletRequestContextTest.java    From crnk-framework with Apache License 2.0 6 votes vote down vote up
@Test
public void testRootPath() {
	MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
	request.setMethod("GET");
	request.setContextPath("");
	request.setServletPath("");
	request.setPathInfo("");
	request.setRequestURI("");
	request.setContentType(HttpHeaders.JSONAPI_CONTENT_TYPE);
	request.addHeader("Accept", "*/*");
	request.setServerName("test");
	request.setServerPort(1234);

	ServletRequestContext context = new ServletRequestContext(servletContext, request, servletResponse, null);

	Assert.assertEquals("/", context.getPath());
	Assert.assertEquals("http://test:1234", context.getBaseUrl());
}
 
Example 8
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 9
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 10
Source File: WebDAVHelperTest.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void canGetUrlPathPrefixFromServletPath()
{
    // Path prefix not explicitly set on helper.
    davHelper.setUrlPathPrefix("");
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/before/the-servlet/folder/filename.txt");
    // Servlet path will be used to determine path prefix.
    request.setServletPath("/the-servlet");
    String prefix = davHelper.getUrlPathPrefix(request);
    assertEquals("/before/the-servlet/", prefix);
}
 
Example 11
Source File: KatharsisServletTest.java    From katharsis-framework with Apache License 2.0 5 votes vote down vote up
@Test
public void onCollectionRequestWithParamsGetShouldReturnCollection() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
	request.setMethod("GET");
	request.setContextPath("");
	request.setServletPath("/api");
	request.setPathInfo("/tasks");
	request.setRequestURI("/api/tasks");
	request.setContentType(JsonApiMediaType.APPLICATION_JSON_API);
	request.addHeader("Accept", "*/*");
	request.addParameter("filter[name]", "John");
	request.setQueryString(URLEncoder.encode("filter[name]", StandardCharsets.UTF_8.name()) + "=John");

	MockHttpServletResponse response = new MockHttpServletResponse();

	katharsisServlet.service(request, response);

	String responseContent = response.getContentAsString();

	log.debug("responseContent: {}", responseContent);
	assertNotNull(responseContent);

	assertJsonPartEquals("tasks", responseContent, "data[0].type");
	assertJsonPartEquals("\"1\"", responseContent, "data[0].id");
	assertJsonPartEquals(FIRST_TASK_ATTRIBUTES, responseContent, "data[0].attributes");
	assertJsonPartEquals(FIRST_TASK_LINKS, responseContent, "data[0].links");
	assertJsonPartEquals(PROJECT1_RELATIONSHIP_LINKS, responseContent, "data[0].relationships.project.links");
}
 
Example 12
Source File: MockHttpServletRequestBuilder.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Update the contextPath, servletPath, and pathInfo of the request.
 */
private void updatePathRequestProperties(MockHttpServletRequest request, String requestUri) {
	Assert.isTrue(requestUri.startsWith(this.contextPath),
			"requestURI [" + requestUri + "] does not start with contextPath [" + this.contextPath + "]");
	request.setContextPath(this.contextPath);
	request.setServletPath(this.servletPath);
	if (ValueConstants.DEFAULT_NONE.equals(this.pathInfo)) {
		Assert.isTrue(requestUri.startsWith(this.contextPath + this.servletPath),
				"Invalid servletPath [" + this.servletPath + "] for requestURI [" + requestUri + "]");
		String extraPath = requestUri.substring(this.contextPath.length() + this.servletPath.length());
		this.pathInfo = (StringUtils.hasText(extraPath)) ? extraPath : null;
	}
	request.setPathInfo(this.pathInfo);
}
 
Example 13
Source File: KatharsisFilterTest.java    From katharsis-framework with Apache License 2.0 5 votes vote down vote up
@Test
public void onSimpleCollectionGetShouldReturnCollectionOfResources() throws Exception {
	MockFilterChain filterChain = new MockFilterChain();

	MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
	request.setMethod("GET");
	request.setContextPath("");
	request.setServletPath(null);
	request.setPathInfo(null);
	request.setRequestURI("/api/tasks/");
	request.setContentType(JsonApiMediaType.APPLICATION_JSON_API);
	request.addHeader("Accept", "*/*");

	MockHttpServletResponse response = new MockHttpServletResponse();

	katharsisFilter.doFilter(request, response, filterChain);

	String responseContent = response.getContentAsString();

	log.debug("responseContent: {}", responseContent);
	assertNotNull(responseContent);

	assertJsonPartEquals("tasks", responseContent, "data[0].type");
	assertJsonPartEquals("\"1\"", responseContent, "data[0].id");
	assertJsonPartEquals(FIRST_TASK_ATTRIBUTES, responseContent, "data[0].attributes");
	assertJsonPartEquals(FIRST_TASK_LINKS, responseContent, "data[0].links");
	assertJsonPartEquals(PROJECT1_RELATIONSHIP_LINKS, responseContent, "data[0].relationships.project.links");
}
 
Example 14
Source File: CrnkFilterTest.java    From crnk-framework with Apache License 2.0 5 votes vote down vote up
@Test
public void onCollectionRequestWithParamsGetShouldReturnCollection() throws Exception {
    MockFilterChain filterChain = new MockFilterChain();

    MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
    request.setMethod("GET");
    request.setContextPath("");
    request.setServletPath("/api");
    request.setPathInfo(null);
    request.setRequestURI("/api/tasks");
    request.setContentType(HttpHeaders.JSONAPI_CONTENT_TYPE);
    request.addHeader("Accept", "*/*");
    request.addParameter("filter[name]", "First task");
    request.setQueryString(URLEncoder.encode("filter[name]", StandardCharsets.UTF_8.name()) + "=First task");

    MockHttpServletResponse response = new MockHttpServletResponse();

    filter.doFilter(request, response, filterChain);

    String responseContent = response.getContentAsString();

    log.debug("responseContent: {}", responseContent);
    assertNotNull(responseContent);

    assertJsonPartEquals("tasks", responseContent, "data[0].type");
    assertJsonPartEquals("\"1\"", responseContent, "data[0].id");
    assertJsonPartEquals(FIRST_TASK_ATTRIBUTES, responseContent, "data[0].attributes");
    assertJsonPartEquals(FIRST_TASK_LINKS, responseContent, "data[0].links");
    assertJsonPartEquals(PROJECT1_RELATIONSHIP_LINKS, responseContent, "data[0].relationships.project.links");
}
 
Example 15
Source File: TestControllerManager.java    From entando-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testService_1() throws ApsSystemException {
    RequestContext reqCtx = this.getRequestContext();
    ControllerManager controller = (ControllerManager) this.getService(SystemConstants.CONTROLLER_MANAGER);
    MockHttpServletRequest request = (MockHttpServletRequest) reqCtx.getRequest();
    request.setServletPath("/it/homepage.page");
    int status = controller.service(reqCtx);
    assertEquals(ControllerManager.OUTPUT, status);

    request.setParameter("username", "admin");
    request.setParameter("password", "admin");
    status = controller.service(reqCtx);
    assertEquals(ControllerManager.OUTPUT, status);
}
 
Example 16
Source File: EndpointsPeerAuthenticatorTest.java    From endpoints-java with Apache License 2.0 5 votes vote down vote up
private static MockHttpServletRequest createRequest(
    String host, int port, String servletPath, String contextPath, String queryString) {
  MockHttpServletRequest request = new MockHttpServletRequest();
  request.addHeader("Host", host);
  request.setServerName(host);
  request.setServerPort(port);
  request.setServletPath(servletPath);
  request.setQueryString(queryString);
  request.setContextPath(contextPath);
  return request;
}
 
Example 17
Source File: KatharsisFilterTest.java    From katharsis-framework with Apache License 2.0 5 votes vote down vote up
@Test
public void onCollectionRequestWithParamsGetShouldReturnCollection() throws Exception {
	MockFilterChain filterChain = new MockFilterChain();

	MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
	request.setMethod("GET");
	request.setContextPath("");
	request.setServletPath(null);
	request.setPathInfo(null);
	request.setRequestURI("/api/tasks");
	request.setContentType(JsonApiMediaType.APPLICATION_JSON_API);
	request.addHeader("Accept", "*/*");
	request.addParameter("filter[name]", "John");
	request.setQueryString(URLEncoder.encode("filter[name]", StandardCharsets.UTF_8.name()) + "=John");

	MockHttpServletResponse response = new MockHttpServletResponse();

	katharsisFilter.doFilter(request, response, filterChain);

	String responseContent = response.getContentAsString();

	log.debug("responseContent: {}", responseContent);
	assertNotNull(responseContent);

	assertJsonPartEquals("tasks", responseContent, "data[0].type");
	assertJsonPartEquals("\"1\"", responseContent, "data[0].id");
	assertJsonPartEquals(FIRST_TASK_ATTRIBUTES, responseContent, "data[0].attributes");
	assertJsonPartEquals(FIRST_TASK_LINKS, responseContent, "data[0].links");
	assertJsonPartEquals(PROJECT1_RELATIONSHIP_LINKS, responseContent, "data[0].relationships.project.links");
}
 
Example 18
Source File: StaticViewerTest.java    From tds with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void checkViewerPropertyWithRemoteWmsReplacement() throws URISyntaxException {
  String host = "http://test.thredds.servlet.StaticViewerTest";
  String contextPath = "/thredds";
  String servletPath = "/catalog";
  String catPathNoExtension = "/checkViewerPropertyWithOpendapReplacement";
  String docBaseUriString = host + contextPath + servletPath + catPathNoExtension + ".xml";
  URI docBaseUri = new URI(docBaseUriString);

  String catalogAsString = setupCatDsWithViewerProperty("viewer1", "{REMOTEWMS}.info,RemoteWms DS info");

  Dataset ds1 = constructCatalogAndAssertAsExpected(docBaseUri, catalogAsString);

  MockHttpServletRequest request = new MockHttpServletRequest();
  request.setMethod("GET");
  request.setContextPath(contextPath);
  request.setServletPath(servletPath);
  request.setPathInfo(catPathNoExtension + ".html");
  request.setParameter("dataset", "ds1");

  ViewerLinkProvider sv = ViewerServiceImpl.getStaticView();
  List<ViewerLinkProvider.ViewerLink> viewerLinks = sv.getViewerLinks(ds1, request);
  assertNotNull(viewerLinks);
  Assert.assertEquals(1, viewerLinks.size());

  ViewerLinkProvider.ViewerLink vl = viewerLinks.get(0);
  assertNotNull(vl);
  Assert.assertEquals("RemoteWms DS info", vl.getTitle());
  Assert.assertEquals("http://server/thredds/wms/test/ds1.nc.info", vl.getUrl());
}
 
Example 19
Source File: ForwardRequestPostProcessor.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) {
	request.setServletPath(this.forwardUrl);
	return request;
}
 
Example 20
Source File: ForwardRequestPostProcessor.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) {
	request.setServletPath(this.forwardUrl);
	return request;
}