1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.apache.hc.client5.http.impl;
28
29 import org.apache.hc.core5.http.HttpHeaders;
30 import org.apache.hc.core5.http.HttpResponse;
31 import org.apache.hc.core5.http.HttpStatus;
32 import org.apache.hc.core5.http.ProtocolException;
33 import org.apache.hc.core5.http.message.BasicHttpResponse;
34 import org.apache.hc.core5.http.ssl.TLS;
35 import org.junit.jupiter.api.Assertions;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38
39
40
41
42 class TestProtocolSwitchStrategy {
43
44 ProtocolSwitchStrategy switchStrategy;
45
46 @BeforeEach
47 void setUp() {
48 switchStrategy = new ProtocolSwitchStrategy();
49 }
50
51 @Test
52 void testSwitchToTLS() throws Exception {
53 final HttpResponse response1 = new BasicHttpResponse(HttpStatus.SC_SWITCHING_PROTOCOLS);
54 response1.addHeader(HttpHeaders.UPGRADE, "TLS");
55 Assertions.assertEquals(TLS.V_1_2.getVersion(), switchStrategy.switchProtocol(response1));
56
57 final HttpResponse response2 = new BasicHttpResponse(HttpStatus.SC_SWITCHING_PROTOCOLS);
58 response2.addHeader(HttpHeaders.UPGRADE, "TLS/1.3");
59 Assertions.assertEquals(TLS.V_1_3.getVersion(), switchStrategy.switchProtocol(response2));
60 }
61
62 @Test
63 void testSwitchToHTTP11AndTLS() throws Exception {
64 final HttpResponse response1 = new BasicHttpResponse(HttpStatus.SC_SWITCHING_PROTOCOLS);
65 response1.addHeader(HttpHeaders.UPGRADE, "TLS, HTTP/1.1");
66 Assertions.assertEquals(TLS.V_1_2.getVersion(), switchStrategy.switchProtocol(response1));
67
68 final HttpResponse response2 = new BasicHttpResponse(HttpStatus.SC_SWITCHING_PROTOCOLS);
69 response2.addHeader(HttpHeaders.UPGRADE, ",, HTTP/1.1, TLS, ");
70 Assertions.assertEquals(TLS.V_1_2.getVersion(), switchStrategy.switchProtocol(response2));
71
72 final HttpResponse response3 = new BasicHttpResponse(HttpStatus.SC_SWITCHING_PROTOCOLS);
73 response3.addHeader(HttpHeaders.UPGRADE, "HTTP/1.1");
74 response3.addHeader(HttpHeaders.UPGRADE, "TLS/1.2");
75 Assertions.assertEquals(TLS.V_1_2.getVersion(), switchStrategy.switchProtocol(response3));
76
77 final HttpResponse response4 = new BasicHttpResponse(HttpStatus.SC_SWITCHING_PROTOCOLS);
78 response4.addHeader(HttpHeaders.UPGRADE, "HTTP/1.1");
79 response4.addHeader(HttpHeaders.UPGRADE, "TLS/1.2, TLS/1.3");
80 Assertions.assertEquals(TLS.V_1_3.getVersion(), switchStrategy.switchProtocol(response4));
81 }
82
83 @Test
84 void testSwitchInvalid() {
85 final HttpResponse response1 = new BasicHttpResponse(HttpStatus.SC_SWITCHING_PROTOCOLS);
86 response1.addHeader(HttpHeaders.UPGRADE, "Crap");
87 Assertions.assertThrows(ProtocolException.class, () -> switchStrategy.switchProtocol(response1));
88
89 final HttpResponse response2 = new BasicHttpResponse(HttpStatus.SC_SWITCHING_PROTOCOLS);
90 response2.addHeader(HttpHeaders.UPGRADE, "TLS, huh?");
91 Assertions.assertThrows(ProtocolException.class, () -> switchStrategy.switchProtocol(response2));
92
93 final HttpResponse response3 = new BasicHttpResponse(HttpStatus.SC_SWITCHING_PROTOCOLS);
94 response3.addHeader(HttpHeaders.UPGRADE, ",,,");
95 Assertions.assertThrows(ProtocolException.class, () -> switchStrategy.switchProtocol(response3));
96 }
97
98 }