private static String getPath() { UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentServletMapping(); String path = builder.build().getPath(); return path != null ? path : ""; }
@Test public void path() { UriComponentsBuilder builder = UriComponentsBuilder.fromPath("/foo/bar"); UriComponents result = builder.build(); assertEquals("/foo/bar", result.getPath()); assertEquals(Arrays.asList("foo", "bar"), result.getPathSegments()); }
@Test public void pathSegmentsSomeEmpty() { UriComponentsBuilder builder = UriComponentsBuilder.newInstance().pathSegment("", "foo", "", "bar"); UriComponents result = builder.build(); assertEquals("/foo/bar", result.getPath()); assertEquals(Arrays.asList("foo", "bar"), result.getPathSegments()); }
private void servletPath(UriComponents uriComponents, MockHttpServletRequest request) { if ("".equals(request.getPathInfo())) { request.setPathInfo(null); } String path = uriComponents.getPath(); servletPath(request, (path != null ? path : "")); }
@Test public void pathSegmentsThenPathSegments() { UriComponentsBuilder builder = UriComponentsBuilder.newInstance().pathSegment("foo").pathSegment("bar"); UriComponents result = builder.build(); assertEquals("/foo/bar", result.getPath()); assertEquals(Arrays.asList("foo", "bar"), result.getPathSegments()); }
@Test // SPR-11391 public void fromMethodNameTypeLevelPathVariableWithoutArgumentValue() { UriComponents uriComponents = fromMethodName(UserContactController.class, "showCreate", 123).build(); assertThat(uriComponents.getPath(), is("/user/123/contacts/create")); }
@Test public void pathSegmentsThenPath() { UriComponentsBuilder builder = UriComponentsBuilder.newInstance().pathSegment("foo").path("/"); UriComponents result = builder.build(); assertEquals("/foo/", result.getPath()); assertEquals(Collections.singletonList("foo"), result.getPathSegments()); }
@Test public void fromMethodNameTwoPathVariables() { DateTime now = DateTime.now(); UriComponents uriComponents = fromMethodName( ControllerWithMethods.class, "methodWithTwoPathVariables", 1, now).build(); assertThat(uriComponents.getPath(), is("/something/1/foo/" + ISODateTimeFormat.date().print(now))); }
@Test public void pathThenPathSegments() { UriComponentsBuilder builder = UriComponentsBuilder.fromPath("/foo/bar").pathSegment("ba/z"); UriComponents result = builder.build().encode(); assertEquals("/foo/bar/ba%2Fz", result.getPath()); assertEquals(Arrays.asList("foo", "bar", "ba%2Fz"), result.getPathSegments()); }
@Test // SPR-12398 public void pathWithDuplicateSlashes() { UriComponents uriComponents = UriComponentsBuilder.fromPath("/foo/////////bar").build(); assertEquals("/foo/bar", uriComponents.getPath()); }
@Test public void pathThenPath() { UriComponentsBuilder builder = UriComponentsBuilder.fromPath("/foo/bar").path("ba/z"); UriComponents result = builder.build().encode(); assertEquals("/foo/barba/z", result.getPath()); assertEquals(Arrays.asList("foo", "barba", "z"), result.getPathSegments()); }
@Test // SPR-13311 public void expandWithRegexVar() { String template = "/myurl/{name:[a-z]{1,5}}/show"; UriComponents uriComponents = UriComponentsBuilder.fromUriString(template).build(); uriComponents = uriComponents.expand(Collections.singletonMap("name", "test")); assertEquals("/myurl/test/show", uriComponents.getPath()); }
@Test public void pathSegments() { UriComponentsBuilder builder = UriComponentsBuilder.newInstance(); UriComponents result = builder.pathSegment("foo").pathSegment("bar").build(); assertEquals("/foo/bar", result.getPath()); assertEquals(Arrays.asList("foo", "bar"), result.getPathSegments()); }
@Test // SPR-12742 public void fromHttpRequestWithTrailingSlash() { UriComponents before = UriComponentsBuilder.fromPath("/foo/").build(); UriComponents after = UriComponentsBuilder.newInstance().uriComponents(before).build(); assertEquals("/foo/", after.getPath()); }
@Test public void copyToUriComponentsBuilder() { UriComponents source = UriComponentsBuilder.fromPath("/foo/bar").pathSegment("ba/z").build(); UriComponentsBuilder targetBuilder = UriComponentsBuilder.newInstance(); source.copyToUriComponentsBuilder(targetBuilder); UriComponents result = targetBuilder.build().encode(); assertEquals("/foo/bar/ba%2Fz", result.getPath()); assertEquals(Arrays.asList("foo", "bar", "ba%2Fz"), result.getPathSegments()); }
@Test public void fromMethodNameWithPathVarAndRequestParam() { UriComponents uriComponents = fromMethodName( ControllerWithMethods.class, "methodForNextPage", "1", 10, 5).build(); assertThat(uriComponents.getPath(), is("/something/1/foo")); MultiValueMap<String, String> queryParams = uriComponents.getQueryParams(); assertThat(queryParams.get("limit"), contains("5")); assertThat(queryParams.get("offset"), contains("10")); }
@Test public void fromMethodCallWithPathVariableAndMultiValueRequestParams() { UriComponents uriComponents = fromMethodCall( on(ControllerWithMethods.class).methodWithMultiValueRequestParams("1", Arrays.asList(3, 7), 5)).build(); assertThat(uriComponents.getPath(), is("/something/1/foo")); MultiValueMap<String, String> queryParams = uriComponents.getQueryParams(); assertThat(queryParams.get("limit"), contains("5")); assertThat(queryParams.get("items"), containsInAnyOrder("3", "7")); }
@Test public void fromMethodCallWithPathVariableAndRequestParams() { UriComponents uriComponents = fromMethodCall( on(ControllerWithMethods.class).methodForNextPage("1", 10, 5)).build(); assertThat(uriComponents.getPath(), is("/something/1/foo")); MultiValueMap<String, String> queryParams = uriComponents.getQueryParams(); assertThat(queryParams.get("limit"), contains("5")); assertThat(queryParams.get("offset"), contains("10")); }
@Test public void expand() { UriComponents uriComponents = UriComponentsBuilder.fromUriString( "http://example.com").path("/{foo} {bar}").build(); uriComponents = uriComponents.expand("1 2", "3 4"); assertEquals("/1 2 3 4", uriComponents.getPath()); assertEquals("http://example.com/1 2 3 4", uriComponents.toUriString()); }
@Test public void fromHierarchicalUri() throws URISyntaxException { URI uri = new URI("http://example.com/foo?bar#baz"); UriComponents result = UriComponentsBuilder.fromUri(uri).build(); assertEquals("http", result.getScheme()); assertEquals("example.com", result.getHost()); assertEquals("/foo", result.getPath()); assertEquals("bar", result.getQuery()); assertEquals("baz", result.getFragment()); assertEquals("Invalid result URI", uri, result.toUri()); }