16
16
17
17
package org .springframework .http .client ;
18
18
19
+ import java .io .IOException ;
19
20
import java .net .URI ;
20
- import java .net .http .HttpClient ;
21
- import java .time .Duration ;
22
- import java .util .concurrent .Executor ;
23
21
22
+ import org .junit .jupiter .api .AfterAll ;
23
+ import org .junit .jupiter .api .BeforeAll ;
24
24
import org .junit .jupiter .api .Test ;
25
25
26
26
import org .springframework .http .HttpMethod ;
27
+ import org .springframework .http .HttpStatusCode ;
28
+ import org .springframework .lang .Nullable ;
27
29
28
30
import static org .assertj .core .api .Assertions .assertThat ;
29
31
32
34
*/
33
35
public class JdkClientHttpRequestFactoryTests extends AbstractHttpRequestFactoryTests {
34
36
37
+ @ Nullable
38
+ private static String originalPropertyValue ;
39
+
40
+ @ BeforeAll
41
+ public static void setProperty () {
42
+ originalPropertyValue = System .getProperty ("jdk.httpclient.allowRestrictedHeaders" );
43
+ System .setProperty ("jdk.httpclient.allowRestrictedHeaders" , "expect" );
44
+ }
45
+
46
+ @ AfterAll
47
+ public static void restoreProperty () {
48
+ if (originalPropertyValue != null ) {
49
+ System .setProperty ("jdk.httpclient.allowRestrictedHeaders" , originalPropertyValue );
50
+ }
51
+ else {
52
+ System .clearProperty ("jdk.httpclient.allowRestrictedHeaders" );
53
+ }
54
+ }
55
+
35
56
@ Override
36
57
protected ClientHttpRequestFactory createRequestFactory () {
37
58
return new JdkClientHttpRequestFactory ();
@@ -45,25 +66,13 @@ public void httpMethods() throws Exception {
45
66
}
46
67
47
68
@ Test
48
- public void customizeDisallowedHeaders () {
49
- String original = System . getProperty ( "jdk.httpclient.allowRestrictedHeaders" );
50
- System . setProperty ( "jdk.httpclient.allowRestrictedHeaders" , "host " );
69
+ public void customizeDisallowedHeaders () throws IOException {
70
+ ClientHttpRequest request = factory . createRequest ( URI . create ( this . baseUrl + "/status/299" ), HttpMethod . PUT );
71
+ request . getHeaders (). set ( "Expect" , "299 " );
51
72
52
- assertThat (TestJdkClientHttpRequest .DISALLOWED_HEADERS ).doesNotContain ("host" );
53
-
54
- if (original != null ) {
55
- System .setProperty ("jdk.httpclient.allowRestrictedHeaders" , original );
56
- }
57
- else {
58
- System .clearProperty ("jdk.httpclient.allowRestrictedHeaders" );
59
- }
60
- }
61
-
62
- static class TestJdkClientHttpRequest extends JdkClientHttpRequest {
63
-
64
- public TestJdkClientHttpRequest (HttpClient httpClient , URI uri , HttpMethod method , Executor executor , Duration readTimeout ) {
65
- super (httpClient , uri , method , executor , readTimeout );
66
- }
73
+ try (ClientHttpResponse response = request .execute ()) {
74
+ assertThat (response .getStatusCode ()).as ("Invalid status code" ).isEqualTo (HttpStatusCode .valueOf (299 ));
75
+ }
67
76
}
68
77
69
78
}
0 commit comments