3030import org .apache .hc .core5 .http .HttpResponse ;
3131import org .apache .hc .core5 .http .HttpStatus ;
3232import org .apache .hc .core5 .http .ProtocolException ;
33+ import org .apache .hc .core5 .http .ProtocolVersion ;
3334import org .apache .hc .core5 .http .message .BasicHttpResponse ;
3435import org .apache .hc .core5 .http .ssl .TLS ;
3536import org .junit .jupiter .api .Assertions ;
3637import org .junit .jupiter .api .BeforeEach ;
3738import org .junit .jupiter .api .Test ;
3839
3940/**
40- * Simple tests for {@link DefaultAuthenticationStrategy }.
41+ * Simple tests for {@link ProtocolSwitchStrategy }.
4142 */
4243class TestProtocolSwitchStrategy {
4344
@@ -95,4 +96,120 @@ void testSwitchInvalid() {
9596 Assertions .assertThrows (ProtocolException .class , () -> switchStrategy .switchProtocol (response3 ));
9697 }
9798
99+ @ Test
100+ void testNullToken () throws ProtocolException {
101+ final HttpResponse response = new BasicHttpResponse (HttpStatus .SC_SWITCHING_PROTOCOLS );
102+ response .addHeader (HttpHeaders .UPGRADE , "TLS," );
103+ response .addHeader (HttpHeaders .UPGRADE , null );
104+ Assertions .assertEquals (TLS .V_1_2 .getVersion (), switchStrategy .switchProtocol (response ));
105+ }
106+
107+ @ Test
108+ void testWhitespaceOnlyToken () throws ProtocolException {
109+ final HttpResponse response = new BasicHttpResponse (HttpStatus .SC_SWITCHING_PROTOCOLS );
110+ response .addHeader (HttpHeaders .UPGRADE , " , TLS" );
111+ Assertions .assertEquals (TLS .V_1_2 .getVersion (), switchStrategy .switchProtocol (response ));
112+ }
113+
114+ @ Test
115+ void testUnsupportedTlsVersion () throws Exception {
116+ final HttpResponse response = new BasicHttpResponse (HttpStatus .SC_SWITCHING_PROTOCOLS );
117+ response .addHeader (HttpHeaders .UPGRADE , "TLS/1.4" );
118+ Assertions .assertEquals (new ProtocolVersion ("TLS" , 1 , 4 ), switchStrategy .switchProtocol (response ));
119+ }
120+
121+ @ Test
122+ void testUnsupportedTlsMajorVersion () throws Exception {
123+ final HttpResponse response = new BasicHttpResponse (HttpStatus .SC_SWITCHING_PROTOCOLS );
124+ response .addHeader (HttpHeaders .UPGRADE , "TLS/2.0" );
125+ Assertions .assertEquals (new ProtocolVersion ("TLS" , 2 , 0 ), switchStrategy .switchProtocol (response ));
126+ }
127+
128+ @ Test
129+ void testUnsupportedHttpVersion () {
130+ final HttpResponse response = new BasicHttpResponse (HttpStatus .SC_SWITCHING_PROTOCOLS );
131+ response .addHeader (HttpHeaders .UPGRADE , "HTTP/2.0" );
132+ Assertions .assertThrows (ProtocolException .class , () -> switchStrategy .switchProtocol (response ),
133+ "Unsupported HTTP version: HTTP/2.0" );
134+ }
135+
136+ @ Test
137+ void testInvalidTlsFormat () {
138+ final HttpResponse response = new BasicHttpResponse (HttpStatus .SC_SWITCHING_PROTOCOLS );
139+ response .addHeader (HttpHeaders .UPGRADE , "TLS/abc" );
140+ Assertions .assertThrows (ProtocolException .class , () -> switchStrategy .switchProtocol (response ),
141+ "Invalid protocol: TLS/abc" );
142+ }
143+
144+ @ Test
145+ void testHttp11Only () {
146+ final HttpResponse response = new BasicHttpResponse (HttpStatus .SC_SWITCHING_PROTOCOLS );
147+ response .addHeader (HttpHeaders .UPGRADE , "HTTP/1.1" );
148+ Assertions .assertThrows (ProtocolException .class , () -> switchStrategy .switchProtocol (response ),
149+ "Invalid protocol switch response: no TLS version found" );
150+ }
151+
152+ @ Test
153+ void testSwitchToTlsValid_TLS_1_2 () throws Exception {
154+ final HttpResponse response = new BasicHttpResponse (HttpStatus .SC_SWITCHING_PROTOCOLS );
155+ response .addHeader (HttpHeaders .UPGRADE , "TLS/1.2" );
156+ final ProtocolVersion result = switchStrategy .switchProtocol (response );
157+ Assertions .assertEquals (TLS .V_1_2 .getVersion (), result );
158+ }
159+
160+ @ Test
161+ void testSwitchToTlsValid_TLS_1_0 () throws Exception {
162+ final HttpResponse response = new BasicHttpResponse (HttpStatus .SC_SWITCHING_PROTOCOLS );
163+ response .addHeader (HttpHeaders .UPGRADE , "TLS/1.0" );
164+ final ProtocolVersion result = switchStrategy .switchProtocol (response );
165+ Assertions .assertEquals (TLS .V_1_0 .getVersion (), result );
166+ }
167+
168+ @ Test
169+ void testSwitchToTlsValid_TLS_1_1 () throws Exception {
170+ final HttpResponse response = new BasicHttpResponse (HttpStatus .SC_SWITCHING_PROTOCOLS );
171+ response .addHeader (HttpHeaders .UPGRADE , "TLS/1.1" );
172+ final ProtocolVersion result = switchStrategy .switchProtocol (response );
173+ Assertions .assertEquals (TLS .V_1_1 .getVersion (), result );
174+ }
175+
176+ @ Test
177+ void testInvalidTlsFormat_NoSlash () {
178+ final HttpResponse response = new BasicHttpResponse (HttpStatus .SC_SWITCHING_PROTOCOLS );
179+ response .addHeader (HttpHeaders .UPGRADE , "TLSv1" );
180+ Assertions .assertThrows (ProtocolException .class , () -> switchStrategy .switchProtocol (response ),
181+ "Invalid protocol: TLSv1" );
182+ }
183+
184+ @ Test
185+ void testSwitchToTlsValid_TLS_1 () throws Exception {
186+ final HttpResponse response = new BasicHttpResponse (HttpStatus .SC_SWITCHING_PROTOCOLS );
187+ response .addHeader (HttpHeaders .UPGRADE , "TLS/1" );
188+ final ProtocolVersion result = switchStrategy .switchProtocol (response );
189+ Assertions .assertEquals (TLS .V_1_0 .getVersion (), result );
190+ }
191+
192+ @ Test
193+ void testInvalidTlsFormat_MissingMajor () {
194+ final HttpResponse response = new BasicHttpResponse (HttpStatus .SC_SWITCHING_PROTOCOLS );
195+ response .addHeader (HttpHeaders .UPGRADE , "TLS/.1" );
196+ Assertions .assertThrows (ProtocolException .class , () -> switchStrategy .switchProtocol (response ),
197+ "Invalid protocol: TLS/.1" );
198+ }
199+
200+ @ Test
201+ void testMultipleHttp11Tokens () {
202+ final HttpResponse response = new BasicHttpResponse (HttpStatus .SC_SWITCHING_PROTOCOLS );
203+ response .addHeader (HttpHeaders .UPGRADE , "HTTP/1.1, HTTP/1.1" );
204+ Assertions .assertThrows (ProtocolException .class , () -> switchStrategy .switchProtocol (response ),
205+ "Invalid protocol switch response: no TLS version found" );
206+ }
207+
208+ @ Test
209+ void testMixedInvalidAndValidTokens () {
210+ final HttpResponse response = new BasicHttpResponse (HttpStatus .SC_SWITCHING_PROTOCOLS );
211+ response .addHeader (HttpHeaders .UPGRADE , "Crap, TLS/1.2, Invalid" );
212+ Assertions .assertThrows (ProtocolException .class , () -> switchStrategy .switchProtocol (response ),
213+ "Invalid protocol: Crap" );
214+ }
98215}
0 commit comments