Polishing

This commit is contained in:
Sam Brannen 2022-05-25 11:20:57 +02:00
parent bde0931e51
commit aa06a09dee
2 changed files with 24 additions and 25 deletions

View File

@ -1321,7 +1321,7 @@ class UriComponentsBuilderTests {
.isInstanceOf(NumberFormatException.class); .isInstanceOf(NumberFormatException.class);
} }
@Test // gh-27039 @Test // gh-27039
void expandPortAndPathWithoutSeparator() { void expandPortAndPathWithoutSeparator() {
URI uri = UriComponentsBuilder URI uri = UriComponentsBuilder
.fromUriString("ws://localhost:{port}{path}") .fromUriString("ws://localhost:{port}{path}")
@ -1330,5 +1330,4 @@ class UriComponentsBuilderTests {
assertThat(uri.toString()).isEqualTo("ws://localhost:7777/test"); assertThat(uri.toString()).isEqualTo("ws://localhost:7777/test");
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2020 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -39,10 +39,10 @@ import static org.springframework.web.util.UriComponentsBuilder.fromUriString;
* @author Phillip Webb * @author Phillip Webb
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
*/ */
public class UriComponentsTests { class UriComponentsTests {
@Test @Test
public void expandAndEncode() { void expandAndEncode() {
UriComponents uri = UriComponentsBuilder UriComponents uri = UriComponentsBuilder
.fromPath("/hotel list/{city} specials").queryParam("q", "{value}").build() .fromPath("/hotel list/{city} specials").queryParam("q", "{value}").build()
.expand("Z\u00fcrich", "a+b").encode(); .expand("Z\u00fcrich", "a+b").encode();
@ -51,7 +51,7 @@ public class UriComponentsTests {
} }
@Test @Test
public void encodeAndExpand() { void encodeAndExpand() {
UriComponents uri = UriComponentsBuilder UriComponents uri = UriComponentsBuilder
.fromPath("/hotel list/{city} specials").queryParam("q", "{value}").encode().build() .fromPath("/hotel list/{city} specials").queryParam("q", "{value}").encode().build()
.expand("Z\u00fcrich", "a+b"); .expand("Z\u00fcrich", "a+b");
@ -60,7 +60,7 @@ public class UriComponentsTests {
} }
@Test @Test
public void encodeAndExpandPartially() { void encodeAndExpandPartially() {
UriComponents uri = UriComponentsBuilder UriComponents uri = UriComponentsBuilder
.fromPath("/hotel list/{city} specials").queryParam("q", "{value}").encode() .fromPath("/hotel list/{city} specials").queryParam("q", "{value}").encode()
.uriVariables(Collections.singletonMap("city", "Z\u00fcrich")).build(); .uriVariables(Collections.singletonMap("city", "Z\u00fcrich")).build();
@ -69,31 +69,31 @@ public class UriComponentsTests {
} }
@Test // SPR-17168 @Test // SPR-17168
public void encodeAndExpandWithDollarSign() { void encodeAndExpandWithDollarSign() {
UriComponents uri = UriComponentsBuilder.fromPath("/path").queryParam("q", "{value}").encode().build(); UriComponents uri = UriComponentsBuilder.fromPath("/path").queryParam("q", "{value}").encode().build();
assertThat(uri.expand("JavaClass$1.class").toString()).isEqualTo("/path?q=JavaClass%241.class"); assertThat(uri.expand("JavaClass$1.class").toString()).isEqualTo("/path?q=JavaClass%241.class");
} }
@Test @Test
public void toUriEncoded() throws URISyntaxException { void toUriEncoded() throws URISyntaxException {
UriComponents uri = UriComponentsBuilder.fromUriString("https://example.com/hotel list/Z\u00fcrich").build(); UriComponents uri = UriComponentsBuilder.fromUriString("https://example.com/hotel list/Z\u00fcrich").build();
assertThat(uri.encode().toUri()).isEqualTo(new URI("https://example.com/hotel%20list/Z%C3%BCrich")); assertThat(uri.encode().toUri()).isEqualTo(new URI("https://example.com/hotel%20list/Z%C3%BCrich"));
} }
@Test @Test
public void toUriNotEncoded() throws URISyntaxException { void toUriNotEncoded() throws URISyntaxException {
UriComponents uri = UriComponentsBuilder.fromUriString("https://example.com/hotel list/Z\u00fcrich").build(); UriComponents uri = UriComponentsBuilder.fromUriString("https://example.com/hotel list/Z\u00fcrich").build();
assertThat(uri.toUri()).isEqualTo(new URI("https://example.com/hotel%20list/Z\u00fcrich")); assertThat(uri.toUri()).isEqualTo(new URI("https://example.com/hotel%20list/Z\u00fcrich"));
} }
@Test @Test
public void toUriAlreadyEncoded() throws URISyntaxException { void toUriAlreadyEncoded() throws URISyntaxException {
UriComponents uri = UriComponentsBuilder.fromUriString("https://example.com/hotel%20list/Z%C3%BCrich").build(true); UriComponents uri = UriComponentsBuilder.fromUriString("https://example.com/hotel%20list/Z%C3%BCrich").build(true);
assertThat(uri.encode().toUri()).isEqualTo(new URI("https://example.com/hotel%20list/Z%C3%BCrich")); assertThat(uri.encode().toUri()).isEqualTo(new URI("https://example.com/hotel%20list/Z%C3%BCrich"));
} }
@Test @Test
public void toUriWithIpv6HostAlreadyEncoded() throws URISyntaxException { void toUriWithIpv6HostAlreadyEncoded() throws URISyntaxException {
UriComponents uri = UriComponentsBuilder.fromUriString( UriComponents uri = UriComponentsBuilder.fromUriString(
"http://[1abc:2abc:3abc::5ABC:6abc]:8080/hotel%20list/Z%C3%BCrich").build(true); "http://[1abc:2abc:3abc::5ABC:6abc]:8080/hotel%20list/Z%C3%BCrich").build(true);
@ -102,7 +102,7 @@ public class UriComponentsTests {
} }
@Test @Test
public void expand() { void expand() {
UriComponents uri = UriComponentsBuilder.fromUriString("https://example.com").path("/{foo} {bar}").build(); UriComponents uri = UriComponentsBuilder.fromUriString("https://example.com").path("/{foo} {bar}").build();
uri = uri.expand("1 2", "3 4"); uri = uri.expand("1 2", "3 4");
@ -111,7 +111,7 @@ public class UriComponentsTests {
} }
@Test // SPR-13311 @Test // SPR-13311
public void expandWithRegexVar() { void expandWithRegexVar() {
String template = "/myurl/{name:[a-z]{1,5}}/show"; String template = "/myurl/{name:[a-z]{1,5}}/show";
UriComponents uri = UriComponentsBuilder.fromUriString(template).build(); UriComponents uri = UriComponentsBuilder.fromUriString(template).build();
uri = uri.expand(Collections.singletonMap("name", "test")); uri = uri.expand(Collections.singletonMap("name", "test"));
@ -120,13 +120,13 @@ public class UriComponentsTests {
} }
@Test // SPR-17630 @Test // SPR-17630
public void uirTemplateExpandWithMismatchedCurlyBraces() { void uirTemplateExpandWithMismatchedCurlyBraces() {
UriComponents uri = UriComponentsBuilder.fromUriString("/myurl/?q={{{{").encode().build(); UriComponents uri = UriComponentsBuilder.fromUriString("/myurl/?q={{{{").encode().build();
assertThat(uri.toUriString()).isEqualTo("/myurl/?q=%7B%7B%7B%7B"); assertThat(uri.toUriString()).isEqualTo("/myurl/?q=%7B%7B%7B%7B");
} }
@Test // gh-22447 @Test // gh-22447
public void expandWithFragmentOrder() { void expandWithFragmentOrder() {
UriComponents uri = UriComponentsBuilder UriComponents uri = UriComponentsBuilder
.fromUriString("https://{host}/{path}#{fragment}").build() .fromUriString("https://{host}/{path}#{fragment}").build()
.expand("example.com", "foo", "bar"); .expand("example.com", "foo", "bar");
@ -135,7 +135,7 @@ public class UriComponentsTests {
} }
@Test // SPR-12123 @Test // SPR-12123
public void port() { void port() {
UriComponents uri1 = fromUriString("https://example.com:8080/bar").build(); UriComponents uri1 = fromUriString("https://example.com:8080/bar").build();
UriComponents uri2 = fromUriString("https://example.com/bar").port(8080).build(); UriComponents uri2 = fromUriString("https://example.com/bar").port(8080).build();
UriComponents uri3 = fromUriString("https://example.com/bar").port("{port}").build().expand(8080); UriComponents uri3 = fromUriString("https://example.com/bar").port("{port}").build().expand(8080);
@ -152,31 +152,31 @@ public class UriComponentsTests {
} }
@Test @Test
public void expandEncoded() { void expandEncoded() {
assertThatIllegalStateException().isThrownBy(() -> assertThatIllegalStateException().isThrownBy(() ->
UriComponentsBuilder.fromPath("/{foo}").build().encode().expand("bar")); UriComponentsBuilder.fromPath("/{foo}").build().encode().expand("bar"));
} }
@Test @Test
public void invalidCharacters() { void invalidCharacters() {
assertThatIllegalArgumentException().isThrownBy(() -> assertThatIllegalArgumentException().isThrownBy(() ->
UriComponentsBuilder.fromPath("/{foo}").build(true)); UriComponentsBuilder.fromPath("/{foo}").build(true));
} }
@Test @Test
public void invalidEncodedSequence() { void invalidEncodedSequence() {
assertThatIllegalArgumentException().isThrownBy(() -> assertThatIllegalArgumentException().isThrownBy(() ->
UriComponentsBuilder.fromPath("/fo%2o").build(true)); UriComponentsBuilder.fromPath("/fo%2o").build(true));
} }
@Test @Test
public void normalize() { void normalize() {
UriComponents uri = UriComponentsBuilder.fromUriString("https://example.com/foo/../bar").build(); UriComponents uri = UriComponentsBuilder.fromUriString("https://example.com/foo/../bar").build();
assertThat(uri.normalize().toString()).isEqualTo("https://example.com/bar"); assertThat(uri.normalize().toString()).isEqualTo("https://example.com/bar");
} }
@Test @Test
public void serializable() throws Exception { void serializable() throws Exception {
UriComponents uri = UriComponentsBuilder.fromUriString( UriComponents uri = UriComponentsBuilder.fromUriString(
"https://example.com").path("/{foo}").query("bar={baz}").build(); "https://example.com").path("/{foo}").query("bar={baz}").build();
@ -190,7 +190,7 @@ public class UriComponentsTests {
} }
@Test @Test
public void copyToUriComponentsBuilder() { void copyToUriComponentsBuilder() {
UriComponents source = UriComponentsBuilder.fromPath("/foo/bar").pathSegment("ba/z").build(); UriComponents source = UriComponentsBuilder.fromPath("/foo/bar").pathSegment("ba/z").build();
UriComponentsBuilder targetBuilder = UriComponentsBuilder.newInstance(); UriComponentsBuilder targetBuilder = UriComponentsBuilder.newInstance();
source.copyToUriComponentsBuilder(targetBuilder); source.copyToUriComponentsBuilder(targetBuilder);
@ -201,7 +201,7 @@ public class UriComponentsTests {
} }
@Test @Test
public void equalsHierarchicalUriComponents() { void equalsHierarchicalUriComponents() {
String url = "https://example.com"; String url = "https://example.com";
UriComponents uric1 = UriComponentsBuilder.fromUriString(url).path("/{foo}").query("bar={baz}").build(); UriComponents uric1 = UriComponentsBuilder.fromUriString(url).path("/{foo}").query("bar={baz}").build();
UriComponents uric2 = UriComponentsBuilder.fromUriString(url).path("/{foo}").query("bar={baz}").build(); UriComponents uric2 = UriComponentsBuilder.fromUriString(url).path("/{foo}").query("bar={baz}").build();
@ -214,7 +214,7 @@ public class UriComponentsTests {
} }
@Test @Test
public void equalsOpaqueUriComponents() { void equalsOpaqueUriComponents() {
String baseUrl = "http:example.com"; String baseUrl = "http:example.com";
UriComponents uric1 = UriComponentsBuilder.fromUriString(baseUrl + "/foo/bar").build(); UriComponents uric1 = UriComponentsBuilder.fromUriString(baseUrl + "/foo/bar").build();
UriComponents uric2 = UriComponentsBuilder.fromUriString(baseUrl + "/foo/bar").build(); UriComponents uric2 = UriComponentsBuilder.fromUriString(baseUrl + "/foo/bar").build();