/** * A variant of {@link #encode()} with a charset other than "UTF-8". * @param charset the charset to use for encoding * @see UriComponentsBuilder#encode(Charset) */ public abstract UriComponents encode(Charset charset);
private URI createUri(UriComponents uric) { if (encodingMode.equals(EncodingMode.URI_COMPONENT)) { uric = uric.encode(); } return URI.create(uric.toString()); } }
@Override public ListenableFuture<WebSocketSession> doHandshake(WebSocketHandler webSocketHandler, String uriTemplate, Object... uriVars) { Assert.notNull(uriTemplate, "'uriTemplate' must not be null"); URI uri = UriComponentsBuilder.fromUriString(uriTemplate).buildAndExpand(uriVars).encode().toUri(); return doHandshake(webSocketHandler, null, uri); }
@Override public ListenableFuture<WebSocketSession> doHandshake( WebSocketHandler handler, String uriTemplate, Object... uriVars) { Assert.notNull(uriTemplate, "uriTemplate must not be null"); URI uri = UriComponentsBuilder.fromUriString(uriTemplate).buildAndExpand(uriVars).encode().toUri(); return doHandshake(handler, null, uri); }
private URI getUrlToUse() { MultiValueMap<String, String> params = this.queryParamsBuilder.buildAndExpand().encode().getQueryParams(); if (!params.isEmpty()) { return UriComponentsBuilder.fromUri(this.url).queryParams(params).build(true).toUri(); } return this.url; } }
/** * Asserts the request was redirected to the given URL template. * <p>This method accepts exact matches against the expanded and encoded URL template. * @param urlTemplate a URL template; the expanded URL will be encoded * @param uriVars zero or more URI variables to populate the template * @see UriComponentsBuilder#fromUriString(String) */ public static ResultMatcher redirectedUrlTemplate(String urlTemplate, Object... uriVars) { String uri = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(uriVars).encode().toUriString(); return redirectedUrl(uri); }
protected UriComponents expandAndEncode(UriComponentsBuilder builder, Object[] uriVariables) { if (!isStrictEncoding()) { return builder.buildAndExpand(uriVariables).encode(); } else { Object[] encodedUriVars = UriUtils.encodeUriVariables(uriVariables); return builder.buildAndExpand(encodedUriVars); } }
/** * Asserts the request was forwarded to the given URL template. * <p>This method accepts exact matches against the expanded and encoded URL template. * @param urlTemplate a URL template; the expanded URL will be encoded * @param uriVars zero or more URI variables to populate the template * @see UriComponentsBuilder#fromUriString(String) */ public static ResultMatcher forwardedUrlTemplate(String urlTemplate, Object... uriVars) { String uri = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(uriVars).encode().toUriString(); return forwardedUrl(uri); }
@Test public void toUriWithIpv6HostAlreadyEncoded() throws URISyntaxException { UriComponents uriComponents = UriComponentsBuilder.fromUriString( "http://[1abc:2abc:3abc::5ABC:6abc]:8080/hotel%20list/Z%C3%BCrich").build(true); UriComponents encoded = uriComponents.encode(); assertEquals(new URI("http://[1abc:2abc:3abc::5ABC:6abc]:8080/hotel%20list/Z%C3%BCrich"), encoded.toUri()); }
@Test public void toUriEncoded() throws URISyntaxException { UriComponents uriComponents = UriComponentsBuilder.fromUriString( "http://example.com/hotel list/Z\u00fcrich").build(); assertEquals(new URI("http://example.com/hotel%20list/Z%C3%BCrich"), uriComponents.encode().toUri()); }
@Override public ListenableFuture<WebSocketSession> doHandshake(WebSocketHandler handler, String uriTemplate, Object... uriVars) { URI uri = UriComponentsBuilder.fromUriString(uriTemplate).buildAndExpand(uriVars).encode().toUri(); return doHandshake(handler, null, uri); }
@Test // SPR-16710 public void fromMethodNameWithStringReturnType() { UriComponents uriComponents = fromMethodName( BookingControllerWithString.class, "getBooking", 21L).buildAndExpand(42); assertEquals("http://localhost/hotels/42/bookings/21", uriComponents.encode().toUri().toString()); }
@Test // SPR-16710 public void fromMethodCallWithObjectReturnType() { UriComponents uriComponents = fromMethodCall( on(BookingControllerWithObject.class).getBooking(21L)).buildAndExpand(42); assertEquals("http://localhost/hotels/42/bookings/21", uriComponents.encode().toUri().toString()); }
@Test(expected = IllegalStateException.class) // SPR-16710 public void fromMethodCallWithStringReturnType() { UriComponents uriComponents = fromMethodCall( on(BookingControllerWithString.class).getBooking(21L)).buildAndExpand(42); assertEquals("http://localhost/hotels/42/bookings/21", uriComponents.encode().toUri().toString()); }
@Test // SPR-16710 public void fromMethodCallWithModelAndViewReturnType() { UriComponents uriComponents = fromMethodCall( on(BookingControllerWithModelAndView.class).getBooking(21L)).buildAndExpand(42); assertEquals("http://localhost/hotels/42/bookings/21", uriComponents.encode().toUri().toString()); }
@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 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 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 expandAndEncode() { UriComponents uri = UriComponentsBuilder .fromPath("/hotel list/{city} specials").queryParam("q", "{value}").build() .expand("Z\u00fcrich", "a+b").encode(); assertEquals("/hotel%20list/Z%C3%BCrich%20specials?q=a+b", uri.toString()); }
@Test public void test() throws Exception { String id = "a/b"; URI url = UriComponentsBuilder.fromUriString("/circuit").pathSegment(id).build().encode().toUri(); ResultActions result = mockMvc.perform(get(url)); result.andExpect(status().isOk()).andExpect(model().attribute("receivedId", is(id))); }