Browse Source

Fix UriComponentsBuilder examples in ref docs

Closes gh-26453
pull/26463/head
Sam Brannen 4 years ago
parent
commit
c5284009a1
  1. 23
      spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java
  2. 12
      src/docs/asciidoc/web/web-uris.adoc

23
spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -51,6 +51,27 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException @@ -51,6 +51,27 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
*/
class UriComponentsBuilderTests {
@Test // see gh-26453
void examplesInReferenceManual() {
final String expected = "/hotel%20list/New%20York?q=foo%2Bbar";
URI uri = UriComponentsBuilder.fromPath("/hotel list/{city}")
.queryParam("q", "{q}")
.encode()
.buildAndExpand("New York", "foo+bar")
.toUri();
assertThat(uri).asString().isEqualTo(expected);
uri = UriComponentsBuilder.fromPath("/hotel list/{city}")
.queryParam("q", "{q}")
.build("New York", "foo+bar");
assertThat(uri).asString().isEqualTo(expected);
uri = UriComponentsBuilder.fromUriString("/hotel list/{city}?q={q}")
.build("New York", "foo+bar");
assertThat(uri).asString().isEqualTo(expected);
}
@Test
void plain() throws URISyntaxException {
UriComponentsBuilder builder = UriComponentsBuilder.newInstance();

12
src/docs/asciidoc/web/web-uris.adoc

@ -82,7 +82,7 @@ as the following example shows: @@ -82,7 +82,7 @@ as the following example shows:
.build("Westin", "123")
----
You shorter it further still with a full URI template, as the following example shows:
You can shorten it further still with a full URI template, as the following example shows:
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
@ -94,7 +94,7 @@ You shorter it further still with a full URI template, as the following example @@ -94,7 +94,7 @@ You shorter it further still with a full URI template, as the following example
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
val uri = UriComponentsBuilder
val uri = UriComponentsBuilder
.fromUriString("https://example.com/hotels/{hotel}?q={q}")
.build("Westin", "123")
----
@ -250,7 +250,7 @@ as the following example shows: @@ -250,7 +250,7 @@ as the following example shows:
----
URI uri = UriComponentsBuilder.fromPath("/hotel list/{city}")
.queryParam("q", "{q}")
.build("New York", "foo+bar")
.build("New York", "foo+bar");
----
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
@ -265,13 +265,13 @@ You can shorten it further still with a full URI template, as the following exam @@ -265,13 +265,13 @@ You can shorten it further still with a full URI template, as the following exam
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
URI uri = UriComponentsBuilder.fromPath("/hotel list/{city}?q={q}")
.build("New York", "foo+bar")
URI uri = UriComponentsBuilder.fromUriString("/hotel list/{city}?q={q}")
.build("New York", "foo+bar");
----
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
val uri = UriComponentsBuilder.fromPath("/hotel list/{city}?q={q}")
val uri = UriComponentsBuilder.fromUriString("/hotel list/{city}?q={q}")
.build("New York", "foo+bar")
----

Loading…
Cancel
Save