/** * Constructor with a base URI. * <p>The given URI template is parsed via * {@link UriComponentsBuilder#fromUriString} and then applied as a base URI * to every UriBuilder via {@link UriComponentsBuilder#uriComponents} unless * the UriBuilder itself was created with a URI template that already has a * target address. * @param baseUriTemplate the URI template to use a base URL */ public DefaultUriBuilderFactory(String baseUriTemplate) { this.baseUri = UriComponentsBuilder.fromUriString(baseUriTemplate); }
@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); }
/** * 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); }
@Test // SPR-17256 public void uriComponentsWithMergedQueryParams() { String uri = UriComponentsBuilder.fromUriString("http://localhost:8081") .uriComponents(UriComponentsBuilder.fromUriString("/{path}?sort={sort}").build()) .queryParam("sort", "another_value").build().toString(); assertEquals("http://localhost:8081/{path}?sort={sort}&sort=another_value", uri); }
@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 public void parsesEmptyFragment() { UriComponents components = UriComponentsBuilder.fromUriString("/example#").build(); assertThat(components.getFragment(), is(nullValue())); assertThat(components.toString(), equalTo("/example")); }
@Test public void buildAndExpandOpaque() { UriComponents result = UriComponentsBuilder.fromUriString("mailto:{user}@{domain}") .buildAndExpand("foo", "example.com"); assertEquals("mailto:foo@example.com", result.toUriString()); Map<String, String> values = new HashMap<>(); values.put("user", "foo"); values.put("domain", "example.com"); UriComponentsBuilder.fromUriString("mailto:{user}@{domain}").buildAndExpand(values); assertEquals("mailto:foo@example.com", result.toUriString()); }
@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 // SPR-9317 public void fromUriEncodedQuery() throws URISyntaxException { URI uri = new URI("http://www.example.org/?param=aGVsbG9Xb3JsZA%3D%3D"); String fromUri = UriComponentsBuilder.fromUri(uri).build().getQueryParams().get("param").get(0); String fromUriString = UriComponentsBuilder.fromUriString(uri.toString()) .build().getQueryParams().get("param").get(0); assertEquals(fromUri, fromUriString); }
@Test public void replacePath() { UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("http://www.ietf.org/rfc/rfc2396.txt"); builder.replacePath("/rfc/rfc3986.txt"); UriComponents result = builder.build(); assertEquals("http://www.ietf.org/rfc/rfc3986.txt", result.toUriString()); builder = UriComponentsBuilder.fromUriString("http://www.ietf.org/rfc/rfc2396.txt"); builder.replacePath(null); result = builder.build(); assertEquals("http://www.ietf.org", result.toUriString()); }
@Test public void queryParamWithoutValueWithoutEquals() { UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://example.com/foo?bar").build(); assertThat(uriComponents.toUriString(), equalTo("http://example.com/foo?bar")); // TODO [SPR-13537] Change equalTo(null) to equalTo(""). assertThat(uriComponents.getQueryParams().get("bar").get(0), equalTo(null)); }
@Test // SPR-17630 public void toUriStringWithCurlyBraces() { assertEquals("/path?q=%7Basa%7Dasa", UriComponentsBuilder.fromUriString("/path?q={asa}asa").toUriString()); } }
@Test public void normalize() { UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://example.com/foo/../bar").build(); assertEquals("http://example.com/bar", uriComponents.normalize().toString()); }
@Test public void fromMethodNameWithCustomBaseUrlViaStaticCall() { UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("http://example.org:9090/base"); UriComponents uriComponents = fromMethodName(builder, ControllerWithMethods.class, "methodWithPathVariable", "1").build(); assertEquals("http://example.org:9090/base/something/1/foo", uriComponents.toString()); assertEquals("http://example.org:9090/base", builder.toUriString()); }
@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))); }
@Test public void fromControllerWithCustomBaseUrlViaInstance() { UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("http://example.org:9090/base"); MvcUriComponentsBuilder mvcBuilder = relativeTo(builder); UriComponents uriComponents = mvcBuilder.withController(PersonControllerImpl.class).build(); assertEquals("http://example.org:9090/base/people", uriComponents.toString()); assertEquals("http://example.org:9090/base", builder.toUriString()); }
@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 fromMethodCallWithCustomBaseUrlViaStaticCall() { UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("http://example.org:9090/base"); UriComponents uriComponents = fromMethodCall(builder, on(ControllerWithMethods.class).myMethod(null)).build(); assertEquals("http://example.org:9090/base/something/else", uriComponents.toString()); assertEquals("http://example.org:9090/base", builder.toUriString()); }
@Test // SPR-11970 public void fromUriStringNoPathWithReservedCharInQuery() { UriComponents result = UriComponentsBuilder.fromUriString("http://example.com?foo=bar@baz").build(); assertTrue(StringUtils.isEmpty(result.getUserInfo())); assertEquals("example.com", result.getHost()); assertTrue(result.getQueryParams().containsKey("foo")); assertEquals("bar@baz", result.getQueryParams().getFirst("foo")); }