20
20
21
21
import org .junit .jupiter .api .Test ;
22
22
23
+ import org .springframework .http .server .PathContainer .Element ;
24
+ import org .springframework .http .server .PathContainer .Options ;
23
25
import org .springframework .http .server .PathContainer .PathSegment ;
24
26
import org .springframework .util .LinkedMultiValueMap ;
25
27
import org .springframework .util .MultiValueMap ;
26
28
27
29
import static org .assertj .core .api .Assertions .assertThat ;
30
+ import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
28
31
29
32
/**
30
33
* Unit tests for {@link DefaultPathContainer}.
@@ -37,42 +40,66 @@ class DefaultPathContainerTests {
37
40
@ Test
38
41
void pathSegment () {
39
42
// basic
40
- testPathSegment ("cars" , "cars" , new LinkedMultiValueMap <> ());
43
+ testPathSegment ("cars" , "cars" , emptyMap ());
41
44
42
45
// empty
43
- testPathSegment ("" , "" , new LinkedMultiValueMap <> ());
46
+ testPathSegment ("" , "" , emptyMap ());
44
47
45
48
// spaces
46
- testPathSegment ("%20%20" , " " , new LinkedMultiValueMap <> ());
47
- testPathSegment ("%20a%20" , " a " , new LinkedMultiValueMap <> ());
49
+ testPathSegment ("%20%20" , " " , emptyMap ());
50
+ testPathSegment ("%20a%20" , " a " , emptyMap ());
48
51
}
49
52
50
53
@ Test
51
54
void pathSegmentParams () {
52
55
// basic
53
- LinkedMultiValueMap <String , String > params = new LinkedMultiValueMap <> ();
56
+ LinkedMultiValueMap <String , String > params = emptyMap ();
54
57
params .add ("colors" , "red" );
55
58
params .add ("colors" , "blue" );
56
59
params .add ("colors" , "green" );
57
60
params .add ("year" , "2012" );
58
61
testPathSegment ("cars;colors=red,blue,green;year=2012" , "cars" , params );
59
62
60
63
// trailing semicolon
61
- params = new LinkedMultiValueMap <> ();
64
+ params = emptyMap ();
62
65
params .add ("p" , "1" );
63
66
testPathSegment ("path;p=1;" , "path" , params );
64
67
65
68
// params with spaces
66
- params = new LinkedMultiValueMap <> ();
69
+ params = emptyMap ();
67
70
params .add ("param name" , "param value" );
68
71
testPathSegment ("path;param%20name=param%20value;%20" , "path" , params );
69
72
70
73
// empty params
71
- params = new LinkedMultiValueMap <> ();
74
+ params = emptyMap ();
72
75
params .add ("p" , "1" );
73
76
testPathSegment ("path;;;%20;%20;p=1;%20" , "path" , params );
74
77
}
75
78
79
+ @ Test
80
+ void pathSegmentParamsAreImmutable () {
81
+ assertPathSegmentParamsAreImmutable ("cars" , emptyMap (), Options .HTTP_PATH );
82
+
83
+ LinkedMultiValueMap <String , String > params = emptyMap ();
84
+ params .add ("colors" , "red" );
85
+ params .add ("colors" , "blue" );
86
+ params .add ("colors" , "green" );
87
+ assertPathSegmentParamsAreImmutable (";colors=red,blue,green" , params , Options .HTTP_PATH );
88
+
89
+ assertPathSegmentParamsAreImmutable (";colors=red,blue,green" , emptyMap (), Options .MESSAGE_ROUTE );
90
+ }
91
+
92
+ private void assertPathSegmentParamsAreImmutable (String path , LinkedMultiValueMap <String , String > params , Options options ) {
93
+ PathContainer container = PathContainer .parsePath (path , options );
94
+ assertThat (container .elements ()).hasSize (1 );
95
+
96
+ PathSegment segment = (PathSegment ) container .elements ().get (0 );
97
+ MultiValueMap <String , String > segmentParams = segment .parameters ();
98
+ assertThat (segmentParams ).isEqualTo (params );
99
+ assertThatExceptionOfType (UnsupportedOperationException .class )
100
+ .isThrownBy (() -> segmentParams .add ("enigma" , "boom" ));
101
+ }
102
+
76
103
private void testPathSegment (String rawValue , String valueToMatch , MultiValueMap <String , String > params ) {
77
104
PathContainer container = PathContainer .parsePath (rawValue );
78
105
@@ -111,10 +138,10 @@ void path() {
111
138
}
112
139
113
140
private void testPath (String input , String value , String ... expectedElements ) {
114
- PathContainer path = PathContainer .parsePath (input , PathContainer . Options .HTTP_PATH );
141
+ PathContainer path = PathContainer .parsePath (input , Options .HTTP_PATH );
115
142
116
143
assertThat (path .value ()).as ("value: '" + input + "'" ).isEqualTo (value );
117
- assertThat (path .elements ()).map (PathContainer . Element ::value ).as ("elements: " + input )
144
+ assertThat (path .elements ()).map (Element ::value ).as ("elements: " + input )
118
145
.containsExactly (expectedElements );
119
146
}
120
147
@@ -137,7 +164,7 @@ void subPath() {
137
164
138
165
@ Test // gh-23310
139
166
void pathWithCustomSeparator () {
140
- PathContainer path = PathContainer .parsePath ("a.b%2Eb.c" , PathContainer . Options .MESSAGE_ROUTE );
167
+ PathContainer path = PathContainer .parsePath ("a.b%2Eb.c" , Options .MESSAGE_ROUTE );
141
168
142
169
Stream <String > decodedSegments = path .elements ().stream ()
143
170
.filter (PathSegment .class ::isInstance )
@@ -147,4 +174,8 @@ void pathWithCustomSeparator() {
147
174
assertThat (decodedSegments ).containsExactly ("a" , "b.b" , "c" );
148
175
}
149
176
177
+ private static LinkedMultiValueMap <String , String > emptyMap () {
178
+ return new LinkedMultiValueMap <>();
179
+ }
180
+
150
181
}
0 commit comments