1
1
/*
2
- * Copyright 2002-2019 the original author or authors.
2
+ * Copyright 2002-2021 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
+
16
17
package org .springframework .http .server ;
17
18
18
- import java .util .Arrays ;
19
- import java .util .Collections ;
20
- import java .util .List ;
21
- import java .util .stream .Collectors ;
19
+ import java .util .stream .Stream ;
22
20
23
21
import org .junit .jupiter .api .Test ;
24
22
30
28
31
29
/**
32
30
* Unit tests for {@link DefaultPathContainer}.
31
+ *
33
32
* @author Rossen Stoyanchev
33
+ * @author Sam Brannen
34
34
*/
35
- public class DefaultPathContainerTests {
35
+ class DefaultPathContainerTests {
36
36
37
37
@ Test
38
- public void pathSegment () {
38
+ void pathSegment () {
39
39
// basic
40
40
testPathSegment ("cars" , "cars" , new LinkedMultiValueMap <>());
41
41
@@ -48,7 +48,7 @@ public void pathSegment() {
48
48
}
49
49
50
50
@ Test
51
- public void pathSegmentParams () throws Exception {
51
+ void pathSegmentParams () {
52
52
// basic
53
53
LinkedMultiValueMap <String , String > params = new LinkedMultiValueMap <>();
54
54
params .add ("colors" , "red" );
@@ -74,15 +74,14 @@ public void pathSegmentParams() throws Exception {
74
74
}
75
75
76
76
private void testPathSegment (String rawValue , String valueToMatch , MultiValueMap <String , String > params ) {
77
-
78
77
PathContainer container = PathContainer .parsePath (rawValue );
79
78
80
79
if ("" .equals (rawValue )) {
81
- assertThat (container .elements (). size ()). isEqualTo ( 0 );
80
+ assertThat (container .elements ()). isEmpty ( );
82
81
return ;
83
82
}
84
83
85
- assertThat (container .elements (). size ()). isEqualTo (1 );
84
+ assertThat (container .elements ()). hasSize (1 );
86
85
PathSegment segment = (PathSegment ) container .elements ().get (0 );
87
86
88
87
assertThat (segment .value ()).as ("value: '" + rawValue + "'" ).isEqualTo (rawValue );
@@ -91,40 +90,36 @@ private void testPathSegment(String rawValue, String valueToMatch, MultiValueMap
91
90
}
92
91
93
92
@ Test
94
- public void path () {
93
+ void path () {
95
94
// basic
96
- testPath ("/a/b/c" , "/a/b/c" , Arrays . asList ( "/" , "a" , "/" , "b" , "/" , "c" ) );
95
+ testPath ("/a/b/c" , "/a/b/c" , "/" , "a" , "/" , "b" , "/" , "c" );
97
96
98
97
// root path
99
- testPath ("/" , "/" , Collections . singletonList ( "/" ) );
98
+ testPath ("/" , "/" , "/" );
100
99
101
100
// empty path
102
- testPath ("" , "" , Collections . emptyList () );
103
- testPath ("%20%20" , "%20%20" , Collections . singletonList ( "%20%20" ) );
101
+ testPath ("" , "" );
102
+ testPath ("%20%20" , "%20%20" , "%20%20" );
104
103
105
104
// trailing slash
106
- testPath ("/a/b/" , "/a/b/" , Arrays . asList ( "/" , "a" , "/" , "b" , "/" ) );
107
- testPath ("/a/b//" , "/a/b//" , Arrays . asList ( "/" , "a" , "/" , "b" , "/" , "/" ) );
105
+ testPath ("/a/b/" , "/a/b/" , "/" , "a" , "/" , "b" , "/" );
106
+ testPath ("/a/b//" , "/a/b//" , "/" , "a" , "/" , "b" , "/" , "/" );
108
107
109
108
// extra slashes and spaces
110
- testPath ("/%20" , "/%20" , Arrays . asList ( "/" , "%20" ) );
111
- testPath ("//%20/%20" , "//%20/%20" , Arrays . asList ( "/" , "/" , "%20" , "/" , "%20" ) );
109
+ testPath ("/%20" , "/%20" , "/" , "%20" );
110
+ testPath ("//%20/%20" , "//%20/%20" , "/" , "/" , "%20" , "/" , "%20" );
112
111
}
113
112
114
- private void testPath (String input , PathContainer . Options options , String value , List < String > expectedElements ) {
115
- PathContainer path = PathContainer .parsePath (input , options );
113
+ private void testPath (String input , String value , String ... expectedElements ) {
114
+ PathContainer path = PathContainer .parsePath (input , PathContainer . Options . HTTP_PATH );
116
115
117
116
assertThat (path .value ()).as ("value: '" + input + "'" ).isEqualTo (value );
118
- assertThat (path .elements ().stream ().map (PathContainer .Element ::value ).collect (Collectors .toList ()))
119
- .as ("elements: " + input ).isEqualTo (expectedElements );
120
- }
121
-
122
- private void testPath (String input , String value , List <String > expectedElements ) {
123
- testPath (input , PathContainer .Options .HTTP_PATH , value , expectedElements );
117
+ assertThat (path .elements ()).map (PathContainer .Element ::value ).as ("elements: " + input )
118
+ .containsExactly (expectedElements );
124
119
}
125
120
126
121
@ Test
127
- public void subPath () {
122
+ void subPath () {
128
123
// basic
129
124
PathContainer path = PathContainer .parsePath ("/a/b/c" );
130
125
assertThat (path .subPath (0 )).isSameAs (path );
@@ -141,15 +136,15 @@ public void subPath() {
141
136
}
142
137
143
138
@ Test // gh-23310
144
- public void pathWithCustomSeparator () {
139
+ void pathWithCustomSeparator () {
145
140
PathContainer path = PathContainer .parsePath ("a.b%2Eb.c" , PathContainer .Options .MESSAGE_ROUTE );
146
141
147
- List <String > decodedSegments = path .elements ().stream ()
148
- .filter (e -> e instanceof PathSegment )
149
- .map (e -> (( PathSegment ) e ). valueToMatch () )
150
- .collect ( Collectors . toList () );
142
+ Stream <String > decodedSegments = path .elements ().stream ()
143
+ .filter (PathSegment . class :: isInstance )
144
+ .map (PathSegment . class :: cast )
145
+ .map ( PathSegment :: valueToMatch );
151
146
152
- assertThat (decodedSegments ).isEqualTo ( Arrays . asList ( "a" , "b.b" , "c" ) );
147
+ assertThat (decodedSegments ).containsExactly ( "a" , "b.b" , "c" );
153
148
}
154
149
155
150
}
0 commit comments