Java Code Examples for org.springframework.mock.web.mockhttpservletrequest#setPathInfo()

The following examples show how to use org.springframework.mock.web.mockhttpservletrequest#setPathInfo() . 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: 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 2
Source File: CrnkServletRejectJsonTest.java    From crnk-framework with Apache License 2.0 6 votes vote down vote up
/**
 * Option to reject plain JSON GET requests is enabled explicitly.
 */
@Test
public void testRejectPlainJson() 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", "application/json");
    request.addParameter("filter[Task][name]", "John");
    request.setQueryString(URLEncoder.encode("filter[Task][name]", StandardCharsets.UTF_8.name()) + "=John");

    MockHttpServletResponse response = new MockHttpServletResponse();

    servlet.service(request, response);

    assertEquals(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, response.getStatus());
    String responseContent = response.getContentAsString();
    assertTrue(responseContent == null || "".equals(responseContent.trim()));
}
 
Example 3
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 4
Source File: SampleKatharsisServletTest.java    From katharsis-framework with Apache License 2.0 6 votes vote down vote up
@Test
public void testKatharsisMatchingException() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
	request.setMethod("GET");
	request.setContextPath("");
	request.setServletPath("/api");
	request.setPathInfo("/tasks-matching-exception");
	request.setRequestURI("/api/matching-exception");
	request.setContentType(JsonApiMediaType.APPLICATION_JSON_API);
	request.addHeader("Accept", "*/*");
	MockHttpServletResponse response = new MockHttpServletResponse();
	katharsisServlet.service(request, response);

	String responseContent = response.getContentAsString();
	assertTrue("Response should not be returned for non matching resource type", StringUtils.isBlank(responseContent));
	assertEquals(404, response.getStatus());

}
 
Example 5
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 6
Source File: OpendapServletTest.java    From tds with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void dodsDataRequestTest() throws IOException {
  String mockURI = "/thredds/dodsC" + path + ".dods";
  String mockQueryString = "Temperature_height_above_ground[0:1:0][0:1:0][41][31]";
  MockHttpServletRequest request = new MockHttpServletRequest("GET", mockURI);
  request.setContextPath("/thredds");
  request.setQueryString(mockQueryString);
  request.setPathInfo(path + ".dods");
  MockHttpServletResponse response = new MockHttpServletResponse();
  opendapServlet.doGet(request, response);
  assertEquals(200, response.getStatus());
  // not set by servlet mocker :: assertEquals("application/octet-stream", response.getContentType());

  String strResponse = response.getContentAsString();
  System.out.printf("%s%n", strResponse);
}
 
Example 7
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 8
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 9
Source File: OpendapServletTest.java    From tds with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void asciiDataRequestTest() throws UnsupportedEncodingException {
  String mockURI = "/thredds/dodsC" + path + ".ascii";
  String mockQueryString = "Temperature_height_above_ground[0:1:0][0:1:0][41][31]";
  MockHttpServletRequest request = new MockHttpServletRequest("GET", mockURI);
  request.setContextPath("/thredds");
  request.setQueryString(mockQueryString);
  request.setPathInfo(path + ".ascii");
  MockHttpServletResponse response = new MockHttpServletResponse();
  opendapServlet.doGet(request, response);
  assertEquals(200, response.getStatus());

  // String strResponse = response.getContentAsString();
  // System.out.printf("%s%n", strResponse);
}
 
Example 10
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 11
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 12
Source File: SampleKatharsisServletTest.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[Task][name]", "John");
	request.setQueryString(URLEncoder.encode("filter[Task][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 13
Source File: StaticViewerTest.java    From tds with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void checkViewerPropertyWithOpendapReplacement() 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", "{OPENDAP}.info,ODAP 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("ODAP DS info", vl.getTitle());
  Assert.assertEquals(host + contextPath + "/dodsC" + "/test/ds1.nc.info", vl.getUrl());
}
 
Example 14
Source File: HtmlUnitRequestBuilder.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void servletPath(UriComponents uriComponents, MockHttpServletRequest request) {
	if ("".equals(request.getPathInfo())) {
		request.setPathInfo(null);
	}
	String path = uriComponents.getPath();
	servletPath(request, (path != null ? path : ""));
}
 
Example 15
Source File: ApmFilterTest.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Test
void testIgnoreUrlEndWith() throws IOException, ServletException {
    filterChain = new MockFilterChain(new HttpServlet() {
    });
    when(webConfiguration.getIgnoreUrls()).thenReturn(Collections.singletonList(WildcardMatcher.valueOf("*.js")));
    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.setServletPath("/resources");
    request.setPathInfo("test.js");
    filterChain.doFilter(request, new MockHttpServletResponse());
    verify(webConfiguration, times(1)).getIgnoreUrls();
    assertThat(reporter.getTransactions()).hasSize(0);
}
 
Example 16
Source File: CrnkFilterTest.java    From crnk-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("/api");
    request.setPathInfo(null);
    request.setRequestURI("/api/tasks/");
    request.setContentType(HttpHeaders.JSONAPI_CONTENT_TYPE);
    request.addHeader("Accept", "*/*");

    MockHttpServletResponse response = new MockHttpServletResponse();

    filter.doFilter(request, response, filterChain);

    Assert.assertEquals(200, response.getStatus());

    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 17
Source File: ConfigAwareTestBase.java    From engine with GNU General Public License v3.0 5 votes vote down vote up
private void setCurrentRequestContext() {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
    request.setPathInfo("/");
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response, null);

    RequestContext.setCurrent(context);
}
 
Example 18
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 19
Source File: MockWebRequest.java    From specification-arg-resolver with Apache License 2.0 4 votes vote down vote up
@Override
public Object getNativeRequest() {
	MockHttpServletRequest req = new MockHttpServletRequest();
	req.setPathInfo(pathInfo);
	return req;
}
 
Example 20
Source File: DatasetHandlerAdapter.java    From tds with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
public static final GridDataset openGridDataset(String pathInfo) throws IOException {

    MockHttpServletRequest req = new MockHttpServletRequest();
    MockHttpServletResponse res = new MockHttpServletResponse();

    req.setPathInfo(pathInfo);

    String datasetPath = AbstractNcssController.getDatasetPath(pathInfo);
    return TdsRequestedDataset.getGridDataset(req, res, datasetPath);
  }